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

#include <UnixSerialPort.hpp>

Inheritance diagram for UnixSerialPort:
Inheritance graph
Collaboration diagram for UnixSerialPort:
Collaboration graph

Public Types

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

Public Member Functions

 UnixSerialPort (std::string device, unsigned long baud_rate=9600, SerialPort::Feature features=SerialPort::DEFAULT, std::unique_ptr< UnixSyscalls > syscalls=std::make_unique< UnixSyscalls >())
 
virtual ~UnixSerialPort ()
 
virtual std::vector< uint8_t > read (const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero()) final
 
virtual void write (const std::vector< uint8_t > &data) final
 
virtual void read (std::back_insert_iterator< std::vector< uint8_t >> it, const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
 
virtual void write (std::vector< uint8_t >::const_iterator first, std::vector< uint8_t >::const_iterator last)
 

Protected Member Functions

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

Detailed Description

A unix serial port.

Definition at line 34 of file UnixSerialPort.hpp.

Member Enumeration Documentation

◆ Feature

enum SerialPort::Feature
inherited

Feature bitflags.

Enumerator
DEFAULT 

No special features.

FLOW_CONTROL 

Enable flow control.

Definition at line 59 of file SerialPort.hpp.

◆ Parity

enum SerialPort::Parity
inherited

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

◆ UnixSerialPort()

UnixSerialPort::UnixSerialPort ( std::string  device,
unsigned long  baud_rate = 9600,
SerialPort::Feature  features = SerialPort::DEFAULT,
std::unique_ptr< UnixSyscalls syscalls = std::make_unique<UnixSyscalls>() 
)

Construct a serial port.

Parameters
deviceThe string representing the serial port. For example "/dev/ttyUSB0".
baud_rateThe baud rate in bits per second, the default value is 9600 bps.
featuresA bitflag of the features to enable, default is to not enable any features. See SerialPort::Feature for flags.
syscallsThe object to use for unix system calls. It is default constructed to the production implementation. This argument is only used for testing.
Exceptions
std::invalid_argumentif the baud rate is not supported.
std::system_errorif a system call produces an error.

Definition at line 47 of file UnixSerialPort.cpp.

◆ ~UnixSerialPort()

UnixSerialPort::~UnixSerialPort ( )
virtual

The serial port destructor.

Closes the underlying file descriptor of the serial port device.

Definition at line 64 of file UnixSerialPort.cpp.

Member Function Documentation

◆ print_()

std::ostream & UnixSerialPort::print_ ( std::ostream &  os) const
finalprotectedvirtual

Print the serial port to the given output stream.

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

Example:

serial {
device /dev/ttyUSB0;
baudrate 115200;
flow_control yes;
}
Parameters
osThe output stream to print to.

Reimplemented from SerialPort.

Definition at line 360 of file UnixSerialPort.cpp.

References SerialPort::FLOW_CONTROL.

◆ read() [1/2]

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

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.
Note
The timeout precision of this implementation is 1 millisecond.
Exceptions
std::system_errorif a system call produces an error.

Reimplemented from SerialPort.

Definition at line 77 of file UnixSerialPort.cpp.

◆ read() [2/2]

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

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 SerialPort::read().

Here is the call graph for this function:

◆ write() [1/2]

void UnixSerialPort::write ( const std::vector< uint8_t > &  data)
finalvirtual

Write data to the serial port (blocking write).

Parameters
dataThe bytes to send.
Exceptions
std::system_errorif a system call produces an error.
PartialSendErrorif it fails to write all the data it is given.

Reimplemented from SerialPort.

Definition at line 118 of file UnixSerialPort.cpp.

◆ write() [2/2]

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

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 SerialPort::write().

Here is the call graph for this function:

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