SerialStreamBuf.h

Go to the documentation of this file.
00001 /*
00002  * Time-stamp: <04/05/05 16:21:47 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 
00017 extern "C++" {
00018     namespace LibSerial {
00036         class SerialStreamBuf : public std::streambuf {
00037         public:
00041 
00043 
00054             enum BaudRateEnum {
00055                 BAUD_50    = B50,         
00056                 BAUD_75    = B75,         
00057                 BAUD_110   = B110,        
00058                 BAUD_134   = B134,        
00059                 BAUD_150   = B150,        
00060                 BAUD_200   = B200,        
00061                 BAUD_300   = B300,        
00062                 BAUD_600   = B600,        
00063                 BAUD_1200  = B1200,       
00064                 BAUD_1800  = B1800,       
00065                 BAUD_2400  = B2400,       
00066                 BAUD_4800  = B4800,       
00067                 BAUD_9600  = B9600,       
00068                 BAUD_19200 = B19200,      
00069                 BAUD_38400 = B38400,      
00070                 BAUD_57600 = B57600,      
00071                 BAUD_115200 = B115200,    
00072                 BAUD_INVALID              
00073             } ;
00074 
00079             enum CharSizeEnum {
00080                 CHAR_SIZE_5 = CS5, 
00081                 CHAR_SIZE_6 = CS6, 
00082                 CHAR_SIZE_7 = CS7, 
00083                 CHAR_SIZE_8 = CS8, 
00084                 CHAR_SIZE_INVALID  
00085             } ;
00086 
00091             enum ParityEnum {
00092                 PARITY_EVEN,     
00093                 PARITY_ODD,      
00094                 PARITY_NONE,     
00095                 PARITY_INVALID   
00096             } ;      
00097 
00102             enum FlowControlEnum {
00103                 FLOW_CONTROL_HARD,   
00104                 FLOW_CONTROL_SOFT,   
00105                 FLOW_CONTROL_NONE,   
00106                 FLOW_CONTROL_INVALID 
00107             } ;
00109 
00110             /* ------------------------------------------------------------
00111              * Public Static Members
00112              * ------------------------------------------------------------
00113              */
00121             static const BaudRateEnum DEFAULT_BAUD ;
00122 
00127             static const CharSizeEnum DEFAULT_CHAR_SIZE ;
00128 
00132             static const short DEFAULT_NO_OF_STOP_BITS ;
00133 
00137             static const ParityEnum DEFAULT_PARITY ;
00138       
00142             static const FlowControlEnum DEFAULT_FLOW_CONTROL ;
00143 
00147             static const short DEFAULT_VMIN ;
00148 
00152             static const short DEFAULT_VTIME ;
00153 
00155 
00156 
00160 
00162 
00169             SerialStreamBuf() ;
00170 
00174             ~SerialStreamBuf() ;
00176 
00185             bool is_open() const ;
00186 
00234             SerialStreamBuf* open( const std::string filename, 
00235                                    std::ios_base::openmode mode = 
00236                                    std::ios_base::in | std::ios_base::out ) ;
00237 
00258             SerialStreamBuf* close() ;
00259 
00264             int SetParametersToDefault() ;
00265 
00271             const BaudRateEnum SetBaudRate(const BaudRateEnum baud_rate) ;
00272 
00278             const BaudRateEnum BaudRate() const ;
00279 
00285             const CharSizeEnum SetCharSize(const CharSizeEnum char_size) ;
00286 
00291             const CharSizeEnum CharSize() const ;
00292 
00300             short SetNumOfStopBits(short stop_bits) ;
00301 
00307             short NumOfStopBits() const ; 
00308 
00314             const ParityEnum SetParity(const ParityEnum parity) ;
00315 
00321             const ParityEnum Parity() const ;
00322 
00326             const FlowControlEnum SetFlowControl(const FlowControlEnum flow_c) ;
00327 
00331             const FlowControlEnum FlowControl() const ;
00332 
00336             const short SetVMin( short vtime ) ;
00337 
00341             const short VMin() const;
00342 
00346             const short SetVTime( short vtime ) ;
00347 
00351             const short VTime() const;
00352 
00354 
00358 
00360 
00361             /* ------------------------------------------------------------
00362              * Friends
00363              * ------------------------------------------------------------
00364              */
00365         protected:
00366             /* ------------------------------------------------------------
00367              * Protected Data Members
00368              * ------------------------------------------------------------
00369              */
00374             static const char CTRL_Q = 0x11 ;
00375       
00380             static const char CTRL_S = 0x13 ;
00381             /* ------------------------------------------------------------
00382              * Protected Methods
00383              * ------------------------------------------------------------
00384              */
00398             virtual std::streambuf* setbuf( char_type*, 
00399                                             std::streamsize ) ;
00400 
00407             virtual std::streamsize xsgetn( char_type*      s, 
00408                                             std::streamsize n ) ;
00409 
00419             virtual std::streamsize showmanyc();
00420 
00428             virtual int_type underflow() ;
00429 
00438             virtual int_type   uflow() ;
00439 
00446             virtual int_type pbackfail(int_type c = traits_type::eof()) ;
00447 
00454             virtual std::streamsize xsputn( const char_type* s, 
00455                                             std::streamsize  n ) ;
00456 
00462             virtual int_type overflow(int_type c) ;
00463 
00464         private:
00465             /* ------------------------------------------------------------
00466              * Private Data Members
00467              * ------------------------------------------------------------
00468              */
00474             char mPutbackChar ;
00475 
00479             bool mPutbackAvailable ;
00480       
00484             int mFileDescriptor ;
00485             /* ------------------------------------------------------------
00486              * Private Methods
00487              * ------------------------------------------------------------
00488              */
00495             int InitializeSerialPort() ;
00496         } ; // class SerialStreamBuf
00497 
00498         inline 
00499         SerialStreamBuf::SerialStreamBuf() :
00500             mPutbackChar(0),
00501             mPutbackAvailable(false),
00502             mFileDescriptor(-1)
00503         {
00504             setbuf(0, 0) ;
00505             return ;
00506         }
00507 
00508         inline 
00509         SerialStreamBuf::~SerialStreamBuf() 
00510         {
00511             if( this->is_open() ) {
00512                 this->close() ;
00513             }
00514             return ;
00515         }
00516 
00517         inline
00518         bool
00519         SerialStreamBuf::is_open() const 
00520         {
00521             return (-1 != mFileDescriptor) ;
00522         }
00523     
00524         inline
00525         std::streambuf* 
00526         SerialStreamBuf::setbuf(char_type *, std::streamsize) 
00527         {
00528             return std::streambuf::setbuf(0, 0) ;
00529         }
00530 
00531         inline
00532         SerialStreamBuf*
00533         SerialStreamBuf::close() 
00534         {
00535             //
00536             // Return a null pointer if the serial port is not currently open. 
00537             //
00538             if( this->is_open() == false ) {
00539                 return 0 ;
00540             }
00541             //
00542             // Otherwise, close the serial port and set the file descriptor
00543             // to an invalid value.
00544             //
00545             if( -1 == ::close(mFileDescriptor) ) {
00546                 //
00547                 // If the close failed then return a null pointer. 
00548                 //
00549                 return 0 ;
00550             } else {
00551                 //
00552                 // Set the file descriptor to an invalid value, -1. 
00553                 //
00554                 mFileDescriptor = -1 ;
00555                 //
00556                 // On success, return "this" as required by the C++ standard.
00557                 //
00558                 return this ;
00559             }
00560         }
00561     
00562         inline
00563         std::streambuf::int_type
00564         SerialStreamBuf::uflow() 
00565         {
00566             int_type next_ch = underflow() ;
00567             mPutbackAvailable = false ;
00568             return next_ch ;
00569         }
00570 
00571     } ; // namespace LibSerial
00572 } // extern "C++"
00573 #endif // #ifndef _SerialStreamBuf_h_

Generated on Tue Jan 22 17:27:35 2008 for libserial by  doxygen 1.5.4