LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to parse 16 bit signed integer

Currently, I am attempting to collect meaningful data from a flow meter connected through serial communication (RS-232 port).  The received data is a 16 bit signed integer in two's complement representation i.e. the 16 bit signed integer is in binary format with 2 bytes synchronization  preceding. 

For each value from the flow meter the byte sequence recevied is as follows:  0x7F (sync) , 0x7F (sync) , MSB , LSB.  When parsing, I would like to ignore the buffers and store/convert the correct values from the flow meter into decimals, but I do not know how to do that.  I attempted to use Instrument I/O Assistant and the programming similar to it, but I don't know how to identify the buffers as seperators. 

Do any of you know how to do this?  Let me know if you need more information. Your help will be most appreciated. 
0 Kudos
Message 1 of 7
(3,324 Views)
Just take the MSB,LSB substring (offset 2, lenght 2) and typcast it into a I16.
0 Kudos
Message 2 of 7
(3,313 Views)
Yeah I did that.  It would be very simple if only one value comes at a time, however each time the Instrument I/O Assistant runs a large array of approximately 100 values are received.  I found it extremely tedious to have to select every correct substrings resulting in countless number of tokens, which is why I was hoping there is a way to treat the buffers as seperators so that I can get an array of the resulting values as I16 rather than one long string including the buffers and values.  Also,  I can not select all substrings one time and assume it will work every time since the number of values received each time the Instrument I/O Assistant runs is not consistent, hence the need for seperators or other possible solutions.

Sorry for not explaining well enough at first.  I hope you can still help me.

0 Kudos
Message 3 of 7
(3,311 Views)
Well, cast the entire string as an array of U32, use "split numbers" to get the last 16 bits each, cast to I16. 🙂
 
(look ma, no loops! :D)

Message Edited by altenbach on 10-12-2005 09:57 AM

0 Kudos
Message 4 of 7
(3,307 Views)
My example is not as elegant but will handle async serial reads and incomplete data sets.  Everything hinges on the commas being present in the data string as indicated by the posting.  You can easily modify it to deal with the data without commas though.
LabVIEW 7.1.1 example attached

Paul
0 Kudos
Message 5 of 7
(3,298 Views)
Thank you all for the help.  I've learned new things from the both of your suggestions.  But I still haven't been able to resolve my issue.

The first one with the typecast is elegant and impressive; however it would not work in my case because there is no error check which I have been trying to think of a way to implement but achieved no success so far.  I used VISA Read to collect the data from my flow meter and occassionally the data received is scrambled a bit where some of the syncs do not come out as 7F unfortunately.  Also the main issue is that sometimes there is only one sync byte at the beginning of the string rather than two sync bytes or MSB and LSB which was assumed.  Is there a way to circumvent this?

The last code has the error check and is well rounded covering several potential issues; however the data streaming from the device do not contain any commas so I tried to modify the ASRL-DataParseExample.vi by eliminating the commas as delimiters.    It still does not work because for some reason when I used VISA read, the raw data is not in hexidecimals (7F7F... etc.) but rather a series of white squares, dashes, and strange characters which is the string format of the hexidecimals i supposed.  I tried to convert the strings into hexidecimals before parsing but that did not work.

I hope you guys are still able to help me out.  It would be most appreciated.
0 Kudos
Message 6 of 7
(3,273 Views)
If you have a variable number of "delimiters" (7F), you can use "scan strings for tokens" with x7F as delimiter. By default, it disallows empty tokens thus any number of consecutive delimiters are contracted into one.
 
The attached example (LabVIEW 7.0) takes all 2byte data that is seperated by 7F and casts it as I16 and appeded to the output array. Whenever the number of bytes between delimiters is not 2, that data is dropped. Modify as needed. 🙂
0 Kudos
Message 7 of 7
(3,260 Views)