I am trying to implement a simple serial port protocol. This happens as follows:
- discard all data until
0xff is received - read the header (node address and data length, 4 bytes)
- reading data (maximum 64 bytes)
- read crc
- process received packet
- send response
- when
0xff displayed, even if it is not expected, as in the middle of the data, it means that a new packet has been received.
I can implement this using boost::asio::serial_port with boost::asio::read() , reading one byte and processing that byte when it is received. Although this works, I was wondering if there is a way to “zoom in” to do this?
I looked at boost::asio::read_until() to read to 0xff , but then I do not know how to discard the data. Storing data in a buffer and then not using a buffer seems a bit wasteful.
I can use boost::asio::read_until() to read to the end of the package, but then MatchCondition must have access to the (buffer) in the buffer. MatchCondition seems to only get the iterator of the first and last bytes received recently.
In addition, the data received with boost::asio::read() ends with stream_buf , and I have to parse the received data into a Packet object. I can do this parsing inside a Packet in a separate ParsePacket object ParsePacket or somehow integrate it with boost::asio (something like boost::asio::read(serial, myPacket); where myPacket is a Packet object)
When 0xff displayed anywhere in the received data, it means that a new package is starting. Therefore, when 0xff received, it must forget any previous data received and start receiving a new packet.
I plan to use asynchronous operations and add timeouts.
So my question is: where to implement such a protocol? Or, more generally, where to implement the protocol using boost::asio . I'm not looking for working code, but something like a recommendation on where to implement the protocol and what functions of boost::asio use.
update:
In this case, no flow control (hardware or software) is used.
c ++ protocols boost-asio
rve
source share