Async Comm
A library for asynchronous serial communication
Macros | Enumerations | Functions | Variables
serial_protocol.cpp File Reference
#include <async_comm/serial.h>
#include <cstdint>
#include <cstdio>
#include <chrono>
#include <condition_variable>
#include <mutex>

Go to the source code of this file.

Macros

#define NUM_MSGS   40000
 
#define START_BYTE   0xA5
 
#define START_BYTE_LEN   1
 
#define PAYLOAD_LEN   12
 
#define CRC_LEN   1
 
#define PACKET_LEN   (START_BYTE_LEN + PAYLOAD_LEN + CRC_LEN)
 
#define BAUD_RATE   921600
 
#define NUM_START_BITS   1
 
#define NUM_STOP_BITS   1
 

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
 

Detailed Description

Author
Daniel Koch danie.nosp@m.lpko.nosp@m.ch@gm.nosp@m.ail..nosp@m.com

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.

Function Documentation

§ callback()

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.

Parameters
bufReceived bytes buffer
lenNumber of bytes received

Definition at line 241 of file serial_protocol.cpp.

§ pack_message()

void pack_message ( uint8_t *  dst,
uint32_t  id,
uint32_t  v1,
uint32_t  v2 
)

Pack message contents into a buffer.

Parameters
[out]dstBuffer in which to store the message
[in]idID field of the message
[in]v1First data field of the message
[in]v2Second data field of the message
Postcondition
The specified buffer contains the complete message packet, including start byte and CRC byte

Definition at line 125 of file serial_protocol.cpp.

§ parse_byte()

void parse_byte ( uint8_t  byte)

Passes a received byte through the parser state machine.

Parameters
byteThe byte to process

Definition at line 184 of file serial_protocol.cpp.

§ unpack_payload()

void unpack_payload ( uint8_t *  src,
uint32_t *  id,
uint32_t *  v1,
uint32_t *  v2 
)

Unpack the contents of a message payload buffer.

Parameters
[in]srcThe buffer to unpack
[out]idID field of the message
[out]v1First data field of the message
[out]v2Second data field of the message
Precondition
Buffer contains a valid message payload
Postcondition
Payload contents have been placed into the specified variables

Definition at line 151 of file serial_protocol.cpp.

§ update_crc()

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

Parameters
inCrcThe current CRC value. This should be initialized to 0 before processing first byte.
inDataThe byte being processed
Returns
The new CRC value

Definition at line 93 of file serial_protocol.cpp.