Problems with accessing hexadecimal response string


Recommended Posts

Hello together,

I have got following problem:
I am trying to handle some data which I receive from a serial device. Sending request to the slave works so far. In the monitor I am able to see the Response string:

Rx (15:43:25.533): \xC1\x14\x00d\x00\x00H\x07\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x11\x1F\x00\x04\x00\x00\x00`\x83

The values displayed here are valid. I have problems with further accessing/handling this data. I try to assign it to a string in following way:

private.strOut = Device.RS485_Test.Read(46)

The resulting string is 46 characters long. (checked with the GetLength function). The values it contains, don't really make sense - I guess I have to convert them? 
Subsequently I want to to use this string for accessing a particular value (e.g. the 35th character in the string)  

My goal is to assing this value to a channel and to log it (with a time stamp).

Do you have any idea/solution for me?

Best regards and thank you in advance

Link to comment
Share on other sites

Its important to remember that a string can be a bunch of ASCII characters, and thus something readable by humans, like "this is a test", but really a string of ASCII codes is just a string of numbers that happen to be in the range of 32-127 and thus in the range of ASCII.  A string is actually a string of bytes, and so can hold any value from 0 -255.   To make things easier, DAQFactory uses strings to pass data with serial streams.  This is especially convenient when talking to serial devices that talk in ASCII, but works just as well when using byte streams like you are.  The trick with byte streams is to convert the DAQFactory string to an DAQFactory array of bytes and vice-versa.  There are two functions for that, asca() and chra().  ASCA() converts from a string to an array of bytes and is the one you want.  So after your read(46), put:

private datain = asca(strOut)

then datain will be an array and you can access individual elements using subsetting.  So, for example the 35th character would then be datain[34]  (note that its always numbered from 0).

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.