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)
| 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.
SerialPort & | operator= (const SerialPort &otherSerialPort)=delete
| Copy assignment is disallowed.
SerialPort & | operator= (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)
| 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 477 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`
| ) |  |

explicit

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

Parameters

  fileName | The file name of the serial port.
  baudRate | The communications baud rate.
  characterSize | The size of the character buffer for storing read/write streams.
  parityType | The parity type for the serial port.
  stopBits | The number of stop bits for the serial port.
  flowControlType | The flow control type for the serial port.

Definition at line 483 of file SerialPort.cpp.

## ~SerialPort()

LibSerial::SerialPort::~SerialPort | ( |  | ) |

virtual default noexcept

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 499 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 522 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 528 of file SerialPort.cpp.

## FlushInputBuffer()

void LibSerial::SerialPort::FlushInputBuffer | ( |  | ) |

Flushes the serial port input buffer.

Definition at line 534 of file SerialPort.cpp.

## FlushIOBuffers()

void LibSerial::SerialPort::FlushIOBuffers | ( |  | ) |

Flushes the serial port input and output buffers.

Definition at line 546 of file SerialPort.cpp.

## FlushOutputBuffer()

void LibSerial::SerialPort::FlushOutputBuffer | ( |  | ) |

Flushes the serial port output buffer.

Definition at line 540 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 576 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 588 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 678 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 684 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 660 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 690 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 600 of file SerialPort.cpp.

## GetModemControlLine()

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

Get the current state of the specified modem control line.

Parameters

  modemLine | One 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 799 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 696 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 612 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 672 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 786 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 624 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 636 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 648 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 552 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 558 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`
| ) |  |

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

Parameters

  fileName | The file name of the serial port.
  openMode | The communication mode status when the serial communication port is opened.

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

Definition at line 514 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 505 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

  dataBuffer | The data buffer to place data into.
  numberOfBytes | The number of bytes to read before returning.
  msTimeout | The timeout period in milliseconds.

Examples
  serial_port_read.cpp, and serial_port_read_write.cpp.

Definition at line 710 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

  dataString | The string to place data into.
  numberOfBytes | The number of bytes to read before returning.
  msTimeout | The timeout period in milliseconds.

Definition at line 720 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

  charBuffer | The character read from the serial port.
  msTimeout | The timeout period in milliseconds.

Examples
  serial_port_read.cpp, and serial_port_read_write.cpp.

Definition at line 730 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

  charBuffer | The character read from the serial port.
  msTimeout | The timeout period in milliseconds.

Definition at line 738 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

  dataString | The data string read from the serial port.
  lineTerminator | The line termination character to specify the end of a line.
  msTimeout | The timeout value to return if a line termination character is not read.

Examples
  serial_port_read_write.cpp.

Definition at line 746 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

  baudRate | The 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 570 of file SerialPort.cpp.

## SetCharacterSize()

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

Sets the character size for the serial port.

Parameters

  characterSize | The character size to be set.

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

Definition at line 582 of file SerialPort.cpp.

## SetDefaultSerialPortParameters()

void LibSerial::SerialPort::SetDefaultSerialPortParameters | ( |  | ) |

Sets all serial port paramters to their default values.

Definition at line 564 of file SerialPort.cpp.

## SetDTR()

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

Sets the DTR line to the specified value.

Parameters

  dtrState | The line voltage state to be set, (true = high, false = low).

Definition at line 654 of file SerialPort.cpp.

## SetFlowControl()

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

Sets flow control for the serial port.

Parameters

  flowControlType | The flow control type to be set.

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

Definition at line 594 of file SerialPort.cpp.

## SetModemControlLine()

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

Set the specified modem control line to the specified value.

Parameters

  modemLine | One of the following four values: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, or TIOCM_DSR.
  lineState | State of the modem line after successful call to this method.

Definition at line 792 of file SerialPort.cpp.

## SetParity()

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

Sets the parity type for the serial port.

Parameters

  parityType | The parity type to be set.

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

Definition at line 606 of file SerialPort.cpp.

## SetRTS()

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

Set the RTS line to the specified value.

Parameters

  rtsState | The line voltage state to be set, (true = high, false = low).

Definition at line 666 of file SerialPort.cpp.

## SetSerialPortBlockingStatus()

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

Sets the current state of the serial port blocking status.

Parameters

  blockingStatus | The serial port blocking status to be set, true if to be set blocking, false if to be set non-blocking.

Definition at line 780 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

  stopBits | The number of stop bits to set.

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

Definition at line 618 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

  vmin | the number of minimum characters to be set.

Definition at line 630 of file SerialPort.cpp.

## SetVTime()

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

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

Parameters

  vtime | The timeout value in deciseconds to be set.

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

Definition at line 642 of file SerialPort.cpp.

## Write() [1/2]

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

Writes a DataBuffer to the serial port.

Parameters

  dataBuffer | The DataBuffer to write to the serial port.

Examples
  serial_port_read_write.cpp.

Definition at line 756 of file SerialPort.cpp.

## Write() [2/2]

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

Writes a std::string to the serial port.

Parameters

  dataString | The data string to write to the serial port.

Definition at line 762 of file SerialPort.cpp.

## WriteByte() [1/2]

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

Writes a single byte to the serial port.

Parameters

  charbuffer | The byte to write to the serial port.

Examples
  serial_port_read_write.cpp, and serial_port_write.cpp.

Definition at line 768 of file SerialPort.cpp.

## WriteByte() [2/2]

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

Writes a single byte to the serial port.

Parameters

  charbuffer | The byte to write to the serial port.

Definition at line 774 of file SerialPort.cpp.

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

- src/libserial/SerialPort.h
- src/SerialPort.cpp