libcyberradio  22.01.24
SerialPort Class Reference

Class that manages communications with a serial port. More...

#include <SerialPort.h>

Inheritance diagram for SerialPort:
Debuggable

Public Member Functions

 SerialPort (const std::string &device, int baudrate=115200, char parity='N', int databits=8, int stopbits=1, bool xonxoff=false, bool rtscts=false, bool debug=false)
 Constructs a SerialPort object. More...
 
 ~SerialPort ()
 Destroys a SerialPort object.
 
 SerialPort (const SerialPort &other)
 Copies a SerialPort object. More...
 
SerialPortoperator= (const SerialPort &other)
 Assignment operator for SerialPort objects. More...
 
bool open ()
 Open the serial port. More...
 
bool close ()
 Close the serial port. More...
 
std::string read ()
 Reads data from the serial port.
 
bool write (const std::string &data)
 Writes data to the serial port. More...
 
int getBaudRate () const
 Gets the current baud rate. More...
 
bool setBaudRate (int baudrate)
 Sets the current baud rate. More...
 
char getParity () const
 Gets the current parity setting. More...
 
bool setParity (char parity)
 Sets the current parity setting. More...
 
int getDataBits () const
 Gets the current number of data bits. More...
 
bool setDataBits (int databits)
 Sets the current number of data bits. More...
 
int getStopBits () const
 Gets the current number of stop bits. More...
 
bool setStopBits (int stopbits)
 Sets the current number of stop bits. More...
 
bool usesXonXoffFlowControl () const
 Gets whether the serial port currently uses XON/XOFF (software) flow control. More...
 
bool enableXonXoffFlowControl (bool enabled)
 Enables XON/XOFF (software) flow control on the serial port. More...
 
bool usesRtsCtsFlowControl () const
 Gets whether the serial port currently uses RTS/CTS (hardware) flow control. More...
 
bool enableRtsCtsFlowControl (bool enabled)
 Enables RTS/CTS (hardware) flow control on the serial port. More...
 
std::string getLastError () const
 Gets the last error message. More...
 
virtual void setDebugName (const std::string &debug_name)
 Sets the debug name for this object. More...
 
virtual void setDebugFile (FILE *debug_fp)
 Sets the debug file pointer for this object. More...
 
virtual void setDebugTimeFormat (const std::string &debug_timefmt)
 Sets the debug time format for this object. More...
 
virtual int debug (const char *format,...)
 Outputs debug information. More...
 
virtual const char * debugBool (bool x)
 Gets a debug output string for a Boolean value. More...
 
virtual bool isDebug () const
 Gets whether this object produces debug output. More...
 
virtual std::string getDebugName () const
 Gets the debug name for this object. More...
 
virtual std::string rawString (const std::string &data)
 Gets a "raw" string representation of a given data string. More...
 

Detailed Description

Class that manages communications with a serial port.

Definition at line 28 of file SerialPort.h.

Constructor & Destructor Documentation

◆ SerialPort() [1/2]

SerialPort ( const std::string &  device,
int  baudrate = 115200,
char  parity = 'N',
int  databits = 8,
int  stopbits = 1,
bool  xonxoff = false,
bool  rtscts = false,
bool  debug = false 
)

Constructs a SerialPort object.

Parameters
deviceSerial device name (such as "/dev/ttyS0")
baudrateBaud rate
parityParity setting. One of 'N' (none), 'E' (even), or 'O' (odd).
databitsNumber of data bits
stopbitsNumber of stop bits
xonxoffWhether the serial port uses XON/XOFF (software) flow control
rtsctsWhether the serial port uses RTS/CTS (hardware) flow control
debugWhether the transport emits debug output (Boolean).

Definition at line 25 of file SerialPort.cpp.

◆ SerialPort() [2/2]

SerialPort ( const SerialPort other)

Copies a SerialPort object.

Parameters
otherThe SerialPort object to copy.

Definition at line 63 of file SerialPort.cpp.

Member Function Documentation

◆ close()

bool close ( )

Close the serial port.

Returns
True on success, false on error. Use getLastError() to determine why the open failed.

Definition at line 212 of file SerialPort.cpp.

◆ debug()

int debug ( const char *  format,
  ... 
)
virtualinherited

Outputs debug information.

This method follows the same semantics as printf(). Output is preceded by a timestamp and the name of the object, if provided.

Parameters
formatThe printf()-style format string.
...Comma-separated list of arguments to print. Note that these need to be arguments that can be supported natively through printf().
Returns
The number of characters outputted.

Definition at line 95 of file Debuggable.cpp.

◆ debugBool()

const char * debugBool ( bool  x)
virtualinherited

Gets a debug output string for a Boolean value.

Parameters
xBoolean value
Returns
A constant string, either "true" or "false".

Definition at line 126 of file Debuggable.cpp.

◆ enableRtsCtsFlowControl()

bool enableRtsCtsFlowControl ( bool  enabled)

Enables RTS/CTS (hardware) flow control on the serial port.

Note
This method has meaning only if the port is open.
Parameters
enabledWhether or not flow control should be used.
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 625 of file SerialPort.cpp.

◆ enableXonXoffFlowControl()

bool enableXonXoffFlowControl ( bool  enabled)

Enables XON/XOFF (software) flow control on the serial port.

Note
This method has meaning only if the port is open.
Parameters
enabledWhether or not flow control should be used.
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 586 of file SerialPort.cpp.

◆ getBaudRate()

int getBaudRate ( ) const

Gets the current baud rate.

Note
This method has meaning only if the port is open.
Returns
The baud rate, in bytes per second.

Definition at line 303 of file SerialPort.cpp.

◆ getDataBits()

int getDataBits ( ) const

Gets the current number of data bits.

Note
This method has meaning only if the port is open.
Returns
The number of data bits (5, 6, 7, or 8).

Definition at line 481 of file SerialPort.cpp.

◆ getDebugName()

std::string getDebugName ( ) const
virtualinherited

Gets the debug name for this object.

Returns
The debug name, as a string.

Definition at line 138 of file Debuggable.cpp.

◆ getLastError()

std::string getLastError ( ) const

Gets the last error message.

Returns
The last error message (string).

Definition at line 659 of file SerialPort.cpp.

◆ getParity()

char getParity ( ) const

Gets the current parity setting.

Note
This method has meaning only if the port is open.
Returns
The parity setting. One of 'N' (none), 'E' (even), or 'O' (odd).

Definition at line 432 of file SerialPort.cpp.

◆ getStopBits()

int getStopBits ( ) const

Gets the current number of stop bits.

Note
This method has meaning only if the port is open.
Returns
The number of stop bits (1 or 2).

Definition at line 537 of file SerialPort.cpp.

◆ isDebug()

bool isDebug ( ) const
virtualinherited

Gets whether this object produces debug output.

Returns
True if producing debug, false otherwise.

Definition at line 133 of file Debuggable.cpp.

◆ open()

bool open ( )

Open the serial port.

Returns
True on success, false on error. Use getLastError() to determine why the open failed.

Definition at line 104 of file SerialPort.cpp.

◆ operator=()

SerialPort & operator= ( const SerialPort other)

Assignment operator for SerialPort objects.

Parameters
otherThe RadioTransport object to copy.
Returns
A reference to the assigned object.

Definition at line 81 of file SerialPort.cpp.

◆ rawString()

std::string rawString ( const std::string &  data)
virtualinherited

Gets a "raw" string representation of a given data string.

"Raw" string representations mimic Python string representations. Whitespace characters are denoted by backslash representations ("\\r", "\\n", "\\t", "\\v", "\\f"), while other non-printable characters are represented with hex representation ("\\x00", etc.)

Parameters
dataData string
Returns
The data's "raw" representation.

Definition at line 143 of file Debuggable.cpp.

◆ setBaudRate()

bool setBaudRate ( int  baudrate)

Sets the current baud rate.

Note
This method has meaning only if the port is open.
Parameters
baudrateThe baud rate, in bytes per second.
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 308 of file SerialPort.cpp.

◆ setDataBits()

bool setDataBits ( int  databits)

Sets the current number of data bits.

Note
This method has meaning only if the port is open.
Parameters
databitsThe number of data bits (5, 6, 7, or 8).
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 486 of file SerialPort.cpp.

◆ setDebugFile()

void setDebugFile ( FILE *  debug_fp)
virtualinherited

Sets the debug file pointer for this object.

Parameters
debug_fpFile to send debug output to.

Definition at line 81 of file Debuggable.cpp.

◆ setDebugName()

void setDebugName ( const std::string &  debug_name)
virtualinherited

Sets the debug name for this object.

Use this method to set unique debug names for objects of the same class for easy differentiation.

Parameters
debug_nameName for identifying this object in debug output.

Definition at line 74 of file Debuggable.cpp.

◆ setDebugTimeFormat()

void setDebugTimeFormat ( const std::string &  debug_timefmt)
virtualinherited

Sets the debug time format for this object.

Parameters
debug_timefmtFormat string for displaying timestamp, as compatible with strftime(). If this is an empty string, don't display a timestamp.

Definition at line 88 of file Debuggable.cpp.

◆ setParity()

bool setParity ( char  parity)

Sets the current parity setting.

Note
This method has meaning only if the port is open.
Parameters
parityThe parity setting. One of 'N' (none), 'E' (even), or 'O' (odd).
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 437 of file SerialPort.cpp.

◆ setStopBits()

bool setStopBits ( int  stopbits)

Sets the current number of stop bits.

Note
This method has meaning only if the port is open.
Parameters
stopbitsThe number of data bits (1 or 2).
Returns
True if successful, false otherwise. Use getLastError() to determine why the call failed.

Definition at line 542 of file SerialPort.cpp.

◆ usesRtsCtsFlowControl()

bool usesRtsCtsFlowControl ( ) const

Gets whether the serial port currently uses RTS/CTS (hardware) flow control.

Note
This method has meaning only if the port is open.
Returns
True if RTS/CTS flow control is in use, false otherwise.

Definition at line 620 of file SerialPort.cpp.

◆ usesXonXoffFlowControl()

bool usesXonXoffFlowControl ( ) const

Gets whether the serial port currently uses XON/XOFF (software) flow control.

Note
This method has meaning only if the port is open.
Returns
True if XON/XOFF flow control is in use, false otherwise.

Definition at line 581 of file SerialPort.cpp.

◆ write()

bool write ( const std::string &  data)

Writes data to the serial port.

Parameters
dataData to write.
Returns
True if data written successfully, false otherwise.

Definition at line 271 of file SerialPort.cpp.


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