Shop OBEX P1 Docs P2 Docs Learn Events
receiving and combining HEX Bytes — Parallax Forums

receiving and combining HEX Bytes

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
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

  • tonyp12tonyp12 Posts: 1,950
    edited 2017-07-21 15:28
    what does the compiled code look like?
    You are using parentheses , so the trap of that << may come after | is not there.
    for a msp430 (with no optimization setting)
    mov.w   #0x0B,R10
    mov.w   #0x80,R11
    clr.w   R8
    clr.w   R9
    
    mov.w   R11,R15   // move 0x80 in
    and.w   #0xFF,R15 // clear upper byte
    swpb    R15       // <<8   
    bis.w   R10,R15   // or'ing in 0x0b
    
    R15= 0x800B = ‭32779‬
  • bnikkel wrote: »
    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

    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 ?
  • sorry her is the correct and full code I am using
    #include "simpletools.h"                      // Library includes
    #include "fdserial.h"
    
    fdserial *lcd;
    
    int main()
    {
                  
     lcd = fdserial_open(2, 3, 0, 9600); 
            
    
      int num1;
      int num2;
      int num3; 
      int num4;
      int address;    
    while(1)
    {
                       
                  num1 = fdserial_rxChar(lcd);
                  num2 = fdserial_rxChar(lcd);
                  num3 = fdserial_rxChar(lcd);
                  num4 = fdserial_rxChar(lcd);
                 
       address =(num2<<8)|(num1);
      print("DEC-%d  HEX-%x \n\n",address,address);    
                             
                       
    pause(100);
    
       
      }    
    }
    
  • i guess i was hoping to be able to just shift all the bits as they come in and only use one variable and discard the 0x00 0x00
  • JonnyMacJonnyMac Posts: 8,927
    edited 2017-07-21 17:14
    I'm not a routine user of C, but couldn't you code the capture like this?
      long result = 0;
    
      for(int i = 0; i < 4; i++) {
        result |= fdserial_rxChar(lcd) << (i << 3);
      }
    
  • tonyp12tonyp12 Posts: 1,950
    edited 2017-07-21 18:05
    I prefer while() over for()
    long result = 0;
    int i=0;
    while (i <= 24) {
       result |= (fdserial_rxChar(lcd) << i );
       i +=8;
     }
    

    reordering Endianness is a good job for union.
    union{
    long result;
    struct{ 
      char resultb0; // stored little-endian in ram
      char resultb1;
      char resultb2;
      char resultb3;
     }
    }
    ...
    while(1){
    resultb0 = fdserial_rxChar(lcd);
    resultb1 = fdserial_rxChar(lcd);
    resultb2 = fdserial_rxChar(lcd);
    resultb3 = fdserial_rxChar(lcd);
    
    print("DEC-%d  HEX-%x \n\n",result,result);
    
  • bnikkel wrote: »
    i guess i was hoping to be able to just shift all the bits as they come in and only use one variable and discard the 0x00 0x00

    So, you just want to optimize it a bit, in that case, aside from the suggestions posted by others, this should work:
    #include "simpletools.h"                      // Library includes
    #include "fdserial.h"
    
    fdserial * lcd;
    
    int main()
    {
        int address;
    
        lcd = fdserial_open(2, 3, 0, 9600);
    
        while (1)
        {
    
            address = fdserial_rxChar(lcd);
            address |= fdserial_rxChar(lcd) << 8;
            address |= fdserial_rxChar(lcd) << 16;
            address |= fdserial_rxChar(lcd) << 24;
    
            print("DEC-%d  HEX-%x \n\n", address, address);
    
            pause(100);
        }
    }
    
Sign In or Register to comment.