Shop OBEX P1 Docs P2 Docs Learn Events
Send Hex With fdserial.h — Parallax Forums

Send Hex With fdserial.h

I am trying to send the following using fdserial.h:

0xBE
0xEF
0
3
1
4
then a checksum which I think is 2

The problem is when receiving with the Parallax Serial Terminal, when I try to send 0xBE I get a symbol which is small, but I think it's the symbol "3/4"

How can I send something that will show up in Parallax Serial Terminal as BE, and hopefully be accepted as a Hex value rather than a string?

Thanks for the help.

Comments

  • You'll want to use dprinti (or dprint if floating point is needed) from simpletext.h.

    Example:
    fdserial *serial = fdserial_open(31, 30, 0, 115200);
    
    dprinti(serial, "0x%02x\n", 0xBE);
    dprinti(serial, "0x%02x\n", 0xEF);
    
    dprinti(serial, "%d\n", 0);
    dprinti(serial, "%d\n", 3);
    dprinti(serial, "%d\n", 1);
    dprinti(serial, "%d\n", 4);
    
  • I'll try this in a bit. Thanks!

    I'd gotten it to kind of work. The problem (I think) was it sending lower case Hex (definitely lower case), then the handshake fails. I wonder if the handshake is case sensitive.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2015-11-10 21:25
    The problem is you were sending a literal byte value (0xBE). What you want to do is send the bytes for the characters 'B' and 'E', which are byte values 66 (0x42) and 69 (0x45) respectively.

    The functions printf / dprint / etc are doing a number to "ascii hex" conversion, which takes 4-bit chunks of the number and sends them as the ascii characters '0' to '9' and 'A' to 'F', to represent the 16 values possible for those 4 bits.

    You can do it yourself easily, like this:
    void SendHex( int value )
    {
      int shift = 32-4;  // Start with the upper four bits
    
      // Scan the value until we find some non-zero bits (removes leading zeros)
      while(  ((value >> shift) & 0xF) == 0  &&  shift > 0 )
        shift -= 4;
    
      do {
        int nybble = (value >> shift) & 0xF;    // Grab the four bits starting at index "shift"
        char sendThis;
        if( nybble < 10 )
          sendThis = '0' + nybble;           // values from 0 to 9 are the ascii characters '0' to '9'
        else
          sendThis = 'A' + (nybble-10);      // values from 10 to 15 are the characters 'A' to 'F'
    
        fdserial_txChar( serial, sendThis );
        shift -= 4;
      } while( shift > 0 );
    }
    

    Note that I haven't actually compiled / run this code, but hopefully it conveys the idea. :)
  • Thanks Jason I'll try that out.
Sign In or Register to comment.