I am trying to implement a simple serial port protocol. It goes like this:
- discard all data until
0xff
is received - read header (node address and data length, 4 bytes)
- read data (max. 64 bytes)
- read crc
- process the received packet
- send response
- when
0xff
is seen, even when not expected like in the middle of the data, it means a new packet is received
I can implement this using boost::asio::serial_port
with boost::asio::read()
reading a single byte and processing this byte when it is received. Although this works, I was wondering if there is a more 'boost' like way to do this?
I looked at boost::asio::read_until()
for reading until 0xff
, but then I don't know how to discard the data. Storing the data in a buffer and then not using the buffer seems a bit wasteful.
I can use boost::asio::read_until()
for reading until the end of the packet but then the MatchCondition
needs to have access to (the header of the packet in) the buffer. It seems the MatchCondition
only gets an iterator to the first and last byte recently received.
Also, the data received using boost::asio::read()
ends up in a stream_buf
and I have to parse the received data into a Packet
object. I can do this parsing inside Packet
, in a separate ParsePacket
object or somehow integrate it with boost::asio
(something like boost::asio::read(serial, myPacket);
where myPacket
is a Packet
object)
When 0xff
is seen anywhere in the received data, it means a new packet is starting. So when 0xff
is received, it must forget any previous received data and start receiving a new packet.
I am planning on using asynchronous operations and adding timeouts.
So, my question is: where to implement a protocol like this? Or more generally, where to implement a protocol using boost::asio
. I am not looking for working code but something like advise on where to implement the protocol and which boost::asio
functionality to use.
update:
There is no flow control (hardware or software) used in this case.
0xff
? – James0xff
, also the data and the headers never contain0xff
.0xff
is reserved for the beginning of a new packet. – rve