00001 /***************************************************************************
00002 * Copyright (C) 2004 by Manish Pagey *
00003 * crayzeewulf@users.sourceforge.net
00004 * *
00005 * This program is free software; you can redistribute it and/or modify *
00006 * it under the terms of the GNU General Public License as published by *
00007 * the Free Software Foundation; either version 2 of the License, or *
00008 * (at your option) any later version. *
00009 * *
00010 * This program is distributed in the hope that it will be useful, *
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
00013 * GNU General Public License for more details. *
00014 * *
00015 * You should have received a copy of the GNU General Public License *
00016 * along with this program; if not, write to the *
00017 * Free Software Foundation, Inc., *
00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
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
00043 class SerialPort
00044 {
00045 public:
00049 enum BaudRate {
00050 BAUD_50 = B50,
00051 BAUD_75 = B75,
00052 BAUD_110 = B110,
00053 BAUD_134 = B134,
00054 BAUD_150 = B150,
00055 BAUD_200 = B200,
00056 BAUD_300 = B300,
00057 BAUD_600 = B600,
00058 BAUD_1200 = B1200,
00059 BAUD_1800 = B1800,
00060 BAUD_2400 = B2400,
00061 BAUD_4800 = B4800,
00062 BAUD_9600 = B9600,
00063 BAUD_19200 = B19200,
00064 BAUD_38400 = B38400,
00065 BAUD_57600 = B57600,
00066 BAUD_115200 = B115200,
00067 BAUD_230400 = B230400,
00068 BAUD_460800 = B460800,
00069 BAUD_DEFAULT = BAUD_57600
00070 } ;
00071
00072 enum CharacterSize {
00073 CHAR_SIZE_5 = CS5,
00074 CHAR_SIZE_6 = CS6,
00075 CHAR_SIZE_7 = CS7,
00076 CHAR_SIZE_8 = CS8,
00077 CHAR_SIZE_DEFAULT = CHAR_SIZE_8
00078 } ;
00079
00080 enum StopBits {
00081 STOP_BITS_1,
00082 STOP_BITS_2,
00083 STOP_BITS_DEFAULT = STOP_BITS_1
00084 } ;
00085
00086 enum Parity {
00087 PARITY_EVEN,
00088 PARITY_ODD,
00089 PARITY_NONE,
00090 PARITY_DEFAULT = PARITY_NONE
00091 } ;
00092
00093 enum FlowControl {
00094 FLOW_CONTROL_HARD,
00095 // FLOW_CONTROL_SOFT,
00096 FLOW_CONTROL_NONE,
00097 FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
00098 } ;
00099
00100 class NotOpen : public std::logic_error
00101 {
00102 public:
00103 NotOpen(const std::string& whatArg) :
00104 logic_error(whatArg) { }
00105 } ;
00106
00107 class OpenFailed : public std::runtime_error
00108 {
00109 public:
00110 OpenFailed(const std::string& whatArg) :
00111 runtime_error(whatArg) { }
00112 } ;
00113
00114 class AlreadyOpen : public std::logic_error
00115 {
00116 public:
00117 AlreadyOpen( const std::string& whatArg ) :
00118 logic_error(whatArg) { }
00119 } ;
00120
00121 class UnsupportedBaudRate : public std::runtime_error
00122 {
00123 public:
00124 UnsupportedBaudRate( const std::string& whatArg ) :
00125 runtime_error(whatArg) { }
00126 } ;
00127
00128 class ReadTimeout : public std::runtime_error
00129 {
00130 public:
00131 ReadTimeout() : runtime_error( "Read timeout" ) { }
00132 } ;
00133
00137 explicit SerialPort( const std::string& serialPortName ) ;
00138
00142 ~SerialPort() throw() ;
00143
00157 void
00158 Open( const BaudRate baudRate = BAUD_DEFAULT,
00159 const CharacterSize charSize = CHAR_SIZE_DEFAULT,
00160 const Parity parityType = PARITY_DEFAULT,
00161 const StopBits stopBits = STOP_BITS_DEFAULT,
00162 const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
00163 throw( AlreadyOpen,
00164 OpenFailed,
00165 UnsupportedBaudRate,
00166 std::invalid_argument ) ;
00167
00171 bool
00172 IsOpen() const ;
00173
00182 void
00183 Close()
00184 throw(NotOpen) ;
00185
00196 void
00197 SetBaudRate( const BaudRate baudRate )
00198 throw( UnsupportedBaudRate,
00199 NotOpen,
00200 std::invalid_argument ) ;
00201
00208 BaudRate
00209 GetBaudRate() const
00210 throw( NotOpen,
00211 std::runtime_error ) ;
00212
00222 void
00223 SetCharSize( const CharacterSize charSize )
00224 throw( NotOpen,
00225 std::invalid_argument ) ;
00233 CharacterSize
00234 GetCharSize() const
00235 throw(NotOpen) ;
00236
00246 void
00247 SetParity( const Parity parityType )
00248 throw( NotOpen,
00249 std::invalid_argument ) ;
00250
00258 Parity
00259 GetParity() const
00260 throw(NotOpen) ;
00261
00271 void
00272 SetNumOfStopBits( const StopBits numOfStopBits )
00273 throw( NotOpen,
00274 std::invalid_argument ) ;
00275
00284 StopBits
00285 GetNumOfStopBits() const
00286 throw(NotOpen) ;
00287
00297 void
00298 SetFlowControl( const FlowControl flowControl )
00299 throw( NotOpen,
00300 std::invalid_argument ) ;
00301
00309 FlowControl
00310 GetFlowControl() const
00311 throw( NotOpen ) ;
00312
00320 bool
00321 IsDataAvailable() const
00322 throw(NotOpen) ;
00323
00330 unsigned char
00331 ReadByte( const unsigned int msTimeout = 0 )
00332 throw( NotOpen,
00333 ReadTimeout,
00334 std::runtime_error ) ;
00335
00346 typedef std::vector<unsigned char> DataBuffer ;
00347 void
00348 Read( DataBuffer& dataBuffer,
00349 const unsigned int numOfBytes = 0,
00350 const unsigned int msTimeout = 0 )
00351 throw( NotOpen,
00352 ReadTimeout,
00353 std::runtime_error ) ;
00354
00355
00359 const std::string
00360 ReadLine( const unsigned int msTimeout = 0,
00361 const char lineTerminator = 'n' )
00362 throw( NotOpen,
00363 ReadTimeout,
00364 std::runtime_error ) ;
00365
00372 void
00373 WriteByte(const unsigned char dataByte)
00374 throw( NotOpen,
00375 std::runtime_error ) ;
00376
00380 void
00381 Write(const DataBuffer& dataBuffer)
00382 throw( NotOpen,
00383 std::runtime_error ) ;
00384
00388 void
00389 Write(const std::string& dataString)
00390 throw( NotOpen,
00391 std::runtime_error ) ;
00392 private:
00393 SerialPort( const SerialPort& otherSerialPort ) ;
00394 SerialPort& operator=(const SerialPort& otherSerialPort ) ;
00395 class SerialPortImpl ;
00396 SerialPortImpl* mSerialPortImpl ;
00397 } ;
00398
00399 #endif // #ifndef _SerialPort_h_
00400