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 Member Functions | Friends
TTYStream Class Reference

TTY streams are used to represent serial connections that are fully "streamable" objects using C++ stream classes and friends. More...

#include <serial.h>

Inheritance diagram for TTYStream:
Inheritance graph
[legend]
Collaboration diagram for TTYStream:
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

 TTYStream (const char *filename, timeout_t to=0)
 Create and open a tty serial port. More...
 
virtual ~TTYStream ()
 End the tty stream and cleanup. More...
 
void setTimeout (timeout_t to)
 Set the timeout control. More...
 
void interactive (bool flag)
 Set tty mode to buffered or "interactive". More...
 
int sync (void)
 Flushes the stream input and out buffers, writes pending output. More...
 
bool isPending (Pending pend, timeout_t timeout=TIMEOUT_INF)
 Get the status of pending operations. 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...
 

Protected Member Functions

 TTYStream ()
 This constructor is used to derive "ttystream", a more C++ style version of the TTYStream class. More...
 
void allocate (void)
 Used to allocate the buffer space needed for iostream operations. More...
 
void endStream (void)
 Used to terminate the buffer space and clean up the tty connection. More...
 
int underflow (void)
 This streambuf method is used to load the input buffer through the established tty serial port. More...
 
int uflow (void)
 This streambuf method is used for doing unbuffered reads through the establish tty serial port when in interactive mode. More...
 
int overflow (int ch)
 This streambuf method is used to write the output buffer through the established tty 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

char * gbuf
 
char * pbuf
 
timeout_t timeout
 
HANDLE dev
 
int bufsize
 

Private Member Functions

int doallocate ()
 

Friends

TTYStreamcrlf (TTYStream &)
 
TTYStreamlfcr (TTYStream &)
 

Detailed Description

TTY streams are used to represent serial connections that are fully "streamable" objects using C++ stream classes and friends.

The first application relevant serial I/O class is the TTYStream class. TTYStream offers a linearly buffered "streaming" I/O session with the serial device. Furthermore, traditional C++ "stream" operators (<< and >>) may be used with the serial device. A more "true" to ANSI C++ library format "ttystream" is also available, and this supports an "open" method in which one can pass initial serial device parameters immediately following the device name in a single string, as in "/dev/tty3a:9600,7,e,1", as an example.

The TTYSession aggragates a TTYStream and a Common C++ Thread which is assumed to be the execution context that will be used to perform actual I/O operations. This class is very anagolous to TCPSession.

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m streamable tty serial I/O class.

Definition at line 423 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

TTYStream::TTYStream ( )
protected

This constructor is used to derive "ttystream", a more C++ style version of the TTYStream class.

TTYStream::TTYStream ( const char *  filename,
timeout_t  to = 0 
)

Create and open a tty serial port.

Parameters
filenamechar name of device to open.
todefault timeout.
virtual TTYStream::~TTYStream ( )
virtual

End the tty stream and cleanup.

Member Function Documentation

void TTYStream::allocate ( void  )
protected

Used to allocate the buffer space needed for iostream operations.

This is based on MAX_INPUT.

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.

int TTYStream::doallocate ( )
private
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 TTYStream::endStream ( void  )
protected

Used to terminate the buffer space and clean up the tty connection.

This function is called by the destructor.

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.

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.

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.

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.

void TTYStream::interactive ( bool  flag)

Set tty mode to buffered or "interactive".

When interactive, all streamed I/O is directly sent to the serial port immediately.

Parameters
flagbool set to true to make interactive.
bool TTYStream::isPending ( Pending  pend,
timeout_t  timeout = TIMEOUT_INF 
)
virtual

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. If read buffer contains data then input is ready and if write buffer contains data it is first flushed then checked.

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

Reimplemented from Serial.

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

Opens the serial device.

Parameters
fnamePathname of device to open
int TTYStream::overflow ( int  ch)
protected

This streambuf method is used to write the output buffer through the established tty port.

Parameters
chchar to push through.
Returns
char pushed through.
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 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 TTYStream::setTimeout ( timeout_t  to)
inline

Set the timeout control.

Parameters
totimeout to use.

Definition at line 499 of file serial.h.

int TTYStream::sync ( void  )

Flushes the stream input and out buffers, writes pending output.

Returns
0 on success.
void Serial::toggleDTR ( timeout_t  millisec)
inherited

Set the DTR mode off momentarily.

Parameters
millisecnumber of milliseconds.
int TTYStream::uflow ( void  )
protected

This streambuf method is used for doing unbuffered reads through the establish tty serial port when in interactive mode.

Also this method will handle proper use of buffers if not in interative mode.

Returns
char from tty serial port, EOF also possible.
int TTYStream::underflow ( void  )
protected

This streambuf method is used to load the input buffer through the established tty serial port.

Returns
char from get buffer, EOF also possible.
void Serial::waitOutput ( void  )
protectedinherited

Used to wait until all output has been sent.

Friends And Related Function Documentation

TTYStream& crlf ( TTYStream )
friend
TTYStream& lfcr ( TTYStream )
friend

Field Documentation

int Serial::bufsize
protectedinherited

Definition at line 162 of file serial.h.

HANDLE Serial::dev
protectedinherited

Definition at line 160 of file serial.h.

char* TTYStream::gbuf
protected

Definition at line 432 of file serial.h.

bool Serial::linebuf
inherited

Definition at line 147 of file serial.h.

char * TTYStream::pbuf
protected

Definition at line 432 of file serial.h.

bool Serial::thrown
inherited

Definition at line 146 of file serial.h.

timeout_t TTYStream::timeout
protected

Definition at line 433 of file serial.h.


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