00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _SerialPort_h_
00021 #define _SerialPort_h_
00022
00023
00024 #include <string>
00025 #include <vector>
00026 #include <stdexcept>
00027 #include <termios.h>
00028
00029
00050 class SerialPort
00051 {
00052 public:
00056 enum BaudRate {
00057 BAUD_50 = B50,
00058 BAUD_75 = B75,
00059 BAUD_110 = B110,
00060 BAUD_134 = B134,
00061 BAUD_150 = B150,
00062 BAUD_200 = B200,
00063 BAUD_300 = B300,
00064 BAUD_600 = B600,
00065 BAUD_1200 = B1200,
00066 BAUD_1800 = B1800,
00067 BAUD_2400 = B2400,
00068 BAUD_4800 = B4800,
00069 BAUD_9600 = B9600,
00070 BAUD_19200 = B19200,
00071 BAUD_38400 = B38400,
00072 BAUD_57600 = B57600,
00073 BAUD_115200 = B115200,
00074 BAUD_230400 = B230400,
00075
00076
00077
00078
00079 #ifdef __linux__
00080 BAUD_460800 = B460800,
00081 #endif
00082 BAUD_DEFAULT = BAUD_57600
00083 } ;
00084
00085 enum CharacterSize {
00086 CHAR_SIZE_5 = CS5,
00087 CHAR_SIZE_6 = CS6,
00088 CHAR_SIZE_7 = CS7,
00089 CHAR_SIZE_8 = CS8,
00090 CHAR_SIZE_DEFAULT = CHAR_SIZE_8
00091 } ;
00092
00093 enum StopBits {
00094 STOP_BITS_1,
00095 STOP_BITS_2,
00096 STOP_BITS_DEFAULT = STOP_BITS_1
00097 } ;
00098
00099 enum Parity {
00100 PARITY_EVEN,
00101 PARITY_ODD,
00102 PARITY_NONE,
00103 PARITY_DEFAULT = PARITY_NONE
00104 } ;
00105
00106 enum FlowControl {
00107 FLOW_CONTROL_HARD,
00108 FLOW_CONTROL_SOFT,
00109 FLOW_CONTROL_NONE,
00110 FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
00111 } ;
00112
00113 class NotOpen : public std::logic_error
00114 {
00115 public:
00116 NotOpen(const std::string& whatArg) :
00117 logic_error(whatArg) { }
00118 } ;
00119
00120 class OpenFailed : public std::runtime_error
00121 {
00122 public:
00123 OpenFailed(const std::string& whatArg) :
00124 runtime_error(whatArg) { }
00125 } ;
00126
00127 class AlreadyOpen : public std::logic_error
00128 {
00129 public:
00130 AlreadyOpen( const std::string& whatArg ) :
00131 logic_error(whatArg) { }
00132 } ;
00133
00134 class UnsupportedBaudRate : public std::runtime_error
00135 {
00136 public:
00137 UnsupportedBaudRate( const std::string& whatArg ) :
00138 runtime_error(whatArg) { }
00139 } ;
00140
00141 class ReadTimeout : public std::runtime_error
00142 {
00143 public:
00144 ReadTimeout() : runtime_error( "Read timeout" ) { }
00145 } ;
00146
00150 explicit SerialPort( const std::string& serialPortName ) ;
00151
00155 virtual ~SerialPort() throw() ;
00156
00170 void
00171 Open( const BaudRate baudRate = BAUD_DEFAULT,
00172 const CharacterSize charSize = CHAR_SIZE_DEFAULT,
00173 const Parity parityType = PARITY_DEFAULT,
00174 const StopBits stopBits = STOP_BITS_DEFAULT,
00175 const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
00176 throw( AlreadyOpen,
00177 OpenFailed,
00178 UnsupportedBaudRate,
00179 std::invalid_argument ) ;
00180
00184 bool
00185 IsOpen() const ;
00186
00195 void
00196 Close()
00197 throw(NotOpen) ;
00198
00209 void
00210 SetBaudRate( const BaudRate baudRate )
00211 throw( UnsupportedBaudRate,
00212 NotOpen,
00213 std::invalid_argument ) ;
00214
00221 BaudRate
00222 GetBaudRate() const
00223 throw( NotOpen,
00224 std::runtime_error ) ;
00225
00235 void
00236 SetCharSize( const CharacterSize charSize )
00237 throw( NotOpen,
00238 std::invalid_argument ) ;
00246 CharacterSize
00247 GetCharSize() const
00248 throw(NotOpen) ;
00249
00259 void
00260 SetParity( const Parity parityType )
00261 throw( NotOpen,
00262 std::invalid_argument ) ;
00263
00271 Parity
00272 GetParity() const
00273 throw(NotOpen) ;
00274
00284 void
00285 SetNumOfStopBits( const StopBits numOfStopBits )
00286 throw( NotOpen,
00287 std::invalid_argument ) ;
00288
00297 StopBits
00298 GetNumOfStopBits() const
00299 throw(NotOpen) ;
00300
00310 void
00311 SetFlowControl( const FlowControl flowControl )
00312 throw( NotOpen,
00313 std::invalid_argument ) ;
00314
00322 FlowControl
00323 GetFlowControl() const
00324 throw( NotOpen ) ;
00325
00333 bool
00334 IsDataAvailable() const
00335 throw(NotOpen) ;
00336
00343 unsigned char
00344 ReadByte( const unsigned int msTimeout = 0 )
00345 throw( NotOpen,
00346 ReadTimeout,
00347 std::runtime_error ) ;
00348
00359 typedef std::vector<unsigned char> DataBuffer ;
00360 void
00361 Read( DataBuffer& dataBuffer,
00362 const unsigned int numOfBytes = 0,
00363 const unsigned int msTimeout = 0 )
00364 throw( NotOpen,
00365 ReadTimeout,
00366 std::runtime_error ) ;
00367
00368
00372 const std::string
00373 ReadLine( const unsigned int msTimeout = 0,
00374 const char lineTerminator = '\n' )
00375 throw( NotOpen,
00376 ReadTimeout,
00377 std::runtime_error ) ;
00378
00385 void
00386 WriteByte(const unsigned char dataByte)
00387 throw( NotOpen,
00388 std::runtime_error ) ;
00389
00393 void
00394 Write(const DataBuffer& dataBuffer)
00395 throw( NotOpen,
00396 std::runtime_error ) ;
00397
00401 void
00402 Write(const std::string& dataString)
00403 throw( NotOpen,
00404 std::runtime_error ) ;
00405
00409 void
00410 SetDtr( const bool dtrState = true )
00411 throw( NotOpen,
00412 std::runtime_error ) ;
00413
00417 bool
00418 GetDtr() const
00419 throw( NotOpen,
00420 std::runtime_error ) ;
00421
00425 void
00426 SetRts( const bool rtsState = true )
00427 throw( NotOpen,
00428 std::runtime_error ) ;
00429
00433 bool
00434 GetRts() const
00435 throw( NotOpen,
00436 std::runtime_error ) ;
00437
00438
00439
00440
00441
00442
00443 bool
00444 GetCts() const
00445 throw( NotOpen,
00446 std::runtime_error ) ;
00447
00448
00449
00450
00451
00452
00453 bool
00454 GetDsr() const
00455 throw( NotOpen,
00456 std::runtime_error ) ;
00457 private:
00462 SerialPort( const SerialPort& otherSerialPort ) ;
00463
00468 SerialPort& operator=(const SerialPort& otherSerialPort ) ;
00469
00470
00471
00472
00473
00474 class SerialPortImpl ;
00475
00479 SerialPortImpl* mSerialPortImpl ;
00480 } ;
00481
00482 #endif // #ifndef _SerialPort_h_
00483