Hi, thank you for this great library, it saved my day (actually month) until now.
I use this in a somewhat complex environment. My PC connected to a Arduino Mega which acts as a gateway. Mega is also connected to 4 other Arduino's (1 Uno, 2 Nano's) though 3 additional HW serial interfaces. I designed a custom protocol to send states, sensor readings, commands etc.
While debugging I find out that the handler is called although there shouldn't be a packet (I send packets rather slow, every 4 seconds).
Here is some related parts of the code on the Mega:
`
...
PacketSerial packetSerialPC;
PacketSerial packetSerialSense;
PacketSerial packetSerialLeft;
PacketSerial packetSerialRight;
...
void setup () {
// SERIAL - PC
Serial.begin(BAUDRATE_UPSTREAM);
Serial.setTimeout(DURATION_SERIAL_TO);
packetSerialPC.setStream(&Serial);
packetSerialPC.setPacketHandler(&packetSerialHandler);
// SERIAL - ARDUINO NANO (LEFT)
Serial1.begin(BAUDRATE_DOWNSTREAM);
Serial1.setTimeout(DURATION_SERIAL_TO);
packetSerialLeft.setStream(&Serial1);
packetSerialLeft.setPacketHandler(&packetSerialHandler);
// SERIAL - ARDUINO UNO (MIDDLE=SENSE)
Serial2.begin(BAUDRATE_DOWNSTREAM);
Serial2.setTimeout(DURATION_SERIAL_TO);
packetSerialSense.setStream(&Serial2);
packetSerialSense.setPacketHandler(&packetSerialHandler);
// SERIAL - ARDUINO NANO (RIGHT)
Serial3.begin(BAUDRATE_DOWNSTREAM);
Serial3.setTimeout(DURATION_SERIAL_TO);
packetSerialLeft.setStream(&Serial3);
packetSerialLeft.setPacketHandler(&packetSerialHandler);
// ....
}
void packetSerialHandler(const void* sender, const uint8_t* buffer, size_t size) {
uint8_t rec_length = buffer[0]; // length in the record
char str[64];
snprintf(str, 64, "[GOT] PACKET FROM=%d TO=%d SIZE=%d LEN=%d", buffer[1], buffer[2], size, buffer[0]);
send_text(ADDR_PC, str);
if ((size <= 3) || (size != rec_length)) { // handle possible invalid packets
return;
}
...
if (sender == &packetSerialPC) {
...
} else if (sender == &packetSerialSense) {
...
} else if (sender == &packetSerialLeft) {
...
} else if (sender == &packetSerialRight) {
...
} else {
send_text(ADDR_PC, "[ERROR] ILLEGAL SENDER!");
}
...some forwarding code...
}
`
I've been using this for a while now implementing upwards messaging, but when I started downward messages/commands (sent by a QT program) I added the "snprintf" debug lines and saw a lot of invalid packet calls which have been filtered out.
The system is on my lab desk, connected through short lines. No particular noise source.
The protocol contains a header:
Byte 0 : Length of the message sent
Byte 1 : Source address
Byte 2 : Destination address
There are 5 devices and 4 special addresses defined in an enum, so the address fields must be between 0-8.
From the debug log on the PC:
This is a valid packet:
2018.12.03 15:46:14 [B] MSG:[GOT] PACKET FROM=1 TO=4 SIZE=22 LEN=22
These are from consecutive invalid calls to the handler:
2018.12.03 15:46:15 [B] MSG:[GOT] PACKET FROM=7 TO=106 SIZE=0 LEN=0
2018.12.03 15:46:15 [B] MSG:[GOT] PACKET FROM=138 TO=11 SIZE=0 LEN=3
2018.12.03 15:46:15 [B] MSG:[GOT] PACKET FROM=138 TO=11 SIZE=0 LEN=3
(about 20 such until)
2018.12.03 15:46:15 [B] MSG:[GOT] PACKET FROM=1 TO=4 SIZE=10 LEN=10
As I get out of scope addresses, 0 size and/or non-matching size/len, the data buffer passed must be invalid.
Any idea why I might get these? I don't see any reason by looking at the code...