SerialStreamBuf.h
Go to the documentation of this file.
/*
* Time-stamp: <02-Nov-2008 14:52:35 pagey>
*
* $Id: SerialStreamBuf.h,v 1.9 2005-10-17 00:19:12 crayzeewulf Exp $
*
*
*/
#ifndef _SerialStreamBuf_h_
#define _SerialStreamBuf_h_
#include <termios.h>
#include <unistd.h>
#include <iosfwd>
#include <streambuf>
#include <string>
#include <SerialPort.h>
extern "C++" {
namespace LibSerial {
class SerialStreamBuf : public std::streambuf {
public:
enum BaudRateEnum {
BAUD_50 = SerialPort::BAUD_50,
BAUD_75 = SerialPort::BAUD_75,
BAUD_110 = SerialPort::BAUD_110,
BAUD_134 = SerialPort::BAUD_134,
BAUD_150 = SerialPort::BAUD_150,
BAUD_200 = SerialPort::BAUD_200,
BAUD_300 = SerialPort::BAUD_300,
BAUD_600 = SerialPort::BAUD_600,
BAUD_1200 = SerialPort::BAUD_1200,
BAUD_1800 = SerialPort::BAUD_1800,
BAUD_2400 = SerialPort::BAUD_2400,
BAUD_4800 = SerialPort::BAUD_4800,
BAUD_9600 = SerialPort::BAUD_9600,
BAUD_19200 = SerialPort::BAUD_19200,
BAUD_38400 = SerialPort::BAUD_38400,
BAUD_57600 = SerialPort::BAUD_57600,
BAUD_115200 = SerialPort::BAUD_115200,
BAUD_230400 = SerialPort::BAUD_230400,
#ifdef __linux__
BAUD_460800 = SerialPort::BAUD_460800,
#endif
BAUD_DEFAULT = SerialPort::BAUD_DEFAULT,
BAUD_INVALID
} ;
enum CharSizeEnum {
CHAR_SIZE_5 = SerialPort::CHAR_SIZE_5,
CHAR_SIZE_6 = SerialPort::CHAR_SIZE_6,
CHAR_SIZE_7 = SerialPort::CHAR_SIZE_7,
CHAR_SIZE_8 = SerialPort::CHAR_SIZE_8,
CHAR_SIZE_DEFAULT = SerialPort::CHAR_SIZE_DEFAULT,
CHAR_SIZE_INVALID
} ;
enum ParityEnum {
PARITY_EVEN = SerialPort::PARITY_EVEN,
PARITY_ODD = SerialPort::PARITY_ODD,
PARITY_NONE = SerialPort::PARITY_NONE,
PARITY_DEFAULT = SerialPort::PARITY_DEFAULT,
PARITY_INVALID
} ;
enum FlowControlEnum {
FLOW_CONTROL_HARD = SerialPort::FLOW_CONTROL_HARD,
FLOW_CONTROL_SOFT = SerialPort::FLOW_CONTROL_SOFT,
FLOW_CONTROL_NONE = SerialPort::FLOW_CONTROL_NONE,
FLOW_CONTROL_DEFAULT = SerialPort::FLOW_CONTROL_DEFAULT,
FLOW_CONTROL_INVALID
} ;
/* ------------------------------------------------------------
* Public Static Members
* ------------------------------------------------------------
*/
static const BaudRateEnum DEFAULT_BAUD ;
static const CharSizeEnum DEFAULT_CHAR_SIZE ;
static const short DEFAULT_NO_OF_STOP_BITS ;
static const ParityEnum DEFAULT_PARITY ;
static const FlowControlEnum DEFAULT_FLOW_CONTROL ;
static const short DEFAULT_VMIN ;
static const short DEFAULT_VTIME ;
SerialStreamBuf() ;
~SerialStreamBuf() ;
bool is_open() const ;
SerialStreamBuf* open( const std::string filename,
std::ios_base::openmode mode =
std::ios_base::in | std::ios_base::out ) ;
SerialStreamBuf* close() ;
int SetParametersToDefault() ;
const BaudRateEnum SetBaudRate(const BaudRateEnum baudRate ) ;
const BaudRateEnum BaudRate() const ;
const CharSizeEnum SetCharSize(const CharSizeEnum charSize) ;
const CharSizeEnum CharSize() const ;
short SetNumOfStopBits(short numOfStopBits) ;
short NumOfStopBits() const ;
const ParityEnum SetParity(const ParityEnum parityType) ;
const ParityEnum Parity() const ;
const FlowControlEnum SetFlowControl(const FlowControlEnum flowControlType) ;
const FlowControlEnum FlowControl() const ;
const short SetVMin( short vtime ) ;
const short VMin() const;
const short SetVTime( short vtime ) ;
const short VTime() const;
/* ------------------------------------------------------------
* Friends
* ------------------------------------------------------------
*/
protected:
/* ------------------------------------------------------------
* Protected Data Members
* ------------------------------------------------------------
*/
static const char CTRL_Q = 0x11 ;
static const char CTRL_S = 0x13 ;
/* ------------------------------------------------------------
* Protected Methods
* ------------------------------------------------------------
*/
virtual std::streambuf* setbuf( char_type*,
std::streamsize ) ;
virtual std::streamsize xsgetn( char_type* s,
std::streamsize n ) ;
virtual std::streamsize showmanyc();
virtual int_type underflow() ;
virtual int_type uflow() ;
virtual int_type pbackfail(int_type c = traits_type::eof()) ;
virtual std::streamsize xsputn( const char_type* s,
std::streamsize n ) ;
virtual int_type overflow(int_type c) ;
private:
/* ------------------------------------------------------------
* Private Data Members
* ------------------------------------------------------------
*/
//
// The copy constructor and the assignment operator are
// declared private but never defined. This allows the
// compiler to catch attempts to copy instances of this
// class.
//
SerialStreamBuf( const SerialStreamBuf& ) ;
SerialStreamBuf& operator=( const SerialStreamBuf& ) ;
char mPutbackChar ;
bool mPutbackAvailable ;
int mFileDescriptor ;
/* ------------------------------------------------------------
* Private Methods
* ------------------------------------------------------------
*/
int InitializeSerialPort() ;
} ; // class SerialStreamBuf
inline
SerialStreamBuf::SerialStreamBuf() :
mPutbackChar(0),
mPutbackAvailable(false),
mFileDescriptor(-1)
{
setbuf(0, 0) ;
return ;
}
inline
SerialStreamBuf::~SerialStreamBuf()
{
if( this->is_open() )
{
this->close() ;
}
return ;
}
inline
bool
SerialStreamBuf::is_open() const
{
return (-1 != mFileDescriptor) ;
}
inline
std::streambuf*
SerialStreamBuf::setbuf(char_type *, std::streamsize)
{
return std::streambuf::setbuf(0, 0) ;
}
inline
SerialStreamBuf*
SerialStreamBuf::close()
{
//
// Return a null pointer if the serial port is not currently open.
//
if( this->is_open() == false ) {
return 0 ;
}
//
// Otherwise, close the serial port and set the file descriptor
// to an invalid value.
//
if( -1 == ::close(mFileDescriptor) ) {
//
// If the close failed then return a null pointer.
//
return 0 ;
} else {
//
// Set the file descriptor to an invalid value, -1.
//
mFileDescriptor = -1 ;
//
// On success, return "this" as required by the C++ standard.
//
return this ;
}
}
inline
std::streambuf::int_type
SerialStreamBuf::uflow()
{
int_type next_ch = underflow() ;
mPutbackAvailable = false ;
return next_ch ;
}
} // namespace LibSerial
} // extern "C++"
#endif // #ifndef _SerialStreamBuf_h_
LibSerial::SerialStreamBuf::InitializeSerialPort
int InitializeSerialPort()
This routine is called by open() in order to initialize some parameters of the serial port and settin...
LibSerial::SerialStreamBuf::FlowControlEnum
FlowControlEnum
The values of the flow control settings for a serial port.
Definition: SerialStreamBuf.h:130
SerialPort::BAUD_38400
Definition: SerialPort.h:71
LibSerial::SerialStreamBuf::BAUD_2400
Definition: SerialStreamBuf.h:73
LibSerial::SerialStreamBuf::DEFAULT_VTIME
static const short DEFAULT_VTIME
The default character buffer timing.
Definition: SerialStreamBuf.h:206
LibSerial::SerialStreamBuf::VTime
const short VTime() const
Get the current timeout value for non-canonical reads in deciseconds.
SerialPort::BAUD_230400
Definition: SerialPort.h:74
LibSerial::SerialStreamBuf::mFileDescriptor
int mFileDescriptor
The file descriptor associated with the serial port.
Definition: SerialStreamBuf.h:546
LibSerial::SerialStreamBuf::CHAR_SIZE_DEFAULT
Definition: SerialStreamBuf.h:102
LibSerial::SerialStreamBuf::DEFAULT_PARITY
static const ParityEnum DEFAULT_PARITY
The default parity setting.
Definition: SerialStreamBuf.h:178
LibSerial::SerialStreamBuf::BaudRate
const BaudRateEnum BaudRate() const
Return the current baud rate of the serial port.
LibSerial::SerialStreamBuf::Parity
const ParityEnum Parity() const
Get the current parity setting for the serial port.
LibSerial::SerialStreamBuf::DEFAULT_VMIN
static const short DEFAULT_VMIN
The default character buffer size.
Definition: SerialStreamBuf.h:196
SerialPort::BAUD_134
Definition: SerialPort.h:60
LibSerial::SerialStreamBuf::BAUD_DEFAULT
Definition: SerialStreamBuf.h:84
LibSerial::SerialStreamBuf::CharSizeEnum
CharSizeEnum
The allowed values of character sizes that can be used during the serial communication.
Definition: SerialStreamBuf.h:97
LibSerial::SerialStreamBuf::BAUD_19200
Definition: SerialStreamBuf.h:76
SerialPort::BAUD_110
Definition: SerialPort.h:59
LibSerial::SerialStreamBuf::BAUD_9600
Definition: SerialStreamBuf.h:75
LibSerial::SerialStreamBuf::FLOW_CONTROL_NONE
Definition: SerialStreamBuf.h:133
LibSerial::SerialStreamBuf::BAUD_115200
Definition: SerialStreamBuf.h:79
LibSerial::SerialStreamBuf::xsputn
virtual std::streamsize xsputn(const char_type *s, std::streamsize n)
Writes upto n characters from the character sequence at s to the serial port associated with the buff...
SerialPort::FLOW_CONTROL_DEFAULT
Definition: SerialPort.h:110
SerialPort::BAUD_1200
Definition: SerialPort.h:65
LibSerial::SerialStreamBuf::CHAR_SIZE_8
Definition: SerialStreamBuf.h:101
LibSerial::SerialStreamBuf::~SerialStreamBuf
~SerialStreamBuf()
The destructor.
Definition: SerialStreamBuf.h:572
LibSerial::SerialStreamBuf::uflow
virtual int_type uflow()
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
Definition: SerialStreamBuf.h:628
SerialPort::CHAR_SIZE_DEFAULT
Definition: SerialPort.h:90
LibSerial::SerialStreamBuf::VMin
const short VMin() const
Get the VMIN value for the device.
LibSerial::SerialStreamBuf::SetNumOfStopBits
short SetNumOfStopBits(short numOfStopBits)
Set the number of stop bits used during serial communication.
LibSerial::SerialStreamBuf::FLOW_CONTROL_SOFT
Definition: SerialStreamBuf.h:132
LibSerial::SerialStreamBuf::BAUD_50
Definition: SerialStreamBuf.h:63
LibSerial::SerialStreamBuf::showmanyc
virtual std::streamsize showmanyc()
Check, wether input is available on the port.
SerialPort::FLOW_CONTROL_SOFT
Definition: SerialPort.h:108
LibSerial::SerialStreamBuf::DEFAULT_CHAR_SIZE
static const CharSizeEnum DEFAULT_CHAR_SIZE
The default value of the character size used during the serial communication.
Definition: SerialStreamBuf.h:162
LibSerial::SerialStreamBuf::SetVTime
const short SetVTime(short vtime)
Set character buffer timeout in 10ths of a second.
LibSerial::SerialStreamBuf::BAUD_230400
Definition: SerialStreamBuf.h:80
LibSerial::SerialStreamBuf::DEFAULT_FLOW_CONTROL
static const FlowControlEnum DEFAULT_FLOW_CONTROL
The default flow control setting.
Definition: SerialStreamBuf.h:186
SerialPort::BAUD_57600
Definition: SerialPort.h:72
LibSerial::SerialStreamBuf::CTRL_Q
static const char CTRL_Q
Character used to signal that I/O can start while using software flow control with the serial port...
Definition: SerialStreamBuf.h:426
LibSerial::SerialStreamBuf::PARITY_ODD
Definition: SerialStreamBuf.h:116
SerialPort::BAUD_115200
Definition: SerialPort.h:73
LibSerial::SerialStreamBuf::mPutbackAvailable
bool mPutbackAvailable
True if a putback value is available in mPutbackChar.
Definition: SerialStreamBuf.h:541
SerialPort::BAUD_75
Definition: SerialPort.h:58
LibSerial::SerialStreamBuf::BAUD_110
Definition: SerialStreamBuf.h:65
LibSerial::SerialStreamBuf::SetBaudRate
const BaudRateEnum SetBaudRate(const BaudRateEnum baudRate)
If is_open() != true, return -1.
LibSerial::SerialStreamBuf::open
SerialStreamBuf * open(const std::string filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
If is_open() != false, returns a null pointer.
LibSerial::SerialStreamBuf::BAUD_38400
Definition: SerialStreamBuf.h:77
LibSerial::SerialStreamBuf::BAUD_1800
Definition: SerialStreamBuf.h:72
LibSerial::SerialStreamBuf::CHAR_SIZE_7
Definition: SerialStreamBuf.h:100
LibSerial::SerialStreamBuf::BAUD_1200
Definition: SerialStreamBuf.h:71
LibSerial::SerialStreamBuf::BaudRateEnum
BaudRateEnum
The baud rates currently supported by the SUS-2 general terminal interface specification.
Definition: SerialStreamBuf.h:62
SerialPort::CHAR_SIZE_7
7 bit characters.
Definition: SerialPort.h:88
SerialPort::BAUD_1800
Definition: SerialPort.h:66
LibSerial::SerialStreamBuf::PARITY_NONE
Definition: SerialStreamBuf.h:117
LibSerial::SerialStreamBuf::BAUD_150
Definition: SerialStreamBuf.h:67
LibSerial::SerialStreamBuf::SetParity
const ParityEnum SetParity(const ParityEnum parityType)
Set the parity for serial communication.
LibSerial::SerialStreamBuf::SetCharSize
const CharSizeEnum SetCharSize(const CharSizeEnum charSize)
Set the character size to be used during serial communication.
LibSerial::SerialStreamBuf::is_open
bool is_open() const
Returns true if a previous call to open() succeeded (returned a non-null value) and there has been no...
Definition: SerialStreamBuf.h:583
LibSerial::SerialStreamBuf::PARITY_INVALID
Invalid parity value.
Definition: SerialStreamBuf.h:119
SerialPort::FLOW_CONTROL_HARD
Definition: SerialPort.h:107
LibSerial::SerialStreamBuf::ParityEnum
ParityEnum
The allowed values of the parity associated with the serial port communications.
Definition: SerialStreamBuf.h:114
LibSerial::SerialStreamBuf::SetParametersToDefault
int SetParametersToDefault()
Initialize the serial communication parameters to their default values.
SerialPort::BAUD_19200
Definition: SerialPort.h:70
SerialPort::PARITY_DEFAULT
Definition: SerialPort.h:103
LibSerial::SerialStreamBuf::BAUD_4800
Definition: SerialStreamBuf.h:74
SerialPort::PARITY_NONE
No parity i.e. parity checking disabled.
Definition: SerialPort.h:102
LibSerial::SerialStreamBuf::SetVMin
const short SetVMin(short vtime)
Set the minimum number of characters for non-canonical reads.
LibSerial::SerialStreamBuf
This is the streambuf subclass used by SerialStream.
Definition: SerialStreamBuf.h:38
SerialPort::CHAR_SIZE_5
5 bit characters.
Definition: SerialPort.h:86
SerialPort::FLOW_CONTROL_NONE
Definition: SerialPort.h:109
LibSerial::SerialStreamBuf::mPutbackChar
char mPutbackChar
We use unbuffered I/O for the serial port.
Definition: SerialStreamBuf.h:536
SerialPort::BAUD_9600
Definition: SerialPort.h:69
SerialPort::BAUD_600
Definition: SerialPort.h:64
LibSerial::SerialStreamBuf::FlowControl
const FlowControlEnum FlowControl() const
Return the current flow control setting.
SerialPort::CHAR_SIZE_8
8 bit characters.
Definition: SerialPort.h:89
LibSerial::SerialStreamBuf::CHAR_SIZE_INVALID
Definition: SerialStreamBuf.h:103
LibSerial::SerialStreamBuf::PARITY_DEFAULT
Definition: SerialStreamBuf.h:118
SerialPort::CHAR_SIZE_6
6 bit characters.
Definition: SerialPort.h:87
LibSerial::SerialStreamBuf::FLOW_CONTROL_DEFAULT
Definition: SerialStreamBuf.h:134
LibSerial::SerialStreamBuf::BAUD_300
Definition: SerialStreamBuf.h:69
LibSerial::SerialStreamBuf::xsgetn
virtual std::streamsize xsgetn(char_type *s, std::streamsize n)
Reads upto n characters from the serial port and returns them through the character array located at ...
LibSerial::SerialStreamBuf::FLOW_CONTROL_INVALID
Invalid flow control setting.
Definition: SerialStreamBuf.h:135
LibSerial::SerialStreamBuf::operator=
SerialStreamBuf & operator=(const SerialStreamBuf &)
SerialPort::BAUD_150
Definition: SerialPort.h:61
SerialPort::BAUD_300
Definition: SerialPort.h:63
LibSerial::SerialStreamBuf::BAUD_75
Definition: SerialStreamBuf.h:64
LibSerial::SerialStreamBuf::close
SerialStreamBuf * close()
If is_open() == false, returns a null pointer.
Definition: SerialStreamBuf.h:597
LibSerial::SerialStreamBuf::underflow
virtual int_type underflow()
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
SerialPort::BAUD_50
Definition: SerialPort.h:57
LibSerial::SerialStreamBuf::NumOfStopBits
short NumOfStopBits() const
Get the number of stop bits being used during serial communication.
LibSerial::SerialStreamBuf::DEFAULT_NO_OF_STOP_BITS
static const short DEFAULT_NO_OF_STOP_BITS
The default number of stop bits used.
Definition: SerialStreamBuf.h:170
SerialPort::BAUD_200
Definition: SerialPort.h:62
LibSerial::SerialStreamBuf::PARITY_EVEN
Definition: SerialStreamBuf.h:115
SerialPort::PARITY_EVEN
Even parity.
Definition: SerialPort.h:100
LibSerial::SerialStreamBuf::DEFAULT_BAUD
static const BaudRateEnum DEFAULT_BAUD
The default value of the baud rate of the serial port.
Definition: SerialStreamBuf.h:153
SerialPort::BAUD_DEFAULT
Definition: SerialPort.h:82
LibSerial::SerialStreamBuf::CHAR_SIZE_6
Definition: SerialStreamBuf.h:99
LibSerial::SerialStreamBuf::BAUD_200
Definition: SerialStreamBuf.h:68
LibSerial::SerialStreamBuf::BAUD_600
Definition: SerialStreamBuf.h:70
LibSerial::SerialStreamBuf::BAUD_57600
Definition: SerialStreamBuf.h:78
LibSerial::SerialStreamBuf::CharSize
const CharSizeEnum CharSize() const
Return the character size currently being used for serial communication.
LibSerial::SerialStreamBuf::setbuf
virtual std::streambuf * setbuf(char_type *, std::streamsize)
Performs an operation that is defined separately for each class derived from streambuf.
Definition: SerialStreamBuf.h:590
LibSerial::SerialStreamBuf::pbackfail
virtual int_type pbackfail(int_type c=traits_type::eof())
This function is called when a putback of a character fails.
LibSerial::SerialStreamBuf::CTRL_S
static const char CTRL_S
Character used to signal that I/O should stop while using software flow control with the serial port...
Definition: SerialStreamBuf.h:432
LibSerial::SerialStreamBuf::SetFlowControl
const FlowControlEnum SetFlowControl(const FlowControlEnum flowControlType)
Use the specified flow control.
LibSerial::SerialStreamBuf::BAUD_INVALID
Definition: SerialStreamBuf.h:85
LibSerial::SerialStreamBuf::overflow
virtual int_type overflow(int_type c)
Writes the specified character to the associated serial port.
SerialPort::BAUD_2400
Definition: SerialPort.h:67
LibSerial::SerialStreamBuf::SerialStreamBuf
SerialStreamBuf()
The default constructor.
Definition: SerialStreamBuf.h:562
LibSerial::SerialStreamBuf::CHAR_SIZE_5
Definition: SerialStreamBuf.h:98
SerialPort.h
LibSerial::SerialStreamBuf::BAUD_134
Definition: SerialStreamBuf.h:66
SerialPort::BAUD_4800
Definition: SerialPort.h:68
LibSerial::SerialStreamBuf::FLOW_CONTROL_HARD
Definition: SerialStreamBuf.h:131
SerialPort::PARITY_ODD
Odd parity.
Definition: SerialPort.h:101