Wednesday, March 21, 2012

Arduino: Sending Hex Bytes to Serial Devices



I have been working on a project which involves an Arduino communicating serially with an LCD display device. Through a lot of trial and error, I finally figured out that when you want to send a HEX-encoded byte from the Arduino, you need to use the Serial.write() command rather than the Serial.print() command. The Serial.print() command works find when you are sending an ASCII byte but it will not work when you try to send a HEX byte. The right and wrong ways are below:
//Serial.print('U');  <----right (ASCII)
//Serial.write('U');  <----also fine (ASCII)
//Serial.print(0x55); <----WRONG! (HEX)
//Serial.write(0x55); <----right (HEX)

LINKS:
Similar problem and solution: http://www.arduino.cc/playground/Learning/SparkFunSerLCD

14 comments:

  1. You can also pass in a second argument to specify the base.

    Serial.print(0x55, HEX);

    http://arduino.cc/en/Serial/Print

    ReplyDelete
    Replies
    1. That is not right, it will send 55 in ASCI or 0x35 0x35 in HEX.

      Delete
  2. what about multiple bytes?

    Serial.write(0x55,0x99,0x20,0x60); ????

    ReplyDelete
  3. Thank you so much for this. Straightforward and useful!

    ReplyDelete
  4. I have the same problem....How to sent multiple bytes. I need to send 0x55 0x01 0x00 over the serial port. How is th=his best done?

    ReplyDelete
    Replies
    1. One way is to create a variable at the beginning of your program such as:
      uint8_t my_serial_bytes[3]={0x55, 0x01, 0x00};

      ...then call it like:
      Serial.write(my_serial_bytes,sizeof(my_serial_bytes));

      This is especially helpful when you are using the same serial command frequently in your program. Good luck!

      Delete
    2. Can the same unsigned integer above be read from a port : Serial.read(my_serial_bytes, size of(my_serial_bytes)); ?

      Delete
  5. Ahh finally :)
    Thank you so much.
    i wrote code a couple years ago that worked, now with the update it doesn't work and i had to change the code to be more ... versatile.

    ReplyDelete
  6. Right: LCD
    Right: LC-Display
    Wrong: LCD Display

    ReplyDelete
  7. Hi nick! Thank you so much for this blog!!
    I have a question.
    I am sending a hex packet from one Arduino to another Arduino using two max 485s (so using rs485 protocol).

    If I use a uint8_t array of hex numbers to serial.write to one arduino, how can I read it in the other arduino. I guess my question is, how do you use the serial.read to read in a uint8_t pair by pair?

    ReplyDelete
    Replies
    1. Hi did you figure this out? I have the same problem, I am trying to send a bunch of hex codes using RS485. I have a RS485 shield for arduino and the hex codes I need to send are of the format 0xFF 0x01 0x05 0xAA etc. I'm using hardware serial. Unfortunately I am not getting back anything from the device.

      Delete
    2. Hi, did you try using nick.weil answer?

      ""One way is to create a variable at the beginning of your program such as:
      uint8_t my_serial_bytes[3]={0x55, 0x01, 0x00};

      ...then call it like:
      Serial.write(my_serial_bytes,sizeof(my_serial_bytes));""

      or do find another way??

      Delete
    3. Hello i need this too plz help how to READ HEX from serial

      Delete
  8. http://arduino.cc/en/Tutorial/ASCIITable

    ReplyDelete