I need to send 4 bits in hex to a sensor via serial to set up some functions. What is the best way to do this?
For example : "2hz setting instruction ----- "(0xA5+0x25+0x01+0xCB)"
I need to send 4 bits in hex to a sensor via serial to set up some functions. What is the best way to do this?
For example : "2hz setting instruction ----- "(0xA5+0x25+0x01+0xCB)"
Did you mean four BYTES?
const byte Set2Hz[] = {0xA5, 0x25, 0x01, 0xCB};
Serial.write(Set2Hz, 4);
Yes BYTES, sorry. Thanks for the help :D.
An alternative that can save RAM space but only works for sequences that DON'T contain 0x00:
Serial.print(F("\xA5\x25\x01\xCB")); // Set to 2 Hz
The "F()" macro is a trick to keep strings in FLASH instead of copying them to RAM if they are only going to be printed. Your 'string' can't contain a "\x00" because that would be taken as the end-of-string marker.
Thanks :D, works. And how can I read the 4 bytes I will receive from the device? I didn't find any examples.
Rorschach_1:
And how can I read the 4 bytes I will receive from the device? I didn't find any examples.
With Serial.read(), one byte at a time.
if (Serial.available() >= 4)
{
Buffer[0] = Serial.read();
Buffer[1] = Serial.read();
Buffer[2] = Serial.read();
Buffer[3] = Serial.read();
}