LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Use of Hex data

Solved!
Go to solution

Anyone who can help:

A temperature gauge we are using is sending (via RS232) HEX data in the following array format:

(Sample data)

 

byte0  0x11

byte1  0x02

byte2  0x01

byte3  0x36

byte4  0x00

byte5  0x4B

 

byte1 high and low nibbles represents the temperature 'xy' of 'zx.y' degrees C, and the low nibble of byte2 represents the temperature 'z', so the temperature reading would be '10.2' degrees C from the sample data.  I assume that byte1 can not exceed 0x99, and byte2 cannot exceed 0x09, giving a maximum temperature of 99.9degC, but have not been able to test this yet.

 

Please can anyone help me in the code to obtain the high and low nibbles from the sample data as shown?   I am using CVI, not LabView, which I understand does have inbuilt functions for obtaining the 'nibbles'.

 

Many thanks for any help,

 

Allen Cherry

0 Kudos
Message 1 of 4
(3,344 Views)

Supposing RS232 data are store in a string 'msg', your data can be found in msg[1] and msg[2] respectively.

The correct value can be obtained with:

double  val;

val = (double)((msg[2] << 8) + msg[1])) / 10.0;

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 4
(3,339 Views)

What I wrote above works if your instrument is sending full HEX data, i.e. every byte can have values from 0x0 up to 0xFF. From your description it appears it is sending data in BCD format instead. If this is the case you can extract single nibbles by appropriately ANDing and shifting every byte in the string:

 

char  lowB, highB;
double  val;

highB = (msg[1] & 0xF0) >> 4;
lowB = msg[1] & 0xF;
val = (double)highB + (double)lowB / 10.0;
lowB = msg[2] & 0xF;
valo += (double)lowB * 10.0;

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 4
(3,336 Views)
Solution
Accepted by topic author allencherry

Roberto,

 

Many thanks for your help - I assumed that the variable 'valo' needed to be declared as a 'double' variable - when I had done that, it worked OK - at least with '+ve' temperatures, I will have to wait to see what happens with '-ve' temperatures.

 

Very much appreciate your assistance,

 

Allen

0 Kudos
Message 4 of 4
(3,299 Views)