Async Comm
A library for asynchronous serial communication
|
#include <async_comm/serial.h>
#include <cstdint>
#include <cstdio>
#include <chrono>
#include <condition_variable>
#include <mutex>
Go to the source code of this file.
Enumerations | |
enum | ParseState { PARSE_STATE_IDLE, PARSE_STATE_GOT_START_BYTE, PARSE_STATE_GOT_PAYLOAD } |
States for the parser state machine. | |
Functions | |
uint8_t | update_crc (uint8_t inCrc, uint8_t inData) |
Recursively update the cyclic redundancy check (CRC) More... | |
void | pack_message (uint8_t *dst, uint32_t id, uint32_t v1, uint32_t v2) |
Pack message contents into a buffer. More... | |
void | unpack_payload (uint8_t *src, uint32_t *id, uint32_t *v1, uint32_t *v2) |
Unpack the contents of a message payload buffer. More... | |
void | parse_byte (uint8_t byte) |
Passes a received byte through the parser state machine. More... | |
void | callback (const uint8_t *buf, size_t len) |
Callback function for the async_comm library. More... | |
int | main (int argc, char **argv) |
Variables | |
ParseState | parse_state = PARSE_STATE_IDLE |
Current state of the parser state machine. | |
uint8_t | receive_buffer [PAYLOAD_LEN] |
Buffer for accumulating received payload. | |
volatile int | receive_count = 0 |
Keeps track of how many valid messages have been received. | |
bool | received [NUM_MSGS] |
Keeps track of which messages we've received back. | |
std::mutex | mutex |
mutex for synchronization between the main thread and callback thread | |
std::condition_variable | condition_variable |
condition variable used to suspend main thread until all messages have been received back | |
volatile bool | all_messages_received = false |
flag for whether all messages have been received back | |
This example implements a simple serial protocol, and tests the async_comm library using that protocol on a serial loopback (USB-to-UART converter with the RX and TX pins connected together).
The message defined by the serial protocol has the following format:
Field | Type | Size (bytes) | Description |
---|---|---|---|
Start Byte | 1 | Identifies the beginning of a message, value is 0xA5 | |
id | uint32_t | 4 | Sequential message ID |
v1 | uint32_t | 4 | The first data field |
v2 | uint32_t | 4 | The second data field |
CRC | uint8_t | 1 | Cyclic redundancy check (CRC) byte |
The "payload" of the message is the part that contains the actual data, and consists of the id
, v1
, and v2
fields.
The parser is implemented as a finite state machine.
Definition in file serial_protocol.cpp.
void callback | ( | const uint8_t * | buf, |
size_t | len | ||
) |
Callback function for the async_comm library.
Passes the received bytes through the parser state machine.
buf | Received bytes buffer |
len | Number of bytes received |
Definition at line 241 of file serial_protocol.cpp.
void pack_message | ( | uint8_t * | dst, |
uint32_t | id, | ||
uint32_t | v1, | ||
uint32_t | v2 | ||
) |
Pack message contents into a buffer.
[out] | dst | Buffer in which to store the message |
[in] | id | ID field of the message |
[in] | v1 | First data field of the message |
[in] | v2 | Second data field of the message |
Definition at line 125 of file serial_protocol.cpp.
void parse_byte | ( | uint8_t | byte | ) |
Passes a received byte through the parser state machine.
byte | The byte to process |
Definition at line 184 of file serial_protocol.cpp.
void unpack_payload | ( | uint8_t * | src, |
uint32_t * | id, | ||
uint32_t * | v1, | ||
uint32_t * | v2 | ||
) |
Unpack the contents of a message payload buffer.
[in] | src | The buffer to unpack |
[out] | id | ID field of the message |
[out] | v1 | First data field of the message |
[out] | v2 | Second data field of the message |
Definition at line 151 of file serial_protocol.cpp.
uint8_t update_crc | ( | uint8_t | inCrc, |
uint8_t | inData | ||
) |
Recursively update the cyclic redundancy check (CRC)
This uses the CRC-8-CCITT polynomial.
Source: http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html#gab27eaaef6d7fd096bd7d57bf3f9ba083
inCrc | The current CRC value. This should be initialized to 0 before processing first byte. |
inData | The byte being processed |
Definition at line 93 of file serial_protocol.cpp.