Async Comm
A library for asynchronous serial communication
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
async_comm::Comm Class Referenceabstract

Abstract base class for an asynchronous communication port. More...

#include <comm.h>

Inheritance diagram for async_comm::Comm:
async_comm::Serial async_comm::TCPClient async_comm::UDP

Public Member Functions

 Comm (MessageHandler &message_handler=default_message_handler_)
 Set up asynchronous communication base class. More...
 
bool init ()
 Initializes and opens the port. More...
 
void close ()
 Closes the port.
 
void send_bytes (const uint8_t *src, size_t len)
 Send bytes from a buffer over the port. More...
 
void send_byte (uint8_t data)
 Send a single byte over the port. More...
 
void register_receive_callback (std::function< void(const uint8_t *, size_t)> fun)
 Register a callback function for when bytes are received on the port. More...
 
void register_listener (CommListener &listener)
 Register a listener for when bytes are received on the port. More...
 

Protected Member Functions

virtual bool is_open ()=0
 
virtual bool do_init ()=0
 
virtual void do_close ()=0
 
virtual void do_async_read (const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
 
virtual void do_async_write (const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
 

Protected Attributes

MessageHandlermessage_handler_
 
boost::asio::io_service io_service_
 

Static Protected Attributes

static constexpr size_t READ_BUFFER_SIZE = 1024
 
static constexpr size_t WRITE_BUFFER_SIZE = 1024
 
static DefaultMessageHandler default_message_handler_
 

Detailed Description

Abstract base class for an asynchronous communication port.

Definition at line 81 of file comm.h.

Constructor & Destructor Documentation

§ Comm()

async_comm::Comm::Comm ( MessageHandler message_handler = default_message_handler_)

Set up asynchronous communication base class.

Parameters
message_handlerCustom message handler, or omit for default handler

Definition at line 48 of file comm.cpp.

Member Function Documentation

§ init()

bool async_comm::Comm::init ( )

Initializes and opens the port.

Returns
True if the port was succesfully initialized

Definition at line 61 of file comm.cpp.

§ register_listener()

void async_comm::Comm::register_listener ( CommListener listener)

Register a listener for when bytes are received on the port.

The listener must inherit from CommListener and implement the receive_callback function. This is another mechanism for receiving data from the Comm interface without needing to create function pointers. Multiple listeners can be added and all will get the callback

Parameters
listenerReference to listener

Definition at line 115 of file comm.cpp.

§ register_receive_callback()

void async_comm::Comm::register_receive_callback ( std::function< void(const uint8_t *, size_t)>  fun)

Register a callback function for when bytes are received on the port.

The callback function needs to accept two parameters. The first is of type const uint8_t*, and is a constant pointer to the data buffer. The second is of type size_t, and specifies the number of bytes available in the buffer.

Warning
The data buffer passed to the callback function will be invalid after the callback function exits. If you want to store the data for later processing, you must copy the data to a new buffer rather than storing the pointer to the buffer.
Parameters
funFunction to call when bytes are received

Definition at line 110 of file comm.cpp.

§ send_byte()

void async_comm::Comm::send_byte ( uint8_t  data)
inline

Send a single byte over the port.

Parameters
dataByte to send

Definition at line 113 of file comm.h.

§ send_bytes()

void async_comm::Comm::send_bytes ( const uint8_t *  src,
size_t  len 
)

Send bytes from a buffer over the port.

Parameters
srcAddress of the buffer
lenNumber of bytes to send

Definition at line 97 of file comm.cpp.


The documentation for this class was generated from the following files: