mavtables  0.2.1
MAVLink router and firewall.
Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
SerialPort Class Reference

#include <SerialPort.hpp>

Inheritance diagram for SerialPort:
Inheritance graph
Collaboration diagram for SerialPort:
Collaboration graph

Public Types

enum  Parity {
  NONE, ODD, EVEN, MARK,
  SPACE
}
 
enum  Feature { DEFAULT = 0, FLOW_CONTROL = 1 << 0 }
 

Public Member Functions

virtual ~SerialPort ()
 
virtual std::vector< uint8_t > read (const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
 
virtual void read (std::back_insert_iterator< std::vector< uint8_t >> it, const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
 
virtual void write (const std::vector< uint8_t > &data)
 
virtual void write (std::vector< uint8_t >::const_iterator first, std::vector< uint8_t >::const_iterator last)
 

Protected Member Functions

virtual std::ostream & print_ (std::ostream &os) const
 

Friends

std::ostream & operator<< (std::ostream &os, const SerialPort &serial_port)
 

Detailed Description

The base class of all serial port classes.

This provides an abstraction of serial ports across operating systems.

Warning
This class should be treated as pure virtual and should never be instantiated.
Either read(const std::chrono::nanoseconds &) or read(std::back_insert_iterator<std::vector<uint8_t>>,const std::chrono::nanoseconds &) must be overridden in child classes to avoid infinite recursion.
Either write(const std::vector<uint8_t> &data) or write(std::vector<uint8_t>::const_iterator,std::vector<uint8_t>::const_iterator) must be overridden in child classes to avoid infinite recursion.

Definition at line 44 of file SerialPort.hpp.

Member Enumeration Documentation

◆ Feature

Feature bitflags.

Enumerator
DEFAULT 

No special features.

FLOW_CONTROL 

Enable flow control.

Definition at line 59 of file SerialPort.hpp.

◆ Parity

Parity options.

Enumerator
NONE 

No parity.

ODD 

Odd parity, must have odd number of set bits.

EVEN 

Even parity, must have even number of set bits.

MARK 

Fill parity bit with 1.

SPACE 

Fill parity bit with 0.

Definition at line 49 of file SerialPort.hpp.

Constructor & Destructor Documentation

◆ ~SerialPort()

SerialPort::~SerialPort ( )
virtual

Definition at line 31 of file SerialPort.cpp.

Member Function Documentation

◆ print_()

std::ostream & SerialPort::print_ ( std::ostream &  os) const
protectedvirtual

Print the serial port to the given output stream.

Parameters
osThe output stream to print to.
Returns
The output stream.

Reimplemented in UnixSerialPort.

Definition at line 104 of file SerialPort.cpp.

◆ read() [1/2]

std::vector< uint8_t > SerialPort::read ( const std::chrono::nanoseconds &  timeout = std::chrono::nanoseconds::zero())
virtual

Read data from the serial port.

Note
The timeout is not guaranteed to be up to nanosecond precision, the actual precision is up to the operating system's implementation but is guaranteed to have at least millisecond precision.
Parameters
timeoutHow long to wait for data to arrive on the serial port if there is not already data to read. The default is to not wait.
Returns
The data read from the serial port.

Reimplemented in UnixSerialPort.

Definition at line 47 of file SerialPort.cpp.

Here is the caller graph for this function:

◆ read() [2/2]

void SerialPort::read ( std::back_insert_iterator< std::vector< uint8_t >>  it,
const std::chrono::nanoseconds &  timeout = std::chrono::nanoseconds::zero() 
)
virtual

Read data from the serial port.

Note
The timeout is not guaranteed to be up to nanosecond precision, the actual precision is up to the operating system's implementation but is guaranteed to have at least millisecond precision.
Parameters
itA back insert iterator to read bytes into.
timeoutHow long to wait for data to arrive on the serial port if there is not already data to read. The default is to not wait.

Definition at line 65 of file SerialPort.cpp.

References read().

Here is the call graph for this function:

◆ write() [1/2]

void SerialPort::write ( const std::vector< uint8_t > &  data)
virtual

Write data to the serial port (blocking write).

Parameters
dataThe bytes to send.

Reimplemented in UnixSerialPort.

Definition at line 78 of file SerialPort.cpp.

Here is the caller graph for this function:

◆ write() [2/2]

void SerialPort::write ( std::vector< uint8_t >::const_iterator  first,
std::vector< uint8_t >::const_iterator  last 
)
virtual

Write data to the serial port (blocking write).

Parameters
firstIterator to first byte in range to send.
lastIterator to one past the last byte to send.

Definition at line 89 of file SerialPort.cpp.

References write().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const SerialPort serial_port 
)
friend

Print the given serial port to the given output stream.

Note
This is a polymorphic print, it will work on any child of SerialPort even if the pointer/reference is to the base class SerialPort.

An example:

serial {
device /dev/ttyUSB0;
baudrate 115200;
flow_control yes;
}

The base SerialPort class will print:

unknown serial port
Parameters
osThe output stream to print to.
serial_portThe serial port (or any child of SerialPort) to print.
Returns
The output stream.

Definition at line 137 of file SerialPort.cpp.


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