SerialDevice Class Reference

#include <Pt/System/SerialDevice.h>

Serial device. More...

Inherits IODevice.

Public Types

enum  BaudRate
 Baud rates.
 
enum  FlowControl
 Flow control values.
 
enum  Parity
 Parity values.
 
enum  StopBits
 Stop bits values.
 

Public Member Functions

 SerialDevice ()
 Default constructor.
 
 SerialDevice (const std::string &file, std::ios::openmode mode)
 Constructs a serial device and open the specified device file.
 
 SerialDevice (const char *file, std::ios::openmode mode)
 Constructs a serial device and open the specified device file.
 
virtual ~SerialDevice ()
 Destructor.
 
unsigned baudRate () const
 Gets the baud rate.
 
void beginRead (char *buffer, std::size_t n)
 Begins to read data.
 
void beginWrite (const char *buffer, std::size_t n)
 Begins to write data.
 
void cancel ()
 Cancels all operations.
 
int charSize () const
 Gets the current char size.
 
void close ()
 Closes the device.
 
void detach ()
 Remove from event loop and cancels outstanding operations.
 
std::size_t endRead ()
 Ends reading data.
 
std::size_t endWrite ()
 Ends writing data.
 
FlowControl flowControl () const
 Gets the current flow control kind.
 
Signal< IODevice & > & inputReady ()
 Notifies about available data. More...
 
bool isEof () const
 Returns if the device has reached EOF.
 
bool isReading () const
 Returns true if the device is reading.
 
bool isWriting () const
 Returns true if the device is writing.
 
EventLooploop () const
 Returns the used event loop.
 
void open (const std::string &file, std::ios::openmode mode)
 Open the specified device file.
 
void open (const char *file, std::ios::openmode mode)
 Open the specified device file.
 
Signal< IODevice & > & outputReady ()
 Notifies when data can be written. More...
 
Parity parity () const
 Gets the current parity.
 
std::size_t peek (char *buffer, std::size_t n)
 Peek data from I/O device without consuming them. More...
 
pos_type position ()
 Returns the current I/O position. More...
 
std::size_t read (char *buffer, std::size_t n)
 Read data from I/O device. More...
 
bool run ()
 Run operation if it is ready.
 
pos_type seek (off_type offset, seekdir sd)
 Moves the read position to the given offset. More...
 
bool seekable () const
 Returns true if device is seekable.
 
void setActive (EventLoop &parent)
 Sets the parent loop, so that operations can be run.
 
void setBaudRate (unsigned rate)
 Sets the baud rate.
 
void setCharSize (int size)
 Sets the char size.
 
void setFlowControl (FlowControl flowControl)
 Sets the flow control kind.
 
void setParity (Parity parity)
 Sets the parity.
 
void setStopBits (StopBits bits)
 Sets the number of stop bits.
 
void setTimeout (std::size_t timeout)
 Sets the timeout for blocking I/O in milliseconds.
 
StopBits stopBits () const
 Gets the current number of stop bits.
 
void sync ()
 Synchronize device. More...
 
std::size_t write (const char *buffer, std::size_t n)
 Write data to I/O device. More...
 

Protected Member Functions

virtual void onAttach (EventLoop &loop)
 Attached to loop.
 
void onCancel ()
 Blocks until operation has cancelled.
 
virtual void onDetach (EventLoop &loop)
 Detached from loop.
 
bool onRun ()
 Check if ready and run.
 

Detailed Description

This class implements access to a serial port as an IODevice. A SerialDevice can be opened by passing a system dependent path and an open mode. Then serial port attributes can be set before read or write operations are performed. The following example opens a COM port on windows, sets serial device attributes for a serial mouse and toggles the flow control to cause the device to send a PNP string which will be read subsequently:

using Pt::System;
Pt::System::SerialDevice serdev( "COM1", std::ios_base::in );
serdev.setBaudRate(Pt::System::SerialDevice::BaudRate1200);
serdev.setCharSize(7);
serdev.setStopBits(Pt::System::SerialDevice::OneStopBit);
serdev.setParity(Pt::System::SerialDevice::ParityNone);
serdev.setFlowControl(Pt::System::SerialDevice::FlowControlHard);
serdev.setFlowControl(Pt::System::SerialDevice::FlowControlSoft);
char pnp_id[200];
size_t size = serdev.read( pnp_id, 200);
std::cerr << "Mouse Id: ";
std::cerr.write(pnp_id, size) << std::endl;

Member Function Documentation

std::size_t read ( char *  buffer,
std::size_t  n 
)
inherited

Reads up to n bytes and stores them in buffer. Returns the number of bytes read, which may be less than requested and even 0 if the device operates in asynchronous (non-blocking) mode. In case of EOF the IODevice is set to eof.

Parameters
bufferbuffer where to place the data to be read.
nnumber of bytes to read
Returns
number of bytes read, which may be less than requested.
Exceptions
IOError
std::size_t write ( const char *  buffer,
std::size_t  n 
)
inherited

Writes n bytes from buffer to this I/O device. Returns the number of bytes written, which may be less than requested. In case of EOF the IODevice is set to eof.

Exceptions
IOError
pos_type seek ( off_type  offset,
seekdir  sd 
)
inherited
Exceptions
IOError
std::size_t peek ( char *  buffer,
std::size_t  n 
)
inherited
void sync ( )
inherited

Commits written data to physical device.

Exceptions
IOError
pos_type position ( )
inherited

The current I/O position is returned or an IOError is thrown if the device is not seekable. Seekability can be tested with seekable().

Exceptions
IOError
Signal<IODevice&>& inputReady ( )
inherited

This signal is send when the IODevice is monitored in an EventLoop and data becomes available.

Signal<IODevice&>& outputReady ( )
inherited

This signal is send when the IODevice is monitored in an EventLoop and the device is ready to write data.