Im pretty new to programming and am just getting into Arduino. I have a project that I have to put together that will listen to the UART port on a device that talks with a HEX protocol. The message length varies from about 12 bytes to over 100 bytes, but each message includes:
Start byte
Payload size info
Some extra bytes always the same in the message
payload
end bytes
The problem is that the start bytes, end bytes etc could be part of the payload data. There is also sometimes some debug info on the port and during powering up can operate at other baud rates (also spitting out debug). The devices usually have an intermittent data byte being output for no apparent reason about every 5 seconds or so.
So, Im not sure which way I need to go to be able to decode whats coming in on the serial port so I can do things with it? Some guidance would be much appreciated!
So, Im not sure which way I need to go to be able to decode whats coming in on the serial port so I can do things with it? Some guidance would be much appreciated!
The first step it to try and identify the protocol and get a published specification for it. It should have a formal name that would help if searching for it's specifications. Without it, it's going to be a little hit and miss trying to write a arduino sketch to interface with a device using that protocol, I would think.
Its not a publicly available protocol, but an example would be something like:
Byte 1 – Fixed field – Always 0x7E
Byte 2 – Field varies with Payload size – Number of payload bytes
Byte 3 - Fixed field – Always 0xFF
Byte 4 to Byte (4+ Number of payload Bytes) – Payload
Byte ((9+ Number of payload Bytes) + 1) - Fixed field – Always 0x9F
Byte ((9+ Number of payload Bytes) + 2) - Fixed field – Always 0x9F
Thats not it, its not an ASCII HEX, its just HEX. If you look with docklight (on the ASCII page) it shows all the unprintable characters. I know how the protocol works, just need to know how to identify that a full command has been sent and pull it out of the other data that may come through.
I know how the protocol works, just need to know how to identify that a full command has been sent
You seem to know that, already. You know what defines the start of a packet (0x7E). You know what defines the end of a packet (2 consecutive 0x9Fs). In between, you have a count and another marker (0xFF). If you read a 0x7E, read and store the next byte. Then, read the next byte, which should be 0XFF. If not, the 0x7E was not the start of a packet. If it was, read and store the data, until two consecutive 0x9Fs arrive.
If the number of bytes received (count them as you store them) matches the number you expect, you got a complete, good packet. Use it.