send and receive HEX over TTL


On the serial monitor I get back some ASCII code.

Why are you sending binary data to a device that expects ASCII data?

I want to save all 5 bytes in a variable

Of what type? And why? Specifically, why not just send that data in the first place?

To read the 5 incoming bytes (0x0F, 0xBB, 0x06, 0x00, 0x00) I could use this code:

You need more than that, but that's the significant statement. So, what is the problem?


Send 5 bytes. Get 5 bytes back. Store them in ONE variable. Again, I'm asking why? There are no 5 byte or larger variables on the Arduino, except long long, which you probably do not want to use.


I will use this data to give information about the device status.

Give what information to what?

In the end I just want to be able to compare this reply

You can compare arrays, so collapsing the array into a single variable is not necessary.

I thought a variable would be suitable.

But, what type of variable?

Maybe String would be possible?



So how can I do it with the array?

uint8_t uselessCrapOut[] = { 0x0F, 0xBB, 0x06, 0x00, 0x00 };
uint8_t uselessReply[5];

uint8_t properReply[5] = { 0x0F, 0xAB, 0x03, 0x04, 0x01 };

// Send
Serial.write(uselessCrapOut, 5);

// Receive
if(Serial.available >= 5)
   Serial.readBytes(uselessReply, 5);

// Compare
if(memcmp(uselessReply, properReply, 5) == 0)
  // Match

The 0FBB060000 hex string doesn't seem to match the 0x0F, 0xBB, 0x06, 0x00, 0x00 format in the provided snippits-r-us example. Will the hex bytes be represented with the 0x characters as hex identifiers?


Am I on the right track?

Does the code do what you want?


I do get some feedback over USB, but I don't know if it works over Tx Rx as well.

Which Arduino do you have? For most, "over USB" and "over Tx Rx" mean exactly the same thing.

Since I always get back some characters instead of hex code it does make it a bit weird to me.

The code and the serial output? Though, if you are talking to the lawn mower using the serial port, you can't also talk to the PC, so I see no basis for this statement.

Should I use if(Serial.available() >= 5) in the loop or is it ok to use it directly after sending?

Yes. Think about that statement, though. Sending serial data is like typing in this text window, and the hitting Post.

Can you reasonably expect the reply to be there immediately?

I'm afraid of having a timeout because the Automower is too slow in response.


So maybe including a delay before reading would work as well, since the Arduino has a buffer for serial communication.

"would work" and "would work as well" are not the same thing.


The serial monitor outputs characters instead hex code. But if it is only for better reading, ok.

The code that does that? We ARE trying to help you, with the code, but, more importantly, with the process of writing and understanding the code.

These two println() statements are going to product different output.

int val = 0x40;
Serial.println(val, HEX);

One is going to output "characters" while the other is going to output "hex code". In reality, both are outputting characters; it's just that the characters are different.

So I have to watch for response in loop and access the response in the switch case somehow.

Yes. Though I don't know what switch case you are referring to.

This could be difficult.

Or easy. If you haven't tried, how do you know?

Therefore I wanted to wait a bit.

How long? That is the key question.



Automower.ino (6.72 KB)