# Hex over uart & checksum calculations

Hi,

I am trying to interface the Arduino with a sensor over UART. I know that the voltage levels are compatible, so it shouldn’t be too hard (typical 56700-8-N-1 stuff).

My question has to do with sending hexadecimal data over UART. I’m pretty solid with the basics of the Arduino, but have never done anything involving hexadecimal before.

The datasheet defines everything as some sort of hex message. Do I send it as regular ASCII, or do I need to convert it first? I know about serial.println(val,hex);, but don’t know if this is already “preformatted” or not (i.e., since I have it in 0x format, do I just send as ASCII, or do I send 0x07,hex?). If I need to convert it, is there an inbuilt function for how to do so?

It’s something like this:
Start, command, length, data, checksum
<0x01><0x27><0x02><0x0003>

The checksum is calculated by (~x+1)&0xFF , where x is the sum of all the message bytes (how would I calculate this simply?).

I was planning on using NewSoftSerial if it makes any difference.

Thanks for you time! I do appreciate the help. My searching turned up a few results, as I said, but I don’t get what format I need to input into things like serial.println(val,hex).

I know about serial.println(val,hex);, but don't know if this is already "preformatted" or not (i.e., since I have it in 0x format, do I just send as ASCII, or do I send 0x07,hex?).

The second argument to the serial.print() function defines the base to be used when converting the value to a string.

If the device you are talking to says that it expects hex values, it means that it wants binary data, not string data, and, therefore, Serial.print() is the wrong function to use. Serial.write() is the correct function.

Thank you! I will try that out tonight.