00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _SerialStreamBuf_h_
00009 #define _SerialStreamBuf_h_
00010
00011 #ifndef _termios_h_INCLUDED_
00012 # include <termios.h>
00013 # define _termios_h_INCLUDED_
00014 #endif
00015
00016 #ifndef _unistd_h_INCLUDED_
00017 # include <unistd.h>
00018 # define _unistd_h_INCLUDED_
00019 #endif
00020
00021 #ifndef _std_iosfwd_INCLUDED_
00022 # include <iosfwd>
00023 # define _std_iosfwd_INCLUDED_
00024 #endif
00025
00026 #ifndef _std_streambuf_INCLUDED_
00027 # include <streambuf>
00028 # define _std_streambuf_INCLUDED_
00029 #endif
00030
00031 #ifndef _std_string_INCLUDED_
00032 # include <string>
00033 # define _std_string_INCLUDED_
00034 #endif
00035
00036 extern "C++" {
00037 namespace LibSerial {
00055 class SerialStreamBuf : public std::streambuf {
00056 public:
00060
00062
00073 enum BaudRateEnum {
00074 BAUD_50 = B50,
00075 BAUD_75 = B75,
00076 BAUD_110 = B110,
00077 BAUD_134 = B134,
00078 BAUD_150 = B150,
00079 BAUD_200 = B200,
00080 BAUD_300 = B300,
00081 BAUD_600 = B600,
00082 BAUD_1200 = B1200,
00083 BAUD_1800 = B1800,
00084 BAUD_2400 = B2400,
00085 BAUD_4800 = B4800,
00086 BAUD_9600 = B9600,
00087 BAUD_19200 = B19200,
00088 BAUD_38400 = B38400,
00089 BAUD_57600 = B57600,
00090 BAUD_115200 = B115200,
00091 BAUD_INVALID
00092 } ;
00093
00098 enum CharSizeEnum {
00099 CHAR_SIZE_5 = CS5,
00100 CHAR_SIZE_6 = CS6,
00101 CHAR_SIZE_7 = CS7,
00102 CHAR_SIZE_8 = CS8,
00103 CHAR_SIZE_INVALID
00104 } ;
00105
00110 enum ParityEnum {
00111 PARITY_EVEN,
00112 PARITY_ODD,
00113 PARITY_NONE,
00114 PARITY_INVALID
00115 } ;
00116
00121 enum FlowControlEnum {
00122 FLOW_CONTROL_HARD,
00123 FLOW_CONTROL_SOFT,
00124 FLOW_CONTROL_NONE,
00125 FLOW_CONTROL_INVALID
00126 } ;
00128
00129
00130
00131
00132
00140 static const BaudRateEnum DEFAULT_BAUD ;
00141
00146 static const CharSizeEnum DEFAULT_CHAR_SIZE ;
00147
00151 static const short DEFAULT_NO_OF_STOP_BITS ;
00152
00156 static const ParityEnum DEFAULT_PARITY ;
00157
00161 static const FlowControlEnum DEFAULT_FLOW_CONTROL ;
00162
00166 static const short DEFAULT_VMIN ;
00167
00171 static const short DEFAULT_VTIME ;
00172
00174
00175
00179
00181
00188 SerialStreamBuf() ;
00189
00193 ~SerialStreamBuf() ;
00195
00204 bool is_open() const ;
00205
00253 SerialStreamBuf* open( const std::string filename,
00254 std::ios_base::openmode mode =
00255 std::ios_base::in | std::ios_base::out ) ;
00256
00277 SerialStreamBuf* close() ;
00278
00283 int SetParametersToDefault() ;
00284
00290 const BaudRateEnum SetBaudRate(const BaudRateEnum baud_rate) ;
00291
00297 const BaudRateEnum BaudRate() const ;
00298
00304 const CharSizeEnum SetCharSize(const CharSizeEnum char_size) ;
00305
00310 const CharSizeEnum CharSize() const ;
00311
00319 short SetNumOfStopBits(short stop_bits) ;
00320
00326 short NumOfStopBits() const ;
00327
00333 const ParityEnum SetParity(const ParityEnum parity) ;
00334
00340 const ParityEnum Parity() const ;
00341
00345 const FlowControlEnum SetFlowControl(const FlowControlEnum flow_c) ;
00346
00350 const FlowControlEnum FlowControl() const ;
00351
00355 const short SetVMin( short vtime ) ;
00356
00360 const short VMin() const;
00361
00365 const short SetVTime( short vtime ) ;
00366
00370 const short VTime() const;
00371
00373
00377
00379
00380
00381
00382
00383
00384 protected:
00385
00386
00387
00388
00393 static const char CTRL_Q = 0x11 ;
00394
00399 static const char CTRL_S = 0x13 ;
00400
00401
00402
00403
00417 virtual std::streambuf* setbuf( char_type*,
00418 std::streamsize ) ;
00419
00426 virtual std::streamsize xsgetn( char_type* s,
00427 std::streamsize n ) ;
00428
00438 virtual std::streamsize showmanyc();
00439
00447 virtual int_type underflow() ;
00448
00457 virtual int_type uflow() ;
00458
00465 virtual int_type pbackfail(int_type c = traits_type::eof()) ;
00466
00473 virtual std::streamsize xsputn( const char_type* s,
00474 std::streamsize n ) ;
00475
00481 virtual int_type overflow(int_type c) ;
00482
00483 private:
00484
00485
00486
00487
00493 char mPutbackChar ;
00494
00498 bool mPutbackAvailable ;
00499
00503 int mFileDescriptor ;
00504
00505
00506
00507
00514 int InitializeSerialPort() ;
00515 } ;
00516
00517 inline
00518 SerialStreamBuf::SerialStreamBuf() :
00519 mPutbackChar(0),
00520 mPutbackAvailable(false),
00521 mFileDescriptor(-1)
00522 {
00523 setbuf(0, 0) ;
00524 return ;
00525 }
00526
00527 inline
00528 SerialStreamBuf::~SerialStreamBuf()
00529 {
00530 if( this->is_open() ) {
00531 this->close() ;
00532 }
00533 return ;
00534 }
00535
00536 inline
00537 bool
00538 SerialStreamBuf::is_open() const
00539 {
00540 return (-1 != mFileDescriptor) ;
00541 }
00542
00543 inline
00544 std::streambuf*
00545 SerialStreamBuf::setbuf(char_type *, std::streamsize)
00546 {
00547 return std::streambuf::setbuf(0, 0) ;
00548 }
00549
00550 inline
00551 SerialStreamBuf*
00552 SerialStreamBuf::close()
00553 {
00554
00555
00556
00557 if( this->is_open() == false ) {
00558 return 0 ;
00559 }
00560
00561
00562
00563
00564 if( -1 == ::close(mFileDescriptor) ) {
00565
00566
00567
00568 return 0 ;
00569 } else {
00570
00571
00572
00573 mFileDescriptor = -1 ;
00574
00575
00576
00577 return this ;
00578 }
00579 }
00580
00581 inline
00582 std::streambuf::int_type
00583 SerialStreamBuf::uflow()
00584 {
00585 int_type next_ch = underflow() ;
00586 mPutbackAvailable = false ;
00587 return next_ch ;
00588 }
00589
00590 } ;
00591 }
00592 #endif // #ifndef _SerialStreamBuf_h_