Hi,
I can read in 10 bytes from a device object “enc” by DeviceRead(enc,10). The first two bytes represent an unsigned 16-bit integer, the next four a signed 32-bit integer and the rest are bytes.
Is there a function to read these integers directly instead of reading the bytes and then converting them to integers in Mathematica?
Something like BinaryRead[stream, {“type1”,“type2”,…}] works for streams.
Is there a function in Mathematica to convert 4 bytes to a signed integer?
Thanks,
Laszlo
Hi Neil,
Thanks for your comment.
I’m using Mathematica built-in i2c driver to read the position of an encoder by a Raspberry Pi.
I can see the numbers changing in the output of dat = Dynamic[DeviceRead[81,10]] (*address 0x51, # of bytes=10 *) but I’ve yet to find a way to extract members of dat.
I know that Dynamic[] makes dat visible in the FrontEnd and it is not a List but there must be a way to get to these members.
Any thought?
Laszlo
I’m not an expert on the Pi version of Mathematica, but I would think that
dat = DeviceRead[81,10]
should work. You should not assign a variable to a Dynamic in Mathematica if you want to manipulate the bytes. To convince yourself of this do dat //FullForm and you will see that the internal representation is not what you would want.
Neil,
Thanks again. I think I’m going the wrong way in my approach. Indeed //FullForm shows that I can’t access the bytes from the FrontEnd. I can go around this in my application,
Regards,
Laszlo