Hello there,
I have a project where I will be monitoring a serial data line that I have zero control over. This data line sends regular messages of varying lengths at regular intervals, and my Arduino Mega could join the "call" at any point, thus not knowing if the bytes coming in are actually the start of a sequence or the middle of one. This serial data line is standard 9600 baud 8N1, and the only rules are that the first two bytes of any message constitute the device ID and the message length, while the final byte is always a checksum byte. However, these three bytes are not exclusive, and as such can appear in the middle of a message (albeit the chances of those three being present in a sequence that would have the checksum byte work out mid-message is approximately zero).
My project looks to listen for an incredibly specific message on said data line, and then immediately respond with a specific sequence of bytes if this message is detected, then idle and wait for the next time said specific message is received. For reference, the message I intend to react to will always look like this:
75 60 {any byte} {any byte} {any byte} {any byte} {any byte} {checksum}
The parts marked {any byte} could be just that; any byte. And as a result, the checksum will fluctuate based on the contents of those bytes. The checksum is a standard two's complement. So as an example of a complete sequence, this could potentially be an incoming message:
75 60 0A 57 60 00 FC 6E
So essentially, the first two bytes of the message will never change, the length of this specific message will always be 8 bytes, the final byte will always be a checksum, but the values of these bytes can appear within a message, and the Arduino could potentially join the bus mid-transmission, so a simple check for a start byte is no good, especially since other messages transmitted by devices on the bus may not be 8 bytes long.
Is there a best practice here? I'm afraid all the searches I've come up with don't appear to touch on this particular issue. I'm particularly worried about speed; the reply to this transmission must happen within 200 milliseconds of receipt or it will be considered "missed" by the device that transmitted it.
Thank you!