SerialDevice Class Reference

#include <Pt/System/SerialDevice.h>

Serial device. More...

Inherits IODevice.

Public Types

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

Protected Member Functions

void onCancel ()
 Blocks until operation has cancelled.
 
bool onRun ()
 Check if ready and run.
 
virtual void onAttach (EventLoop &loop)
 Attached to loop.
 
virtual void onDetach (EventLoop &loop)
 Detached from loop.
 
void post ()
 Posts this selectable to its event loop from any thread.
 

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

◆ read()

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

◆ write()

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

◆ seek()

pos_type seek ( off_type  offset,
seekdir  sd 
)
inherited
Exceptions
IOError

◆ peek()

std::size_t peek ( char *  buffer,
std::size_t  n 
)
inherited
Exceptions
IOError

◆ sync()

void sync ( )
inherited

Commits written data to physical device.

Exceptions
IOError

◆ position()

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

◆ inputReady()

Signal<IODevice&>& inputReady ( )
inherited

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

◆ outputReady()

Signal<IODevice&>& outputReady ( )
inherited

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

static void sleep(unsigned int ms)
Sleep for some time.
Serial device.
Definition: SerialDevice.h:73
System programming
Definition: Client.h:41