SerialPort
A light-weight C++ library for cross-platform serial communication.
Loading...
Searching...
No Matches
serial_port::SerialPort Class Reference

A SerialPort class. More...

#include <serial_port.h>

Public Member Functions

 SerialPort ()
 Default constructor. Does not open any port.
 
 SerialPort (const Settings &settings)
 Create a port with specific settings (but do not open it)
 
 SerialPort (const std::string &port_name, int baud_rate, Parity parity=Parity::kNone, NumStopBits stop_bits=serial_port::NumStopBits::kOne, bool hardware_flow_control=false, unsigned long int timeout_s=0, unsigned long int timeout_ms=0)
 Create a port with specific settings (but do not open it)
 
 ~SerialPort ()=default
 Default destructor. Port will be closed.
 
 SerialPort (SerialPort &&)=default
 SerialPort objects may be moved.
 
SerialPortoperator= (SerialPort &&other) noexcept
 
 SerialPort (const SerialPort &)=delete
 SerialPort objects may not be copied.
 
SerialPortoperator= (const SerialPort &other)=delete
 
void Open () const
 Open the port with the current settings. If a port was opened through this object previously, it will be closed first.
 
void Close () const
 Close the port.
 
bool IsOpen () const
 Returns whether or not the port is currently open.
 
const SettingsGetSettings () const
 Get the currently defined settings.
 
unsigned long NumBytesAvailable () const
 Return the number of bytes available in the RX buffer.
 
void FlushBuffer () const
 Flush the RX and TX buffers.
 
unsigned long ReadData (char *data, unsigned long num_bytes) const
 Read data from the port.
 
std::string ReadString () const
 Read a string from the port terminated with a '\n' symbol.
 
unsigned long WriteData (const char *data, unsigned long num_bytes) const
 Write data to the port.
 
unsigned long WriteString (const std::string &str) const
 Write a string to the port.
 

Static Public Member Functions

static std::vector< PortInfoEnumeratePorts ()
 A static function to enumerate available ports.
 

Friends

std::ostream & operator<< (std::ostream &os, const SerialPort &obj)
 Overloaded stream output operator to print the port settings.
 

Detailed Description

A SerialPort class.

This class represents a serial port on the machine and handles opening, writing, reading, and closing a port.

Constructor & Destructor Documentation

◆ SerialPort() [1/2]

serial_port::SerialPort::SerialPort ( const Settings & settings)
explicit

Create a port with specific settings (but do not open it)

Parameters
settingsA Settings object

◆ SerialPort() [2/2]

serial_port::SerialPort::SerialPort ( const std::string & port_name,
int baud_rate,
Parity parity = Parity::kNone,
NumStopBits stop_bits = serial_port::NumStopBits::kOne,
bool hardware_flow_control = false,
unsigned long int timeout_s = 0,
unsigned long int timeout_ms = 0 )

Create a port with specific settings (but do not open it)

Parameters
port_nameName of the port (e.g. "COM1" on Windows or "/dev/ttyS0" on Linux)
baud_rateA baud rate. Any value will be accepted here. It will be checked for validity when the port is actually opened.
parityThe parity setting for the port.
stop_bitsThe number of stop bits being used.
hardware_flow_controlWhether or not to use hardware flow control.
timeout_sTimeout value in seconds (currently not used!)
timeout_msTimeout value in milliseconds (currently not used!)

Member Function Documentation

◆ EnumeratePorts()

std::vector< serial_port::PortInfo > serial_port::SerialPort::EnumeratePorts ( )
static

A static function to enumerate available ports.

Returns
A list of PortInfo objects describing the available ports

◆ ReadData()

unsigned long serial_port::SerialPort::ReadData ( char * data,
unsigned long num_bytes ) const

Read data from the port.

Parameters
dataA pointer to a char array. Must be at least num_bytes elements long!
num_bytesThe number of bytes to attempt to read from the port
Returns
The actual number of bytes read

◆ WriteData()

unsigned long serial_port::SerialPort::WriteData ( const char * data,
unsigned long num_bytes ) const

Write data to the port.

Parameters
dataAn array of bytes to write
num_bytesthe number of bytes in the array
Returns
The number of bytes actually written

◆ WriteString()

unsigned long serial_port::SerialPort::WriteString ( const std::string & str) const

Write a string to the port.

Parameters
strA string terminated by a '\n' symbol
Returns
The number of bytes actually written

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