SerialPort Class Reference

`#include <SerialPort.h>`

## Classes

class | AlreadyOpen
class | NotOpen
class | OpenFailed
class | ReadTimeout
class | UnsupportedBaudRate

## Public Types

enum | BaudRate { BAUD_50 = B50, BAUD_75 = B75, BAUD_110 = B110, BAUD_134 = B134, BAUD_150 = B150, BAUD_200 = B200, BAUD_300 = B300, BAUD_600 = B600, BAUD_1200 = B1200, BAUD_1800 = B1800, BAUD_2400 = B2400, BAUD_4800 = B4800, BAUD_9600 = B9600, BAUD_19200 = B19200, BAUD_38400 = B38400, BAUD_57600 = B57600, BAUD_115200 = B115200, BAUD_230400 = B230400, BAUD_DEFAULT = BAUD_57600 }
| The allowed set of baud rates. More...
enum | CharacterSize { CHAR_SIZE_5 = CS5, CHAR_SIZE_6 = CS6, CHAR_SIZE_7 = CS7, CHAR_SIZE_8 = CS8, CHAR_SIZE_DEFAULT = CHAR_SIZE_8 }
enum | StopBits { STOP_BITS_1, STOP_BITS_2, STOP_BITS_DEFAULT = STOP_BITS_1 }
enum | Parity { PARITY_EVEN, PARITY_ODD, PARITY_NONE, PARITY_DEFAULT = PARITY_NONE }
enum | FlowControl { FLOW_CONTROL_HARD, FLOW_CONTROL_SOFT, FLOW_CONTROL_NONE, FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE }
typedef std::vector< unsigned char > | DataBuffer
| Read the specified number of bytes from the serial port. More...

## Public Member Functions

| SerialPort (const std::string &serialPortName)
| Constructor for a serial port. More...
virtual | ~SerialPort () throw ()
| Destructor. More...
void | Open (const BaudRate baudRate=BAUD_DEFAULT, const CharacterSize charSize=CHAR_SIZE_DEFAULT, const Parity parityType=PARITY_DEFAULT, const StopBits stopBits=STOP_BITS_DEFAULT, const FlowControl flowControl=FLOW_CONTROL_DEFAULT) throw ( AlreadyOpen, OpenFailed, UnsupportedBaudRate, std::invalid_argument )
| Open the serial port with the specified settings. More...
bool | IsOpen () const
| Check if the serial port is open for I/O. More...
void | Close () throw (NotOpen)
| Close the serial port. More...
void | SetBaudRate (const BaudRate baudRate) throw ( UnsupportedBaudRate, NotOpen, std::invalid_argument )
| Set the baud rate for the serial port to the specified value (baudRate). More...
BaudRate | GetBaudRate () const throw ( NotOpen, std::runtime_error )
| Get the current baud rate for the serial port. More...
void | SetCharSize (const CharacterSize charSize) throw ( NotOpen, std::invalid_argument )
| Set the character size for the serial port. More...
CharacterSize | GetCharSize () const throw (NotOpen)
| Get the current character size for the serial port. More...
void | SetParity (const Parity parityType) throw ( NotOpen, std::invalid_argument )
| Set the parity type for the serial port. More...
Parity | GetParity () const throw (NotOpen)
| Get the parity type for the serial port. More...
void | SetNumOfStopBits (const StopBits numOfStopBits) throw ( NotOpen, std::invalid_argument )
| Set the number of stop bits to be used with the serial port. More...
StopBits | GetNumOfStopBits () const throw (NotOpen)
| Get the number of stop bits currently being used by the serial port. More...
void | SetFlowControl (const FlowControl flowControl) throw ( NotOpen, std::invalid_argument )
| Set flow control. More...
FlowControl | GetFlowControl () const throw ( NotOpen )
| Get the current flow control setting. More...
bool | IsDataAvailable () const throw (NotOpen)
| Check if data is available at the input of the serial port. More...
unsigned char | ReadByte (const unsigned int msTimeout=0) throw ( NotOpen, ReadTimeout, std::runtime_error )
| Read a single byte from the serial port. More...
void | Read (DataBuffer &dataBuffer, const unsigned int numOfBytes=0, const unsigned int msTimeout=0) throw ( NotOpen, ReadTimeout, std::runtime_error )
const std::string | ReadLine (const unsigned int msTimeout=0, const char lineTerminator='\n') throw ( NotOpen, ReadTimeout, std::runtime_error )
| Read a line of characters from the serial port. More...
void | WriteByte (const unsigned char dataByte) throw ( NotOpen, std::runtime_error )
| Send a single byte to the serial port. More...
void | Write (const DataBuffer &dataBuffer) throw ( NotOpen, std::runtime_error )
| Write the data from the specified vector to the serial port. More...
void | Write (const std::string &dataString) throw ( NotOpen, std::runtime_error )
| Write a string to the serial port. More...
void | SetDtr (const bool dtrState=true) throw ( NotOpen, std::runtime_error )
| Set the DTR line to the specified value. More...
bool | GetDtr () const throw ( NotOpen, std::runtime_error )
| Get the status of the DTR line. More...
void | SetRts (const bool rtsState=true) throw ( NotOpen, std::runtime_error )
| Set the RTS line to the specified value. More...
bool | GetRts () const throw ( NotOpen, std::runtime_error )
| Get the status of the RTS line. More...
bool | GetCts () const throw ( NotOpen, std::runtime_error )
bool | GetDsr () const throw ( NotOpen, std::runtime_error )
int | GetFileDescriptor () const
| Get the low-level file descriptor associated with the serial port. More...

## Private Member Functions

| SerialPort (const SerialPort &otherSerialPort)
| Prevent copying of objects of this class by declaring the copy constructor private. More...
SerialPort & | operator= (const SerialPort &otherSerialPort)
| Prevent copying of objects of this class by declaring the assignment operator private. More...

## Private Attributes

SerialPortImpl * | mSerialPortImpl
| Pointer to implementation class instance. More...

## Detailed Description

Note
  This class attaches a handler to the SIGIO signal to detect the data arriving at a serial port. However, this signal handler will also call any signal handler that is already attached to this signal. However, if other parts of the application attach a signal handler to SIGIO after constructing an instance of SIGIO, they must ensure that they call the existing signal handler. Otherwise, it may not be possible to receive any data through the serial port using this class.

:FIXME: Provide examples of the above potential problem.

Todo:
  The current implementation does not check if another process has locked the serial port device and does not lock the serial port device after opening it. This has been observed to cause problems while using this library while other programs such as minicom are also accessing the same device. It will be useful to lock the serial port device when it is being used by this class.

Definition at line 50 of file SerialPort.h.

## Member Typedef Documentation

## DataBuffer

typedef std::vector<unsigned char> SerialPort::DataBuffer

Read 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 0, then this method will block till all requested bytes are received. If numOfBytes is zero, then this method will keep reading data till no more data is available at the serial port. In all cases, all read data is available in dataBuffer on return from this method.

Definition at line 370 of file SerialPort.h.

## Member Enumeration Documentation

## BaudRate

enum SerialPort::BaudRate

The allowed set of baud rates.

Enumerator
BAUD_50 |
BAUD_75 |
BAUD_110 |
BAUD_134 |
BAUD_150 |
BAUD_200 |
BAUD_300 |
BAUD_600 |
BAUD_1200 |
BAUD_1800 |
BAUD_2400 |
BAUD_4800 |
BAUD_9600 |
BAUD_19200 |
BAUD_38400 |
BAUD_57600 |
BAUD_115200 |
BAUD_230400 |
BAUD_DEFAULT |

Definition at line 56 of file SerialPort.h.

## CharacterSize

enum SerialPort::CharacterSize

Enumerator
CHAR_SIZE_5 | 5 bit characters.
CHAR_SIZE_6 | 6 bit characters.
CHAR_SIZE_7 | 7 bit characters.
CHAR_SIZE_8 | 8 bit characters.
CHAR_SIZE_DEFAULT |

Definition at line 96 of file SerialPort.h.

## FlowControl

enum SerialPort::FlowControl

Enumerator
FLOW_CONTROL_HARD |
FLOW_CONTROL_SOFT |
FLOW_CONTROL_NONE |
FLOW_CONTROL_DEFAULT |

Definition at line 117 of file SerialPort.h.

## Parity

enum SerialPort::Parity

Enumerator
PARITY_EVEN | Even parity.
PARITY_ODD | Odd parity.
PARITY_NONE | No parity i.e. parity checking disabled.
PARITY_DEFAULT |

Definition at line 110 of file SerialPort.h.

## StopBits

enum SerialPort::StopBits

Enumerator
STOP_BITS_1 |
STOP_BITS_2 | 1 stop bit.
STOP_BITS_DEFAULT | 2 stop bits.

Definition at line 104 of file SerialPort.h.

## Constructor & Destructor Documentation

## SerialPort() [1/2]

SerialPort::SerialPort | ( | const std::string & | serialPortName | ) |

explicit

Constructor for a serial port.

## ~SerialPort()

virtual SerialPort::~SerialPort | ( |  | ) |
throw | ( |
| ) |  |

virtual

Destructor.

## SerialPort() [2/2]

SerialPort::SerialPort | ( | const SerialPort & | otherSerialPort | ) |

private

Prevent copying of objects of this class by declaring the copy constructor private.

This method is never defined.

## Member Function Documentation

## Close()

void SerialPort::Close | ( |  | ) |
throw | ( | NotOpen
| ) |  |

Close the serial port.

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

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetBaudRate()

BaudRate SerialPort::GetBaudRate | ( |  | ) | const
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Get the current baud rate for the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetCharSize()

CharacterSize SerialPort::GetCharSize | ( |  | ) | const
throw | ( | NotOpen
| ) |  |

Get the current character size for the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetCts()

bool SerialPort::GetCts | ( |  | ) | const
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

## GetDsr()

bool SerialPort::GetDsr | ( |  | ) | const
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

## GetDtr()

bool SerialPort::GetDtr | ( |  | ) | const
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Get the status of the DTR line.

## GetFileDescriptor()

int SerialPort::GetFileDescriptor | ( |  | ) | const

Get the low-level file descriptor associated with the serial port.

Warning
  This is for advanced/power users only. Messing with this file descriptor (such as closing it) will probably cause problems. Be careful when you use it. Using this file descriptor after the controlling serial port has been closed or deleted (for example, when it goes out of scope) will result in undefined behavior. You have been warned!

Returns
  The low-level file descriptor corresponding to the serial port.

Exceptions

  This | method will throw NotOpen if the serial port is not currently open.

## GetFlowControl()

FlowControl SerialPort::GetFlowControl | ( |  | ) | const
throw | ( | NotOpen
| ) |  |

Get the current flow control setting.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetNumOfStopBits()

StopBits SerialPort::GetNumOfStopBits | ( |  | ) | const
throw | ( | NotOpen
| ) |  |

Get the number of stop bits currently being used by the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetParity()

Parity SerialPort::GetParity | ( |  | ) | const
throw | ( | NotOpen
| ) |  |

Get the parity type for the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## GetRts()

bool SerialPort::GetRts | ( |  | ) | const
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Get the status of the RTS line.

## IsDataAvailable()

bool SerialPort::IsDataAvailable | ( |  | ) | const
throw | ( | NotOpen
| ) |  |

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

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## IsOpen()

bool SerialPort::IsOpen | ( |  | ) | const

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

## Open()

void SerialPort::Open | ( | const BaudRate | baudRate = `BAUD_DEFAULT`,
|  | const CharacterSize | charSize = `CHAR_SIZE_DEFAULT`,
|  | const Parity | parityType = `PARITY_DEFAULT`,
|  | const StopBits | stopBits = `STOP_BITS_DEFAULT`,
|  | const FlowControl | flowControl = `FLOW_CONTROL_DEFAULT`
| ) |  |
throw | ( | AlreadyOpen,
|  | OpenFailed,
|  | UnsupportedBaudRate,
|  | std::invalid_argument
| ) |  |

Open the serial port with the specified settings.

A serial port cannot be used till it is open.

Exceptions

  AlreadyOpen | This exception is thrown if the serial port is already open.
  OpenFailed | This exception is thrown if the serial port could not be opened.
  std::invalid_argument | This exception is thrown if an invalid parameter value is specified.

## operator=()

SerialPort& SerialPort::operator= | ( | const SerialPort & | otherSerialPort | ) |

private

Prevent copying of objects of this class by declaring the assignment operator private.

This method is never defined.

## Read()

void SerialPort::Read | ( | DataBuffer & | dataBuffer,
|  | const unsigned int | numOfBytes = `0`,
|  | const unsigned int | msTimeout = `0`
| ) |  |
throw | ( | NotOpen,
|  | ReadTimeout,
|  | std::runtime_error
| ) |  |

## ReadByte()

unsigned char SerialPort::ReadByte | ( | const unsigned int | msTimeout = `0` | ) |
throw | ( | NotOpen,
|  | ReadTimeout,
|  | std::runtime_error
| ) |  |

Read a single byte from the serial port.

If no data is available in the specified number of milliseconds (msTimeout), then this method will throw ReadTimeout exception. If msTimeout is 0, then this method will block till data is available.

## ReadLine()

const std::string SerialPort::ReadLine | ( | const unsigned int | msTimeout = `0`,
|  | const char | lineTerminator = `'\n'`
| ) |  |
throw | ( | NotOpen,
|  | ReadTimeout,
|  | std::runtime_error
| ) |  |

Read a line of characters from the serial port.

## SetBaudRate()

void SerialPort::SetBaudRate | ( | const BaudRate | baudRate | ) |
throw | ( | UnsupportedBaudRate,
|  | NotOpen,
|  | std::invalid_argument
| ) |  |

Set the baud rate for the serial port to the specified value (baudRate).

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.
  std::invalid_argument | Thrown if an invalid baud rate is specified.

## SetCharSize()

void SerialPort::SetCharSize | ( | const CharacterSize | charSize | ) |
throw | ( | NotOpen,
|  | std::invalid_argument
| ) |  |

Set the character size for the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.
  std::invalid_argument | Thrown if an invalid character size is specified.

## SetDtr()

void SerialPort::SetDtr | ( | const bool | dtrState = `true` | ) |
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Set the DTR line to the specified value.

## SetFlowControl()

void SerialPort::SetFlowControl | ( | const FlowControl | flowControl | ) |
throw | ( | NotOpen,
|  | std::invalid_argument
| ) |  |

Set flow control.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.
  std::invalid_argument | Thrown if an invalid flow control is specified.

## SetNumOfStopBits()

void SerialPort::SetNumOfStopBits | ( | const StopBits | numOfStopBits | ) |
throw | ( | NotOpen,
|  | std::invalid_argument
| ) |  |

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

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.
  std::invalid_argument | Thrown if an invalid number of stop bits is specified.

## SetParity()

void SerialPort::SetParity | ( | const Parity | parityType | ) |
throw | ( | NotOpen,
|  | std::invalid_argument
| ) |  |

Set the parity type for the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.
  std::invalid_argument | Thrown if an invalid parity is specified.

## SetRts()

void SerialPort::SetRts | ( | const bool | rtsState = `true` | ) |
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Set the RTS line to the specified value.

## Write() [1/2]

void SerialPort::Write | ( | const DataBuffer & | dataBuffer | ) |
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Write the data from the specified vector to the serial port.

## Write() [2/2]

void SerialPort::Write | ( | const std::string & | dataString | ) |
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Write a string to the serial port.

## WriteByte()

void SerialPort::WriteByte | ( | const unsigned char | dataByte | ) |
throw | ( | NotOpen,
|  | std::runtime_error
| ) |  |

Send a single byte to the serial port.

Exceptions

  NotOpen | Thrown if this method is called while the serial port is not open.

## Member Data Documentation

## mSerialPortImpl

SerialPortImpl* SerialPort::mSerialPortImpl

private

Pointer to implementation class instance.

Definition at line 504 of file SerialPort.h.

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

- SerialPort.h