receiving and combining HEX Bytes
bnikkel
Posts: 104
in Propeller 1
Hi Everyone, I have a device that is sending out the number 32779, it is sent in hex as 0x0b 0x80 0x00 0x00. I am trying to make my code a little more compact by shifting the first two bytes and discarding the 0x00 0x00 but I cant seem to get the order right.
here is how I'm currently doing it
here is how I'm currently doing it
int main() { lcd = fdserial_open(2, 3, 0, 9600); int num1; int num2; int num3; int num4; while(1) { num1 = fdserial_rxChar(lcd); num2 = fdserial_rxChar(lcd); num3 = fdserial_rxChar(lcd); num4 = fdserial_rxChar(lcd); num3 =(num2<<8)|(num1); print("DEC-%d HEX-%x \n\n",num3,num3); pause(100); } }
Comments
You are using parentheses , so the trap of that << may come after | is not there.
for a msp430 (with no optimization setting) R15= 0x800B = 32779
Your code seems to work correctly, at least the part of combining the received bytes (just a note: your device is not sending HEX numbers, it is sending BINARY values). You can easily verify by manually assigning the values to num1 and num2 instead of receiving from the serial port.
Are you sure that this is the actual code you are using and not a stripped down version of what you think is the part that doesn't work ?
reordering Endianness is a good job for union.
So, you just want to optimize it a bit, in that case, aside from the suggestions posted by others, this should work: