11-06-2012 03:07 PM
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
Solved! Go to Solution.
11-06-2012 11:34 PM
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;
11-07-2012 12:05 AM
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;
11-09-2012 04:20 AM
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