LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
LibSerial::SerialPort Class Reference

SerialPort allows an object oriented approach to serial port communication. A serial port object can be created to allow opening the port with specified modes and settings. The SerialPort class also provides Get/Set methods to access the most commonly utilized parameters associated with serial port communication. More...

#include <SerialPort.h>

Classes

class  Implementation
 SerialPort::Implementation is the SerialPort implementation class. More...
 

Public Member Functions

 SerialPort ()
 Default Constructor.
 
 SerialPort (const std::string &fileName, const BaudRate &baudRate=BaudRate::BAUD_DEFAULT, const CharacterSize &characterSize=CharacterSize::CHAR_SIZE_DEFAULT, const FlowControl &flowControlType=FlowControl::FLOW_CONTROL_DEFAULT, const Parity &parityType=Parity::PARITY_DEFAULT, const StopBits &stopBits=StopBits::STOP_BITS_DEFAULT, bool exclusive=true)
 Constructor that allows a SerialPort instance to be created and opened, initializing the corresponding serial port with the specified parameters.
 
virtual ~SerialPort () noexcept
 Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if open.
 
 SerialPort (const SerialPort &otherSerialPort)=delete
 Copy construction is disallowed.
 
 SerialPort (SerialPort &&otherSerialPort)
 Move construction is allowed.
 
SerialPortoperator= (const SerialPort &otherSerialPort)=delete
 Copy assignment is disallowed.
 
SerialPortoperator= (SerialPort &&otherSerialPort)
 Move assignment is allowed.
 
void Open (const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
 Opens the serial port associated with the specified file name and the specified mode.
 
void Close ()
 Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port.
 
void DrainWriteBuffer ()
 Waits until the write buffer is drained and then returns.
 
void FlushInputBuffer ()
 Flushes the serial port input buffer.
 
void FlushOutputBuffer ()
 Flushes the serial port output buffer.
 
void FlushIOBuffers ()
 Flushes the serial port input and output buffers.
 
bool IsDataAvailable ()
 Checks if data is available at the input of the serial port.
 
bool IsOpen () const
 Determines if the serial port is open for I/O.
 
void SetDefaultSerialPortParameters ()
 Sets all serial port paramters to their default values.
 
void SetBaudRate (const BaudRate &baudRate)
 Sets the baud rate for the serial port to the specified value.
 
BaudRate GetBaudRate () const
 Gets the current baud rate for the serial port.
 
void SetCharacterSize (const CharacterSize &characterSize)
 Sets the character size for the serial port.
 
CharacterSize GetCharacterSize () const
 Gets the character size being used for serial communication.
 
void SetFlowControl (const FlowControl &flowControlType)
 Sets flow control for the serial port.
 
FlowControl GetFlowControl () const
 Gets the current flow control setting.
 
void SetParity (const Parity &parityType)
 Sets the parity type for the serial port.
 
Parity GetParity () const
 Gets the parity type for the serial port.
 
void SetStopBits (const StopBits &stopBits)
 Sets the number of stop bits to be used with the serial port.
 
StopBits GetStopBits () const
 Gets the number of stop bits currently being used by the serial.
 
void SetVMin (const short vmin)
 Sets the minimum number of characters for non-canonical reads.
 
short GetVMin () const
 Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.
 
void SetVTime (const short vtime)
 Sets character buffer timeout for non-canonical reads in deciseconds.
 
short GetVTime () const
 Gets the current timeout value for non-canonical reads in deciseconds.
 
void SetDTR (const bool dtrState=true)
 Sets the DTR line to the specified value.
 
bool GetDTR () const
 Gets the status of the DTR line.
 
void SetRTS (const bool rtsState=true)
 Set the RTS line to the specified value.
 
bool GetRTS () const
 Get the status of the RTS line.
 
bool GetCTS ()
 Get the status of the CTS line.
 
bool GetDSR ()
 Get the status of the DSR line.
 
int GetFileDescriptor () const
 Gets the serial port file descriptor.
 
int GetNumberOfBytesAvailable ()
 Gets the number of bytes available in the read buffer.
 
void Read (DataBuffer &dataBuffer, size_t numberOfBytes=0, size_t msTimeout=0)
 Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataBuffer on return from this method.
 
void Read (std::string &dataString, size_t numberOfBytes=0, size_t msTimeout=0)
 Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataString on return from this method.
 
void ReadByte (char &charBuffer, size_t msTimeout=0)
 Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.
 
void ReadByte (unsigned char &charBuffer, size_t msTimeout=0)
 Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.
 
void ReadLine (std::string &dataString, char lineTerminator='\n', size_t msTimeout=0)
 Reads a line of characters from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is 0, then this method will block until a line terminator is received. In all cases, any data received remains available in the string on return from this method.
 
void Write (const DataBuffer &dataBuffer)
 Writes a DataBuffer to the serial port.
 
void Write (const std::string &dataString)
 Writes a std::string to the serial port.
 
void WriteByte (char charbuffer)
 Writes a single byte to the serial port.
 
void WriteByte (unsigned char charbuffer)
 Writes a single byte to the serial port.
 
void SetSerialPortBlockingStatus (bool blockingStatus)
 Sets the current state of the serial port blocking status.
 
bool GetSerialPortBlockingStatus () const
 Gets the current state of the serial port blocking status.
 
void SetModemControlLine (int modemLine, bool lineState)
 Set the specified modem control line to the specified value.
 
bool GetModemControlLine (int modemLine)
 Get the current state of the specified modem control line.
 

Detailed Description

SerialPort allows an object oriented approach to serial port communication. A serial port object can be created to allow opening the port with specified modes and settings. The SerialPort class also provides Get/Set methods to access the most commonly utilized parameters associated with serial port communication.

Examples
main_page_example.cpp, serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 55 of file SerialPort.h.

Constructor & Destructor Documentation

◆ SerialPort() [1/4]

LibSerial::SerialPort::SerialPort ( )
explicit

Default Constructor.

Definition at line 487 of file SerialPort.cpp.

◆ SerialPort() [2/4]

LibSerial::SerialPort::SerialPort ( const std::string &  fileName,
const BaudRate &  baudRate = BaudRate::BAUD_DEFAULT,
const CharacterSize &  characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
const FlowControl &  flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
const Parity &  parityType = Parity::PARITY_DEFAULT,
const StopBits &  stopBits = StopBits::STOP_BITS_DEFAULT,
bool  exclusive = true 
)
explicit

Constructor that allows a SerialPort instance to be created and opened, initializing the corresponding serial port with the specified parameters.

Parameters
fileNameThe file name of the serial port.
baudRateThe communications baud rate.
characterSizeThe size of the character buffer for storing read/write streams.
parityTypeThe parity type for the serial port.
stopBitsThe number of stop bits for the serial port.
flowControlTypeThe flow control type for the serial port.
exclusiveSet exclusive access for the serial port.

Definition at line 493 of file SerialPort.cpp.

◆ ~SerialPort()

LibSerial::SerialPort::~SerialPort ( )
virtualdefaultnoexcept

Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if open.

◆ SerialPort() [3/4]

LibSerial::SerialPort::SerialPort ( const SerialPort otherSerialPort)
delete

Copy construction is disallowed.

◆ SerialPort() [4/4]

LibSerial::SerialPort::SerialPort ( SerialPort &&  otherSerialPort)

Move construction is allowed.

Definition at line 511 of file SerialPort.cpp.

Member Function Documentation

◆ Close()

void LibSerial::SerialPort::Close ( )

Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed on the serial port.

Examples
serial_port_read_write.cpp.

Definition at line 536 of file SerialPort.cpp.

◆ DrainWriteBuffer()

void LibSerial::SerialPort::DrainWriteBuffer ( )

Waits until the write buffer is drained and then returns.

Examples
serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 542 of file SerialPort.cpp.

◆ FlushInputBuffer()

void LibSerial::SerialPort::FlushInputBuffer ( )

Flushes the serial port input buffer.

Definition at line 548 of file SerialPort.cpp.

◆ FlushIOBuffers()

void LibSerial::SerialPort::FlushIOBuffers ( )

Flushes the serial port input and output buffers.

Definition at line 560 of file SerialPort.cpp.

◆ FlushOutputBuffer()

void LibSerial::SerialPort::FlushOutputBuffer ( )

Flushes the serial port output buffer.

Definition at line 554 of file SerialPort.cpp.

◆ GetBaudRate()

BaudRate LibSerial::SerialPort::GetBaudRate ( ) const

Gets the current baud rate for the serial port.

Returns
Returns the baud rate.

Definition at line 590 of file SerialPort.cpp.

◆ GetCharacterSize()

CharacterSize LibSerial::SerialPort::GetCharacterSize ( ) const

Gets the character size being used for serial communication.

Returns
Returns the current character size.

Definition at line 602 of file SerialPort.cpp.

◆ GetCTS()

bool LibSerial::SerialPort::GetCTS ( )

Get the status of the CTS line.

Returns
Returns true iff the status of the CTS line is high.

Definition at line 692 of file SerialPort.cpp.

◆ GetDSR()

bool LibSerial::SerialPort::GetDSR ( )

Get the status of the DSR line.

Returns
Returns true iff the status of the DSR line is high.

Definition at line 698 of file SerialPort.cpp.

◆ GetDTR()

bool LibSerial::SerialPort::GetDTR ( ) const

Gets the status of the DTR line.

Returns
Returns true iff the status of the DTR line is high.

Definition at line 674 of file SerialPort.cpp.

◆ GetFileDescriptor()

int LibSerial::SerialPort::GetFileDescriptor ( ) const

Gets the serial port file descriptor.

Returns
Returns the serial port file descriptor.

Definition at line 704 of file SerialPort.cpp.

◆ GetFlowControl()

FlowControl LibSerial::SerialPort::GetFlowControl ( ) const

Gets the current flow control setting.

Returns
Returns the flow control type of the serial port.

Definition at line 614 of file SerialPort.cpp.

◆ GetModemControlLine()

bool LibSerial::SerialPort::GetModemControlLine ( int  modemLine)

Get the current state of the specified modem control line.

Parameters
modemLineOne of the following four values: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, or TIOCM_DSR.
Returns
True if the specified line is currently set and false otherwise.

Definition at line 813 of file SerialPort.cpp.

◆ GetNumberOfBytesAvailable()

int LibSerial::SerialPort::GetNumberOfBytesAvailable ( )

Gets the number of bytes available in the read buffer.

Returns
Returns the number of bytes avilable in the read buffer.

Definition at line 710 of file SerialPort.cpp.

◆ GetParity()

Parity LibSerial::SerialPort::GetParity ( ) const

Gets the parity type for the serial port.

Returns
Returns the parity type.

Definition at line 626 of file SerialPort.cpp.

◆ GetRTS()

bool LibSerial::SerialPort::GetRTS ( ) const

Get the status of the RTS line.

Returns
Returns true iff the status of the RTS line is high.

Definition at line 686 of file SerialPort.cpp.

◆ GetSerialPortBlockingStatus()

bool LibSerial::SerialPort::GetSerialPortBlockingStatus ( ) const

Gets the current state of the serial port blocking status.

Returns
True if port is blocking, false if port non-blocking.

Definition at line 800 of file SerialPort.cpp.

◆ GetStopBits()

StopBits LibSerial::SerialPort::GetStopBits ( ) const

Gets the number of stop bits currently being used by the serial.

Returns
Returns the number of stop bits.

Definition at line 638 of file SerialPort.cpp.

◆ GetVMin()

short LibSerial::SerialPort::GetVMin ( ) const

Gets the VMIN value for the device, which represents the minimum number of characters for non-canonical reads.

Returns
Returns the minimum number of characters for non-canonical reads.

Definition at line 650 of file SerialPort.cpp.

◆ GetVTime()

short LibSerial::SerialPort::GetVTime ( ) const

Gets the current timeout value for non-canonical reads in deciseconds.

Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 662 of file SerialPort.cpp.

◆ IsDataAvailable()

bool LibSerial::SerialPort::IsDataAvailable ( )

Checks if data is available at the input of the serial port.

Returns
Returns true iff data is available to read.
Examples
serial_port_read.cpp.

Definition at line 566 of file SerialPort.cpp.

◆ IsOpen()

bool LibSerial::SerialPort::IsOpen ( ) const

Determines if the serial port is open for I/O.

Returns
Returns true iff the serial port is open.

Definition at line 572 of file SerialPort.cpp.

◆ Open()

void LibSerial::SerialPort::Open ( const std::string &  fileName,
const std::ios_base::openmode &  openMode = std::ios_base::in | std::ios_base::out,
bool  exclusive = true 
)

Opens the serial port associated with the specified file name and the specified mode.

Parameters
fileNameThe file name of the serial port.
openModeThe communication mode status when the serial communication port is opened.
exclusiveSet exclusive access for the serial port.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 526 of file SerialPort.cpp.

◆ operator=() [1/2]

SerialPort & LibSerial::SerialPort::operator= ( const SerialPort otherSerialPort)
delete

Copy assignment is disallowed.

◆ operator=() [2/2]

SerialPort & LibSerial::SerialPort::operator= ( SerialPort &&  otherSerialPort)

Move assignment is allowed.

Definition at line 517 of file SerialPort.cpp.

◆ Read() [1/2]

void LibSerial::SerialPort::Read ( DataBuffer &  dataBuffer,
size_t  numberOfBytes = 0,
size_t  msTimeout = 0 
)

Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataBuffer on return from this method.

Parameters
dataBufferThe data buffer to place data into.
numberOfBytesThe number of bytes to read before returning.
msTimeoutThe timeout period in milliseconds.
Examples
serial_port_read.cpp, and serial_port_read_write.cpp.

Definition at line 724 of file SerialPort.cpp.

◆ Read() [2/2]

void LibSerial::SerialPort::Read ( std::string &  dataString,
size_t  numberOfBytes = 0,
size_t  msTimeout = 0 
)

Reads the specified number of bytes from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is zero, then the method will block until all requested bytes are received. If numberOfBytes is zero and msTimeout is non-zero, the method will continue receiving data for the specified of milliseconds. If numberOfBytes is zero and msTimeout is zero, the method will return immediately. In all cases, any data received remains available in the dataString on return from this method.

Parameters
dataStringThe string to place data into.
numberOfBytesThe number of bytes to read before returning.
msTimeoutThe timeout period in milliseconds.

Definition at line 734 of file SerialPort.cpp.

◆ ReadByte() [1/2]

void LibSerial::SerialPort::ReadByte ( char &  charBuffer,
size_t  msTimeout = 0 
)

Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.

Parameters
charBufferThe character read from the serial port.
msTimeoutThe timeout period in milliseconds.
Examples
serial_port_read.cpp, and serial_port_read_write.cpp.

Definition at line 744 of file SerialPort.cpp.

◆ ReadByte() [2/2]

void LibSerial::SerialPort::ReadByte ( unsigned char &  charBuffer,
size_t  msTimeout = 0 
)

Reads a single byte from the serial port. If no data is available within the specified number of milliseconds, (msTimeout), then this method will throw a ReadTimeout exception. If msTimeout is zero, then this method will block until data becomes available.

Parameters
charBufferThe character read from the serial port.
msTimeoutThe timeout period in milliseconds.

Definition at line 752 of file SerialPort.cpp.

◆ ReadLine()

void LibSerial::SerialPort::ReadLine ( std::string &  dataString,
char  lineTerminator = '\n',
size_t  msTimeout = 0 
)

Reads a line of characters from the serial port. The method will timeout if no data is received in the specified number of milliseconds (msTimeout). If msTimeout is 0, then this method will block until a line terminator is received. In all cases, any data received remains available in the string on return from this method.

Parameters
dataStringThe data string read from the serial port.
lineTerminatorThe line termination character to specify the end of a line.
msTimeoutThe timeout value to return if a line termination character is not read.
Examples
serial_port_read_write.cpp.

Definition at line 760 of file SerialPort.cpp.

◆ SetBaudRate()

void LibSerial::SerialPort::SetBaudRate ( const BaudRate &  baudRate)

Sets the baud rate for the serial port to the specified value.

Parameters
baudRateThe baud rate to be set for the serial port.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 584 of file SerialPort.cpp.

◆ SetCharacterSize()

void LibSerial::SerialPort::SetCharacterSize ( const CharacterSize &  characterSize)

Sets the character size for the serial port.

Parameters
characterSizeThe character size to be set.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 596 of file SerialPort.cpp.

◆ SetDefaultSerialPortParameters()

void LibSerial::SerialPort::SetDefaultSerialPortParameters ( )

Sets all serial port paramters to their default values.

Definition at line 578 of file SerialPort.cpp.

◆ SetDTR()

void LibSerial::SerialPort::SetDTR ( const bool  dtrState = true)

Sets the DTR line to the specified value.

Parameters
dtrStateThe line voltage state to be set, (true = high, false = low).

Definition at line 668 of file SerialPort.cpp.

◆ SetFlowControl()

void LibSerial::SerialPort::SetFlowControl ( const FlowControl &  flowControlType)

Sets flow control for the serial port.

Parameters
flowControlTypeThe flow control type to be set.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 608 of file SerialPort.cpp.

◆ SetModemControlLine()

void LibSerial::SerialPort::SetModemControlLine ( int  modemLine,
bool  lineState 
)

Set the specified modem control line to the specified value.

Parameters
modemLineOne of the following four values: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, or TIOCM_DSR.
lineStateState of the modem line after successful call to this method.

Definition at line 806 of file SerialPort.cpp.

◆ SetParity()

void LibSerial::SerialPort::SetParity ( const Parity &  parityType)

Sets the parity type for the serial port.

Parameters
parityTypeThe parity type to be set.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 620 of file SerialPort.cpp.

◆ SetRTS()

void LibSerial::SerialPort::SetRTS ( const bool  rtsState = true)

Set the RTS line to the specified value.

Parameters
rtsStateThe line voltage state to be set, (true = high, false = low).

Definition at line 680 of file SerialPort.cpp.

◆ SetSerialPortBlockingStatus()

void LibSerial::SerialPort::SetSerialPortBlockingStatus ( bool  blockingStatus)

Sets the current state of the serial port blocking status.

Parameters
blockingStatusThe serial port blocking status to be set, true if to be set blocking, false if to be set non-blocking.

Definition at line 794 of file SerialPort.cpp.

◆ SetStopBits()

void LibSerial::SerialPort::SetStopBits ( const StopBits &  stopBits)

Sets the number of stop bits to be used with the serial port.

Parameters
stopBitsThe number of stop bits to set.
Examples
serial_port_read.cpp, serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 632 of file SerialPort.cpp.

◆ SetVMin()

void LibSerial::SerialPort::SetVMin ( const short  vmin)

Sets the minimum number of characters for non-canonical reads.

Note
See VMIN in man termios(3).
Parameters
vminthe number of minimum characters to be set.

Definition at line 644 of file SerialPort.cpp.

◆ SetVTime()

void LibSerial::SerialPort::SetVTime ( const short  vtime)

Sets character buffer timeout for non-canonical reads in deciseconds.

Parameters
vtimeThe timeout value in deciseconds to be set.
Returns
Returns the character buffer timeout for non-canonical reads in deciseconds.

Definition at line 656 of file SerialPort.cpp.

◆ Write() [1/2]

void LibSerial::SerialPort::Write ( const DataBuffer &  dataBuffer)

Writes a DataBuffer to the serial port.

Parameters
dataBufferThe DataBuffer to write to the serial port.
Examples
serial_port_read_write.cpp.

Definition at line 770 of file SerialPort.cpp.

◆ Write() [2/2]

void LibSerial::SerialPort::Write ( const std::string &  dataString)

Writes a std::string to the serial port.

Parameters
dataStringThe data string to write to the serial port.

Definition at line 776 of file SerialPort.cpp.

◆ WriteByte() [1/2]

void LibSerial::SerialPort::WriteByte ( char  charbuffer)

Writes a single byte to the serial port.

Parameters
charbufferThe byte to write to the serial port.
Examples
serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 782 of file SerialPort.cpp.

◆ WriteByte() [2/2]

void LibSerial::SerialPort::WriteByte ( unsigned char  charbuffer)

Writes a single byte to the serial port.

Parameters
charbufferThe byte to write to the serial port.

Definition at line 788 of file SerialPort.cpp.


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