---
name: "SerialPort"
section: "3"
platform: "linux"
package: "libserial-doc"
version: "0.6.0~rc2+svn122-4"
release: "Debian 10 (Buster)"
platforms: ["linux"]
url: "https://new.carta.tech/SerialPort"
---

# SerialPort(3)

## NAME

SerialPort -

## SYNOPSIS

`#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.*  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.*

### Public Member Functions

**SerialPort** (const std::string &serialPortName)\
 *Constructor for a serial port.*  virtual **~SerialPort** ()  throw ()\
 *Destructor.*  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.*  bool **IsOpen** () const \
 *Check if the serial port is open for I/O.*  void **Close** ()  throw (NotOpen)\
 *Close the serial port.*  void **SetBaudRate** (const **BaudRate** baudRate)  throw ( UnsupportedBaudRate,               NotOpen,               std::invalid\_argument )\
 *Set the baud rate for the serial port to the specified value (baudRate).*  **BaudRate** **GetBaudRate** () const   throw ( NotOpen,               std::runtime\_error )\
 *Get the current baud rate for the serial port.*  void **SetCharSize** (const **CharacterSize** charSize)  throw ( NotOpen,               std::invalid\_argument )\
 *Set the character size for the serial port.*  **CharacterSize** **GetCharSize** () const   throw (NotOpen)\
 *Get the current character size for the serial port.*  void **SetParity** (const **Parity** parityType)  throw ( NotOpen,               std::invalid\_argument )\
 *Set the parity type for the serial port.*  **Parity** **GetParity** () const   throw (NotOpen)\
 *Get the parity type for the serial port.*  void **SetNumOfStopBits** (const **StopBits** numOfStopBits)  throw ( NotOpen,               std::invalid\_argument )\
 *Set the number of stop bits to be used with the serial port.*  **StopBits** **GetNumOfStopBits** () const   throw (NotOpen)\
 *Get the number of stop bits currently being used by the serial port.*  void **SetFlowControl** (const **FlowControl** flowControl)  throw ( NotOpen,               std::invalid\_argument )\
 *Set flow control.*  **FlowControl** **GetFlowControl** () const   throw ( NotOpen )\
 *Get the current flow control setting.*  bool **IsDataAvailable** () const   throw (NotOpen)\
 *Check if data is available at the input of the serial port.*  unsigned char **ReadByte** (const unsigned int msTimeout=0)  throw ( NotOpen,               ReadTimeout,               std::runtime\_error )\
 *Read a single byte from the serial port.*  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.*  void **WriteByte** (const unsigned char dataByte)  throw ( NotOpen,               std::runtime\_error )\
 *Send a single byte to the serial port.*  void **Write** (const **DataBuffer** &dataBuffer)  throw ( NotOpen,               std::runtime\_error )\
 *Write the data from the specified vector to the serial port.*  void **Write** (const std::string &dataString)  throw ( NotOpen,               std::runtime\_error )\
 *Write a string to the serial port.*  void **SetDtr** (const bool dtrState=true)  throw ( NotOpen,               std::runtime\_error )\
 *Set the DTR line to the specified value.*  bool **GetDtr** () const   throw ( NotOpen,               std::runtime\_error )\
 *Get the status of the DTR line.*  void **SetRts** (const bool rtsState=true)  throw ( NotOpen,               std::runtime\_error )\
 *Set the RTS line to the specified value.*  bool **GetRts** () const   throw ( NotOpen,               std::runtime\_error )\
 *Get the status of the RTS line.*  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.*

### Private Member Functions

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

### Private Attributes

SerialPortImpl \* **mSerialPortImpl**\
 *Pointer to implementation class instance.*

## 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

### 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

### 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.*

### 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.*

### enum SerialPort::FlowControl

**Enumerator**

- *FLOW\_CONTROL\_HARD*

  - *FLOW\_CONTROL\_SOFT*

  - *FLOW\_CONTROL\_NONE*

  - *FLOW\_CONTROL\_DEFAULT*

  *Definition at line 117 of file SerialPort.h.*

### 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.*

### 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::SerialPort (const std::string & serialPortName)` [explicit]`

*Constructor for a serial port.*

### virtual SerialPort::~SerialPort ()` [virtual]`

*Destructor.*

### 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

### void SerialPort::Close ()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.**

### BaudRate SerialPort::GetBaudRate () const 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.**

### CharacterSize SerialPort::GetCharSize () constNotOpen

**Get the current character size for the serial port.**

**Exceptions:**

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

### bool SerialPort::GetCts () const NotOpen,std::runtime\_error

### bool SerialPort::GetDsr () const NotOpen,std::runtime\_error

### bool SerialPort::GetDtr () const NotOpen,std::runtime\_error

**Get the status of the DTR line.**

### 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.**

### FlowControl SerialPort::GetFlowControl () constNotOpen

**Get the current flow control setting.**

**Exceptions:**

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

### StopBits SerialPort::GetNumOfStopBits () constNotOpen

**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.**

### Parity SerialPort::GetParity () constNotOpen

**Get the parity type for the serial port.**

**Exceptions:**

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

### bool SerialPort::GetRts () const NotOpen,std::runtime\_error

**Get the status of the RTS line.**

### bool SerialPort::IsDataAvailable () constNotOpen

**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.**

### bool SerialPort::IsOpen () const

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

### 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) 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.**

### 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.**

### void SerialPort::Read (DataBuffer & dataBuffer, const unsigned int numOfBytes = `0`, const unsigned int msTimeout = `0`) NotOpen, ReadTimeout,std::runtime\_error

### unsigned char SerialPort::ReadByte (const unsigned int msTimeout = `0`) 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.**

### const std::string SerialPort::ReadLine (const unsigned int msTimeout = `0`, const char lineTerminator = `'\n'`) NotOpen, ReadTimeout,std::runtime\_error

**Read a line of characters from the serial port.**

### void SerialPort::SetBaudRate (const BaudRate baudRate) 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.**

### void SerialPort::SetCharSize (const CharacterSize charSize) 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.**

### void SerialPort::SetDtr (const bool dtrState = `true`) NotOpen,std::runtime\_error

**Set the DTR line to the specified value.**

### void SerialPort::SetFlowControl (const FlowControl flowControl) 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.**

### void SerialPort::SetNumOfStopBits (const StopBits numOfStopBits) 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.**

### void SerialPort::SetParity (const Parity parityType) 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.**

### void SerialPort::SetRts (const bool rtsState = `true`) NotOpen,std::runtime\_error

**Set the RTS line to the specified value.**

### void SerialPort::Write (const DataBuffer & dataBuffer) NotOpen,std::runtime\_error

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

### void SerialPort::Write (const std::string & dataString) NotOpen,std::runtime\_error

**Write a string to the serial port.**

### void SerialPort::WriteByte (const unsigned char dataByte) 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

### SerialPortImpl\* SerialPort::mSerialPortImpl` [private]`

**Pointer to implementation class instance.**

**Definition at line 504 of file SerialPort.h.**

## Author

**Generated automatically by Doxygen for libserial from the source code.**
