Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | Friends
SerialPort Class Reference

The serial port is an internal class which is attached to and then serviced by a specified SerialService thread. More...

#include <serial.h>

Inheritance diagram for SerialPort:
Inheritance graph
[legend]
Collaboration diagram for SerialPort:
Collaboration graph
[legend]

Public Types

enum  Error {
  errSuccess = 0, errOpenNoTty, errOpenFailed, errSpeedInvalid,
  errFlowInvalid, errParityInvalid, errCharsizeInvalid, errStopbitsInvalid,
  errOptionInvalid, errResourceFailure, errOutput, errInput,
  errTimeout, errExtended
}
 
typedef enum Error Error
 
enum  Flow { flowNone, flowSoft, flowHard, flowBoth }
 
typedef enum Flow Flow
 
enum  Parity { parityNone, parityOdd, parityEven }
 
typedef enum Parity Parity
 
enum  Pending { pendingInput, pendingOutput, pendingError }
 
typedef enum Pending Pending
 

Public Member Functions

void setTimer (timeout_t timeout=0)
 Derived setTimer to notify the service thread pool of changes in expected timeout. More...
 
void incTimer (timeout_t timeout)
 Derived incTimer to notify the service thread pool of a change in expected timeout. More...
 
Error setSpeed (unsigned long speed)
 Set serial port speed for both input and output. More...
 
Error setCharBits (int bits)
 Set character size. More...
 
Error setParity (Parity parity)
 Set parity mode. More...
 
Error setStopBits (int bits)
 Set number of stop bits. More...
 
Error setFlowControl (Flow flow)
 Set flow control. More...
 
void toggleDTR (timeout_t millisec)
 Set the DTR mode off momentarily. More...
 
void sendBreak (void)
 Send the "break" signal. More...
 
Error getErrorNumber (void)
 Often used by a "catch" to fetch the last error of a thrown serial. More...
 
char * getErrorString (void)
 Often used by a "catch" to fetch the user set error string of a thrown serial. More...
 
int getBufferSize (void)
 Get the "buffer" size for buffered operations. More...
 
virtual bool isPending (Pending pend, timeout_t timeout=TIMEOUT_INF)
 Get the status of pending operations. More...
 
void decTimer (timeout_t timeout)
 Adjust a timeout based on the current time reference value either from object creation or the last setTimer(). More...
 
void sleepTimer (void)
 Sleep until the current timer expires. More...
 
void endTimer (void)
 This is used to "disable" the service thread from expiring the timer object. More...
 
timeout_t getTimer (void) const
 This is used by service threads to determine how much time remains before the timer expires based on a timeout specified in setTimer() or incTimer(). More...
 
timeout_t getElapsed (void) const
 This is used to determine how much time has elapsed since a timer port setTimer benchmark time was initially set. More...
 

Protected Member Functions

 SerialPort (SerialService *svc, const char *name)
 Construct a tty serial port for a named serial device. More...
 
virtual ~SerialPort ()
 Disconnect the Serial Port from the service pool thread and shutdown the port. More...
 
void setDetectPending (bool)
 Used to indicate if the service thread should monitor pending data for us. More...
 
bool getDetectPending (void) const
 Get the current state of the DetectPending flag. More...
 
void setDetectOutput (bool)
 Used to indicate if output ready monitoring should be performed by the service thread. More...
 
bool getDetectOutput (void) const
 Get the current state of the DetectOutput flag. More...
 
virtual void expired (void)
 Called by the service thread when the objects timer has expired. More...
 
virtual void pending (void)
 Called by the service thread when input data is pending for this tty port. More...
 
virtual void disconnect (void)
 Called by the service thread when an exception has occured such as a hangup. More...
 
int output (void *buf, int len)
 Transmit "send" data to the serial port. More...
 
virtual void output (void)
 Perform when output is available for sending data. More...
 
int input (void *buf, int len)
 Receive "input" for pending data from the serial port. More...
 
void open (const char *fname)
 Opens the serial device. More...
 
void close (void)
 Closes the serial device. More...
 
virtual int aRead (char *Data, const int Length)
 Reads from serial device. More...
 
virtual int aWrite (const char *Data, const int Length)
 Writes to serial device. More...
 
Error error (Error error, char *errstr=NULL)
 This service is used to throw all serial errors which usually occur during the serial constructor. More...
 
void error (char *err)
 This service is used to thow application defined serial errors where the application specific error code is a string. More...
 
void setError (bool enable)
 This method is used to turn the error handler on or off for "throwing" execptions by manipulating the thrown flag. More...
 
int setPacketInput (int size, unsigned char btimer=0)
 Set packet read mode and "size" of packet read buffer. More...
 
int setLineInput (char newline=13, char nl1=0)
 Set "line buffering" read mode and specifies the newline character to be used in seperating line records. More...
 
void restore (void)
 Restore serial device to the original settings at time of open. More...
 
void flushInput (void)
 Used to flush the input waiting queue. More...
 
void flushOutput (void)
 Used to flush any pending output data. More...
 
void waitOutput (void)
 Used to wait until all output has been sent. More...
 
void endSerial (void)
 Used as the default destructor for ending serial I/O services. More...
 
void initConfig (void)
 Used to initialize a newly opened serial file handle. More...
 

Protected Attributes

HANDLE dev
 
int bufsize
 

Private Attributes

SerialPortnext
 
SerialPortprev
 
SerialServiceservice
 
bool detect_pending
 
bool detect_output
 
bool detect_disconnect
 

Friends

class SerialService
 

Detailed Description

The serial port is an internal class which is attached to and then serviced by a specified SerialService thread.

Derived versions of this class offer specific functionality such as serial integration protocols.

The TTYPort and TTYService classes are used to form thread-pool serviced serial I/O protocol sets. These can be used when one has a large number of serial devices to manage, and a single (or limited number of) thread(s) can then be used to service the tty port objects present. Each tty port supports a timer control and several virtual methods that the service thread can call when events occur. This model provides for "callback" event management, whereby the service thread performs a "callback" into the port object when events occur. Specific events supported include the expiration of a TTYPort timer, pending input data waiting to be read, and "sighup" connection breaks.

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m base class for thread pool serviced serial I/O.

Definition at line 631 of file serial.h.

Member Typedef Documentation

typedef enum Error Serial::Error
inherited

Definition at line 117 of file serial.h.

typedef enum Flow Serial::Flow
inherited

Definition at line 125 of file serial.h.

typedef enum Parity Serial::Parity
inherited

Definition at line 132 of file serial.h.

typedef enum Pending Serial::Pending
inherited

Definition at line 139 of file serial.h.

Member Enumeration Documentation

enum Serial::Error
inherited
Enumerator
errSuccess 
errOpenNoTty 
errOpenFailed 
errSpeedInvalid 
errFlowInvalid 
errParityInvalid 
errCharsizeInvalid 
errStopbitsInvalid 
errOptionInvalid 
errResourceFailure 
errOutput 
errInput 
errTimeout 
errExtended 

Definition at line 101 of file serial.h.

enum Serial::Flow
inherited
Enumerator
flowNone 
flowSoft 
flowHard 
flowBoth 

Definition at line 119 of file serial.h.

enum Serial::Parity
inherited
Enumerator
parityNone 
parityOdd 
parityEven 

Definition at line 127 of file serial.h.

enum Serial::Pending
inherited
Enumerator
pendingInput 
pendingOutput 
pendingError 

Definition at line 134 of file serial.h.

Constructor & Destructor Documentation

SerialPort::SerialPort ( SerialService svc,
const char *  name 
)
protected

Construct a tty serial port for a named serial device.

Parameters
svcpool thread object.
nameof tty port.
virtual SerialPort::~SerialPort ( )
protectedvirtual

Disconnect the Serial Port from the service pool thread and shutdown the port.

Member Function Documentation

virtual int Serial::aRead ( char *  Data,
const int  Length 
)
protectedvirtualinherited

Reads from serial device.

Parameters
DataPoint to character buffer to receive data. Buffers MUST be at least Length + 1 bytes in size.
LengthNumber of bytes to read.
virtual int Serial::aWrite ( const char *  Data,
const int  Length 
)
protectedvirtualinherited

Writes to serial device.

Parameters
DataPoint to character buffer containing data to write. Buffers MUST
LengthNumber of bytes to write.
void Serial::close ( void  )
protectedinherited

Closes the serial device.

void TimerPort::decTimer ( timeout_t  timeout)
inherited

Adjust a timeout based on the current time reference value either from object creation or the last setTimer().

This reference can be used to time synchronize realtime data over specified intervals and force expiration when a new frame should be released in a synchronized manner.

Parameters
timeoutdelay in milliseconds from reference.
virtual void SerialPort::disconnect ( void  )
protectedvirtual

Called by the service thread when an exception has occured such as a hangup.

void Serial::endSerial ( void  )
protectedinherited

Used as the default destructor for ending serial I/O services.

It will restore the port to it's original state.

void TimerPort::endTimer ( void  )
inherited

This is used to "disable" the service thread from expiring the timer object.

It does not effect the reference time from either creation or a setTimer().

Error Serial::error ( Error  error,
char *  errstr = NULL 
)
protectedinherited

This service is used to throw all serial errors which usually occur during the serial constructor.

Parameters
errordefined serial error id.
errstrstring or message to optionally pass.
void Serial::error ( char *  err)
inlineprotectedinherited

This service is used to thow application defined serial errors where the application specific error code is a string.

Parameters
errstring or message to pass.

Definition at line 209 of file serial.h.

virtual void SerialPort::expired ( void  )
protectedvirtual

Called by the service thread when the objects timer has expired.

void Serial::flushInput ( void  )
protectedinherited

Used to flush the input waiting queue.

void Serial::flushOutput ( void  )
protectedinherited

Used to flush any pending output data.

int Serial::getBufferSize ( void  )
inlineinherited

Get the "buffer" size for buffered operations.

This can be used when setting packet or line read modes to determine how many bytes to wait for in a given read call.

Returns
number of bytes used for buffering.

Definition at line 387 of file serial.h.

bool SerialPort::getDetectOutput ( void  ) const
inlineprotected

Get the current state of the DetectOutput flag.

Definition at line 681 of file serial.h.

bool SerialPort::getDetectPending ( void  ) const
inlineprotected

Get the current state of the DetectPending flag.

Definition at line 669 of file serial.h.

timeout_t TimerPort::getElapsed ( void  ) const
inherited

This is used to determine how much time has elapsed since a timer port setTimer benchmark time was initially set.

This allows one to use setTimer() to set the timer to the current time and then measure elapsed time from that point forward.

return time elapsed in milliseconds, or TIMEOUT_INF if inactive.

Error Serial::getErrorNumber ( void  )
inlineinherited

Often used by a "catch" to fetch the last error of a thrown serial.

Returns
error numbr of last Error.

Definition at line 368 of file serial.h.

char* Serial::getErrorString ( void  )
inlineinherited

Often used by a "catch" to fetch the user set error string of a thrown serial.

Returns
string for error message.

Definition at line 377 of file serial.h.

timeout_t TimerPort::getTimer ( void  ) const
inherited

This is used by service threads to determine how much time remains before the timer expires based on a timeout specified in setTimer() or incTimer().

It can also be called after setting a timeout with incTimer() to see if the current timeout has already expired and hence that the application is already delayed and should skip frame(s).

return time remaining in milliseconds, or TIMEOUT_INF if inactive.

void SerialPort::incTimer ( timeout_t  timeout)

Derived incTimer to notify the service thread pool of a change in expected timeout.

This allows SerialService to reschedule all timers.

void Serial::initConfig ( void  )
protectedinherited

Used to initialize a newly opened serial file handle.

You should set serial properties and DTR manually before first use.

int SerialPort::input ( void *  buf,
int  len 
)
inlineprotected

Receive "input" for pending data from the serial port.

This is not a public member since it's meant to support internal protocols rather than direct external access to the device.

Returns
number of bytes received.
Parameters
bufaddress of buffer to input.
lenof input buffer used.

Definition at line 729 of file serial.h.

virtual bool Serial::isPending ( Pending  pend,
timeout_t  timeout = TIMEOUT_INF 
)
virtualinherited

Get the status of pending operations.

This can be used to examine if input or output is waiting, or if an error has occured on the serial device.

Returns
true if ready, false if timeout.
Parameters
pendready check to perform.
timeoutin milliseconds.

Reimplemented in TTYStream.

void Serial::open ( const char *  fname)
protectedinherited

Opens the serial device.

Parameters
fnamePathname of device to open
int SerialPort::output ( void *  buf,
int  len 
)
inlineprotected

Transmit "send" data to the serial port.

This is not public since it's meant to support internal protocols rather than direct public access to the device.

Returns
number of bytes send.
Parameters
bufaddress of buffer to send.
lenof bytes to send.

Definition at line 712 of file serial.h.

virtual void SerialPort::output ( void  )
protectedvirtual

Perform when output is available for sending data.

virtual void SerialPort::pending ( void  )
protectedvirtual

Called by the service thread when input data is pending for this tty port.

Effected by setPacketInput and by setLineInput.

void Serial::restore ( void  )
protectedinherited

Restore serial device to the original settings at time of open.

void Serial::sendBreak ( void  )
inherited

Send the "break" signal.

Error Serial::setCharBits ( int  bits)
inherited

Set character size.

Returns
0 on success.
Parameters
bitscharacter size to use (usually 7 or 8).
void SerialPort::setDetectOutput ( bool  )
protected

Used to indicate if output ready monitoring should be performed by the service thread.

void SerialPort::setDetectPending ( bool  )
protected

Used to indicate if the service thread should monitor pending data for us.

void Serial::setError ( bool  enable)
inlineprotectedinherited

This method is used to turn the error handler on or off for "throwing" execptions by manipulating the thrown flag.

Parameters
enabletrue to enable handler.

Definition at line 219 of file serial.h.

Error Serial::setFlowControl ( Flow  flow)
inherited

Set flow control.

Returns
0 on success.
Parameters
flowcontrol mode.
int Serial::setLineInput ( char  newline = 13,
char  nl1 = 0 
)
protectedinherited

Set "line buffering" read mode and specifies the newline character to be used in seperating line records.

isPending can then be used to wait for an entire line of input.

Parameters
newlinenewline character.
nl1EOL2 control character.
Returns
size of conical input buffer.
int Serial::setPacketInput ( int  size,
unsigned char  btimer = 0 
)
protectedinherited

Set packet read mode and "size" of packet read buffer.

This sets VMIN to x. VTIM is normally set to "0" so that "isPending()" can wait for an entire packet rather than just the first byte.

Returns
actual buffer size set.
Parameters
sizeof packet read request.
btimeroptional inter-byte data packet timeout.
Error Serial::setParity ( Parity  parity)
inherited

Set parity mode.

Returns
0 on success.
Parameters
paritymode.
Error Serial::setSpeed ( unsigned long  speed)
inherited

Set serial port speed for both input and output.

Returns
0 on success.
Parameters
speedto select. 0 signifies modem "hang up".
Error Serial::setStopBits ( int  bits)
inherited

Set number of stop bits.

Returns
0 on success.
Parameters
bitsstop bits.
void SerialPort::setTimer ( timeout_t  timeout = 0)

Derived setTimer to notify the service thread pool of changes in expected timeout.

This allows SerialService to reschedule all timers.

Parameters
timeoutin milliseconds.
void TimerPort::sleepTimer ( void  )
inherited

Sleep until the current timer expires.

This is useful in time syncing realtime periodic tasks.

void Serial::toggleDTR ( timeout_t  millisec)
inherited

Set the DTR mode off momentarily.

Parameters
millisecnumber of milliseconds.
void Serial::waitOutput ( void  )
protectedinherited

Used to wait until all output has been sent.

Friends And Related Function Documentation

friend class SerialService
friend

Definition at line 643 of file serial.h.

Field Documentation

int Serial::bufsize
protectedinherited

Definition at line 162 of file serial.h.

bool SerialPort::detect_disconnect
private

Definition at line 641 of file serial.h.

bool SerialPort::detect_output
private

Definition at line 640 of file serial.h.

bool SerialPort::detect_pending
private

Definition at line 639 of file serial.h.

HANDLE Serial::dev
protectedinherited

Definition at line 160 of file serial.h.

bool Serial::linebuf
inherited

Definition at line 147 of file serial.h.

SerialPort* SerialPort::next
private

Definition at line 634 of file serial.h.

SerialPort * SerialPort::prev
private

Definition at line 634 of file serial.h.

SerialService* SerialPort::service
private

Definition at line 635 of file serial.h.

bool Serial::thrown
inherited

Definition at line 146 of file serial.h.


The documentation for this class was generated from the following file: