https://github.com/eric-wieser/packet-io
An arduino library for framing protocols, inspired by PacketSerial
https://github.com/eric-wieser/packet-io
arduino cobs framing-protocols streaming
Last synced: 8 months ago
JSON representation
An arduino library for framing protocols, inspired by PacketSerial
- Host: GitHub
- URL: https://github.com/eric-wieser/packet-io
- Owner: eric-wieser
- License: mit
- Created: 2016-12-22T22:33:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-12-08T23:26:14.000Z (about 5 years ago)
- Last Synced: 2025-03-28T01:48:29.717Z (9 months ago)
- Topics: arduino, cobs, framing-protocols, streaming
- Language: C++
- Homepage: https://packetio.readthedocs.io
- Size: 67.4 KB
- Stars: 23
- Watchers: 2
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
PacketIO [](http://packetio.readthedocs.org)
========
A PlatformIO library for framing packets sent or received over an arduino `Stream`, such as `Serial`.
The framing methods are exposed through the `PacketPrint` and `PacketStream` interfaces.
* `PacketPrint` - extends `Print`:
* `bool end()` - end a packet, returning whether it was sucessfull
* `void abort()` - try as best as possible to start a new packet
* `PacketStream` - extends `Stream`:
* `read()`, `peek()` - return `PacketStream::EOP` (-2) if the packet is complete (without touching the underlying stream), otherwise behaves as normal
* `next()` - stop reading from this packet, and move on
Framing Protocols
-----------------
Implemented so far
* [x] COBS
* [x] SLIPS
* [ ] COBS-R
* [ ] Length-prefixed
Examples
--------
### Writing
```c++
// pick a protocol here - everything below doesn't need to change
COBSPrint cobs_out(Serial);
PacketPrint& out = cobs_out;
out.print("Hello ");
out.print("World");
out.end(); // mark the end of a packet
out.print("Goodbye ");
if(something_went_horribly_wrong) {
out.abort(); // terminate this packet early and try to mark what was sent as bad
}
else {
out.print("World");
out.end();
}
```
### Reading
```c++
// pick a protocol here - everything below doesn't need to change
COBSStream cobs_in(Serial);
PacketStream& in = cobs_in;
while(true) {
char message[10];
size_t n = 0;
while(true) {
// read until we get something
int c = in.read();
if(c == in::EOF) continue;
// detect End Of Packet
if(c == in::EOP) break;
// save anything else
message[n++] = c;
}
Serial.print("Got: ");
Serial.write(message, n);
Serial.println();
// start reading the next message
in.next();
}
```
Future work
-----------
Provide an interface to [`nanopb`](https://github.com/nanopb/nanopb), that converts `Print` into `pb_ostream_s` and `Stream` into `pb_istream_s`, to allow protobufs to be framed.