Message Boards Message Boards

Establish a i2c communication using DeviceRead and WL?

Posted 5 years ago

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

POSTED BY: Laszlo Sturmann
4 Replies

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

POSTED BY: Laszlo Sturmann

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.

Regards,

Neil

POSTED BY: Neil Singer

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

POSTED BY: Laszlo Sturmann

Laszlo,

DeviceRead and DeviceReadList take the same arguments as BinaryRead, however, the device driver needs to support the format you want.

DeviceRead[device,{param1,param,...}]

I do not know the particulars of the device driver you are using. The examples show "Real" and "Integer" as options but others may be supported.

You can convert a list of bytes to another format using the Binary reading functions (making sure you have the correct ByteOrdering):

str = StringToStream[StringJoin[FromCharacterCode[lst]]]
BinaryReadList[str, "Integer32", numberOfIntegers, ByteOrdering -> 1]

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract