Serial port class. More...
#include <SerialPort.h>
Public Member Functions | |
SerialPort () | |
Class constructor. | |
virtual | ~SerialPort () |
Class destructor. | |
bool | open (const char *portDev) |
Open serial port. | |
bool | close () |
Close serial port. | |
virtual bool | reOpen () |
Reopen serial port. | |
virtual bool | isPortOpen () |
Query port status. | |
virtual ssize_t | readData (void *pData, size_t maxLen) |
Read data from serial port. | |
virtual ssize_t | writeData (const void *pData, size_t dataLen) |
Write data to serial port. | |
virtual size_t | numInputPending (int timeOut) |
Get number of pending bytes. | |
virtual bool | isInputPending (int timeOut) |
Check if there is input data. | |
virtual void | flushInput () |
Flush serial port input. | |
virtual void | flushOutput () |
Flush serial port output. | |
virtual void | drainOutput () |
Drain the serial port. | |
bool | setSpeed (unsigned long speed) |
Set baud rate. | |
bool | setFlowControl (S_FLOW_CONTROL flow) |
Set flow control mode. | |
bool | setStopBits (int bits) |
Set number of stop bits. | |
bool | setCharBits (int bits) |
Set number of data bits. | |
bool | setParity (S_PARITY_CONTROL parity) |
Set parity mode. | |
void | sendBreak () |
Send break sequence. | |
void | setRTS (bool enable) |
Set RTS signal. | |
void | setDTR (bool enable) |
Set DTR signal. | |
bool | getCTS () |
Read CTS signal. | |
Protected Member Functions | |
void | initConfig () |
Configure serial port Initiates serial port configuration. | |
Protected Attributes | |
int | devFile |
std::string | devName |
bool | hup |
bool | lastAttrValid |
struct termios | lastAttr |
Serial port class.
Serial port class is a wrapper for generic serial port I/O. The class is meant to simplify the handling of serial ports.
SerialPort::SerialPort | ( | ) |
Class constructor.
Initializes the serial class object.
SerialPort::~SerialPort | ( | ) | [virtual] |
Class destructor.
Destroys the serial class object.
bool SerialPort::close | ( | ) |
Close serial port.
Closes the serial port, also flushes I/O before closing the descriptor.
void SerialPort::drainOutput | ( | void | ) | [virtual] |
Drain the serial port.
Waits until all output written to the serial port has been transmitted.
Implements SerialPortInterface.
void SerialPort::flushInput | ( | void | ) | [virtual] |
Flush serial port input.
Discards all serial port input currently pending for receive.
Implements SerialPortInterface.
void SerialPort::flushOutput | ( | void | ) | [virtual] |
Flush serial port output.
Discards all serial port output currently pending for transmit.
Implements SerialPortInterface.
bool SerialPort::getCTS | ( | ) |
Read CTS signal.
void SerialPort::initConfig | ( | void | ) | [protected] |
Configure serial port Initiates serial port configuration.
bool SerialPort::isInputPending | ( | int | timeOut | ) | [virtual] |
Check if there is input data.
Expects incoming data from the serial port. The function blocks if there is no data currently pending for receive, until the timeout expires or the data arrives. The function returns immediately if there is data in the receive buffer already.
timeOut | Specifies the timeout in milliseconds. |
Implements SerialPortInterface.
bool SerialPort::isPortOpen | ( | ) | [virtual] |
Query port status.
Checks if port is open. Port may be closed by system using HUP
Implements SerialPortInterface.
size_t SerialPort::numInputPending | ( | int | timeOut | ) | [virtual] |
Get number of pending bytes.
If no bytes are currently pending in the receive buffer, the function will block until timeout expires, or the data arrives to serial port. If there are bytes pending in the receive buffer already, the function will return immediately.
timeOut | Specifies the timeout in milliseconds. |
Implements SerialPortInterface.
bool SerialPort::open | ( | const char * | portDev | ) |
Open serial port.
Opens the specified serial port and initializes it to default settings. Port baud rate is not changed.
portDev | Specifies the serial port device name, e.g. /dev/ttyAT1. |
ssize_t SerialPort::readData | ( | void * | pData, | |
size_t | maxLen | |||
) | [virtual] |
Read data from serial port.
Receives the specified number of bytes from the serial port. The reading may stop prematurely if a timeout expires while receiving the next character.
pData | Points to the data buffer. | |
maxLen | Specifies the number of bytes to be read. |
Implements SerialPortInterface.
bool SerialPort::reOpen | ( | ) | [virtual] |
Reopen serial port.
Reopens the specified serial port and initializes it to last settings. Port baud rate is not changed.
Implements SerialPortInterface.
void SerialPort::sendBreak | ( | void | ) |
Send break sequence.
Transmits a continuous stream of zero-valued bits for 0.25 seconds.
bool SerialPort::setCharBits | ( | int | bits | ) |
Set number of data bits.
Sets number of data bits for the serial port character. Valid options are 5, 6, 7 or 8 data bits.
bits | Specifies the number of data bits. |
void SerialPort::setDTR | ( | bool | enable | ) |
Set DTR signal.
Set the serial port DTR signal to the specified state.
enable | Specifies the new DTR state |
bool SerialPort::setFlowControl | ( | S_FLOW_CONTROL | flow | ) |
Set flow control mode.
Sets the serial port flow control mode. Allowed modes are listed below.
flow | Specifies the new flow control mode. |
bool SerialPort::setParity | ( | S_PARITY_CONTROL | parity | ) |
Set parity mode.
Sets the new parity control mode of the serial port. Valid modes are listed below.
parity | Specifies the serial port parity check mode. |
void SerialPort::setRTS | ( | bool | enable | ) |
Set RTS signal.
Set the serial port RTS signal to the specified state.
enable | Specifies the new RTS state |
bool SerialPort::setSpeed | ( | unsigned long | speed | ) |
Set baud rate.
Sets new baud rate of the serial port. Accepts the following rates: 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 600, 300, 110
speed | Specifies the new baud rate. |
bool SerialPort::setStopBits | ( | int | bits | ) |
Set number of stop bits.
Sets number of stop bits for the serial port character.
bits | Specifies the number of stop bits, should be set to 1 or 2. |
ssize_t SerialPort::writeData | ( | const void * | pData, | |
size_t | dataLen | |||
) | [virtual] |
Write data to serial port.
Sends the specified number of bytes to the serial port.
pData | Points to the data buffer. | |
dataLen | Specifies the number of bytes to write. |
Implements SerialPortInterface.
int SerialPort::devFile [protected] |
Port device file handle
std::string SerialPort::devName [protected] |
Port device file name
bool SerialPort::hup [protected] |
If set to true, port is closed by the system using HUP
struct termios SerialPort::lastAttr [protected] |
Stores serial port parameters
bool SerialPort::lastAttrValid [protected] |
If set to true, the stored serial port parameters are valid and do not need reconfiguration