SerialPort Class Reference

Serial port class. More...

#include <SerialPort.h>

Inheritance diagram for SerialPort:
SerialPortInterface BarcodeReader LMXBluetoothPort ModemPort

List of all members.

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

Detailed Description

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.


Constructor & Destructor Documentation

SerialPort::SerialPort (  ) 

Class constructor.

Initializes the serial class object.

SerialPort::~SerialPort (  )  [virtual]

Class destructor.

Destroys the serial class object.


Member Function Documentation

bool SerialPort::close (  ) 

Close serial port.

Closes the serial port, also flushes I/O before closing the descriptor.

Returns:
Returns true if the operation was a success.
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.

Returns:
Returns current serial port CTS signal state.
void SerialPort::initConfig ( void   )  [protected]

Configure serial port Initiates serial port configuration.

Note:
If there are already valid configuration attributes stored the attributes are simply reapplied to the serial port.
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.

Parameters:
timeOut Specifies the timeout in milliseconds.
Returns:
Returns true if there is data pending for reading.

Implements SerialPortInterface.

bool SerialPort::isPortOpen (  )  [virtual]

Query port status.

Checks if port is open. Port may be closed by system using HUP

Returns:
Returns true if the serial port is opened.

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.

Parameters:
timeOut Specifies the timeout in milliseconds.
Returns:
Returns number of bytes pending for read. Returns 0 if no bytes are pending.

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.

Parameters:
portDev Specifies the serial port device name, e.g. /dev/ttyAT1.
Returns:
Returns true if the serial port was opened successfully.
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.

Parameters:
pData Points to the data buffer.
maxLen Specifies the number of bytes to be read.
Returns:
Returns number of bytes actually received from the serial port. Returns -1 on error.

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.

Returns:
Returns true if the serial port was reopened successfully.

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.

Parameters:
bits Specifies the number of data bits.
Returns:
Returns true if the operation was a success.
void SerialPort::setDTR ( bool  enable  ) 

Set DTR signal.

Set the serial port DTR signal to the specified state.

Parameters:
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_NONE
  • FLOW_SOFT
  • FLOW_HARD
  • FLOW_BOTH
Parameters:
flow Specifies the new flow control mode.
Returns:
Returns true if the operation was a success.
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_NONE
  • PARITY_EVEN
  • PARITY_ODD
Parameters:
parity Specifies the serial port parity check mode.
Returns:
Returns true if the operation was a success.
void SerialPort::setRTS ( bool  enable  ) 

Set RTS signal.

Set the serial port RTS signal to the specified state.

Parameters:
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

Parameters:
speed Specifies the new baud rate.
Returns:
Returns true if the operation was a success.
bool SerialPort::setStopBits ( int  bits  ) 

Set number of stop bits.

Sets number of stop bits for the serial port character.

Parameters:
bits Specifies the number of stop bits, should be set to 1 or 2.
Returns:
Returns true if the operation was a success.
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.

Parameters:
pData Points to the data buffer.
dataLen Specifies the number of bytes to write.
Returns:
Returns number of bytes actually written to the serial port.

Implements SerialPortInterface.


Member Data Documentation

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


The documentation for this class was generated from the following files:
Generated on Wed Oct 20 11:13:44 2010 for libspatha by  doxygen 1.6.3