libserial 0.6.0rc1

SerialStreamBuf.h

Go to the documentation of this file.
00001 /*
00002  * Time-stamp: <02-Nov-2008 14:52:35 pagey>
00003  *
00004  * $Id: SerialStreamBuf.h,v 1.9 2005-10-17 00:19:12 crayzeewulf Exp $
00005  *
00006  *
00007  */
00008 #ifndef _SerialStreamBuf_h_
00009 #define _SerialStreamBuf_h_
00010 
00011 #include <termios.h>
00012 #include <unistd.h>
00013 #include <iosfwd>
00014 #include <streambuf>
00015 #include <string>
00016 #include <SerialPort.h>
00017 
00018 extern "C++" {
00019     namespace LibSerial {
00038         class SerialStreamBuf : public std::streambuf {
00039         public:
00043 
00045 
00062             enum BaudRateEnum {
00063                 BAUD_50    = SerialPort::BAUD_50,
00064                 BAUD_75    = SerialPort::BAUD_75,
00065                 BAUD_110   = SerialPort::BAUD_110,
00066                 BAUD_134   = SerialPort::BAUD_134,
00067                 BAUD_150   = SerialPort::BAUD_150,
00068                 BAUD_200   = SerialPort::BAUD_200,
00069                 BAUD_300   = SerialPort::BAUD_300,
00070                 BAUD_600   = SerialPort::BAUD_600,
00071                 BAUD_1200  = SerialPort::BAUD_1200,
00072                 BAUD_1800  = SerialPort::BAUD_1800,
00073                 BAUD_2400  = SerialPort::BAUD_2400,
00074                 BAUD_4800  = SerialPort::BAUD_4800,
00075                 BAUD_9600  = SerialPort::BAUD_9600,
00076                 BAUD_19200 = SerialPort::BAUD_19200,
00077                 BAUD_38400 = SerialPort::BAUD_38400,
00078                 BAUD_57600 = SerialPort::BAUD_57600,
00079                 BAUD_115200 = SerialPort::BAUD_115200,
00080                 BAUD_230400 = SerialPort::BAUD_230400,
00081 #ifdef __linux__
00082                 BAUD_460800 = SerialPort::BAUD_460800,
00083 #endif
00084                 BAUD_DEFAULT = SerialPort::BAUD_DEFAULT,
00085                 BAUD_INVALID
00086             } ;
00087 
00097             enum CharSizeEnum {
00098                 CHAR_SIZE_5 = SerialPort::CHAR_SIZE_5,
00099                 CHAR_SIZE_6 = SerialPort::CHAR_SIZE_6,
00100                 CHAR_SIZE_7 = SerialPort::CHAR_SIZE_7,
00101                 CHAR_SIZE_8 = SerialPort::CHAR_SIZE_8,
00102                 CHAR_SIZE_DEFAULT = SerialPort::CHAR_SIZE_DEFAULT,
00103                 CHAR_SIZE_INVALID
00104             } ;
00105 
00114             enum ParityEnum {
00115                 PARITY_EVEN = SerialPort::PARITY_EVEN,
00116                 PARITY_ODD  = SerialPort::PARITY_ODD,
00117                 PARITY_NONE = SerialPort::PARITY_NONE,
00118                 PARITY_DEFAULT = SerialPort::PARITY_DEFAULT,
00119                 PARITY_INVALID   
00120             } ;
00121 
00130             enum FlowControlEnum {
00131                 FLOW_CONTROL_HARD    = SerialPort::FLOW_CONTROL_HARD,
00132                 FLOW_CONTROL_SOFT    = SerialPort::FLOW_CONTROL_SOFT,
00133                 FLOW_CONTROL_NONE    = SerialPort::FLOW_CONTROL_NONE,
00134                 FLOW_CONTROL_DEFAULT = SerialPort::FLOW_CONTROL_DEFAULT,
00135                 FLOW_CONTROL_INVALID 
00136             } ;
00138 
00139             /* ------------------------------------------------------------
00140              * Public Static Members
00141              * ------------------------------------------------------------
00142              */
00153             static const BaudRateEnum DEFAULT_BAUD ;
00154 
00162             static const CharSizeEnum DEFAULT_CHAR_SIZE ;
00163 
00170             static const short DEFAULT_NO_OF_STOP_BITS ;
00171 
00178             static const ParityEnum DEFAULT_PARITY ;
00179       
00186             static const FlowControlEnum DEFAULT_FLOW_CONTROL ;
00187 
00196             static const short DEFAULT_VMIN ;
00197 
00206             static const short DEFAULT_VTIME ;
00207 
00209 
00210 
00217             SerialStreamBuf() ;
00218 
00222             ~SerialStreamBuf() ;
00224 
00233             bool is_open() const ;
00234 
00282             SerialStreamBuf* open( const std::string filename,
00283                                    std::ios_base::openmode mode =
00284                                    std::ios_base::in | std::ios_base::out ) ;
00285 
00306             SerialStreamBuf* close() ;
00307 
00312             int SetParametersToDefault() ;
00313 
00319             const BaudRateEnum SetBaudRate(const BaudRateEnum baudRate ) ;
00320 
00326             const BaudRateEnum BaudRate() const ;
00327 
00333             const CharSizeEnum SetCharSize(const CharSizeEnum charSize) ;
00334 
00339             const CharSizeEnum CharSize() const ;
00340 
00348             short SetNumOfStopBits(short numOfStopBits) ;
00349 
00355             short NumOfStopBits() const ; 
00356 
00362             const ParityEnum SetParity(const ParityEnum parityType) ;
00363 
00369             const ParityEnum Parity() const ;
00370 
00374             const FlowControlEnum SetFlowControl(const FlowControlEnum flowControlType) ;
00375 
00379             const FlowControlEnum FlowControl() const ;
00380 
00385             const short SetVMin( short vtime ) ;
00386 
00391             const short VMin() const;
00392 
00397             const short SetVTime( short vtime ) ;
00398 
00403             const short VTime() const;
00404 
00406 
00410 
00412 
00413             /* ------------------------------------------------------------
00414              * Friends
00415              * ------------------------------------------------------------
00416              */
00417         protected:
00418             /* ------------------------------------------------------------
00419              * Protected Data Members
00420              * ------------------------------------------------------------
00421              */
00426             static const char CTRL_Q = 0x11 ;
00427       
00432             static const char CTRL_S = 0x13 ;
00433             /* ------------------------------------------------------------
00434              * Protected Methods
00435              * ------------------------------------------------------------
00436              */
00450             virtual std::streambuf* setbuf( char_type*, 
00451                                             std::streamsize ) ;
00452 
00459             virtual std::streamsize xsgetn( char_type*      s, 
00460                                             std::streamsize n ) ;
00461 
00471             virtual std::streamsize showmanyc();
00472 
00480             virtual int_type underflow() ;
00481 
00490             virtual int_type   uflow() ;
00491 
00498             virtual int_type pbackfail(int_type c = traits_type::eof()) ;
00499 
00506             virtual std::streamsize xsputn( const char_type* s, 
00507                                             std::streamsize  n ) ;
00508 
00514             virtual int_type overflow(int_type c) ;
00515 
00516         private:
00517             /* ------------------------------------------------------------
00518              * Private Data Members
00519              * ------------------------------------------------------------
00520              */
00521             //
00522             // The copy constructor and the assignment operator are
00523             // declared private but never defined. This allows the
00524             // compiler to catch attempts to copy instances of this
00525             // class.
00526             //
00527             SerialStreamBuf( const SerialStreamBuf& ) ;
00528             SerialStreamBuf& operator=( const SerialStreamBuf& ) ;
00529 
00536             char mPutbackChar ;
00537 
00541             bool mPutbackAvailable ;
00542       
00546             int mFileDescriptor ;
00547             /* ------------------------------------------------------------
00548              * Private Methods
00549              * ------------------------------------------------------------
00550              */
00558             int InitializeSerialPort() ;
00559         } ; // class SerialStreamBuf
00560 
00561         inline 
00562         SerialStreamBuf::SerialStreamBuf() :
00563             mPutbackChar(0),
00564             mPutbackAvailable(false),
00565             mFileDescriptor(-1)
00566         {
00567             setbuf(0, 0) ;
00568             return ;
00569         }
00570 
00571         inline 
00572         SerialStreamBuf::~SerialStreamBuf() 
00573         {
00574             if( this->is_open() ) 
00575             {
00576                 this->close() ;
00577             }
00578             return ;
00579         }
00580 
00581         inline
00582         bool
00583         SerialStreamBuf::is_open() const 
00584         {
00585             return (-1 != mFileDescriptor) ;
00586         }
00587     
00588         inline
00589         std::streambuf* 
00590         SerialStreamBuf::setbuf(char_type *, std::streamsize) 
00591         {
00592             return std::streambuf::setbuf(0, 0) ;
00593         }
00594 
00595         inline
00596         SerialStreamBuf*
00597         SerialStreamBuf::close() 
00598         {
00599             //
00600             // Return a null pointer if the serial port is not currently open. 
00601             //
00602             if( this->is_open() == false ) {
00603                 return 0 ;
00604             }
00605             //
00606             // Otherwise, close the serial port and set the file descriptor
00607             // to an invalid value.
00608             //
00609             if( -1 == ::close(mFileDescriptor) ) {
00610                 //
00611                 // If the close failed then return a null pointer. 
00612                 //
00613                 return 0 ;
00614             } else {
00615                 //
00616                 // Set the file descriptor to an invalid value, -1. 
00617                 //
00618                 mFileDescriptor = -1 ;
00619                 //
00620                 // On success, return "this" as required by the C++ standard.
00621                 //
00622                 return this ;
00623             }
00624         }
00625     
00626         inline
00627         std::streambuf::int_type
00628         SerialStreamBuf::uflow() 
00629         {
00630             int_type next_ch = underflow() ;
00631             mPutbackAvailable = false ;
00632             return next_ch ;
00633         }
00634 
00635     } // namespace LibSerial
00636 } // extern "C++"
00637 #endif // #ifndef _SerialStreamBuf_h_