SerialPort.h

Go to the documentation of this file.
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         //
00069         // Bug#1318912
00070         // B460800 is defined on Linux but not on Mac OS X. What about other
00071         // operating systems ?
00072         //
00073 #ifdef __linux__       
00074         BAUD_460800  = B460800,
00075 #endif
00076         BAUD_DEFAULT = BAUD_57600
00077     } ;
00078 
00079     enum CharacterSize {
00080         CHAR_SIZE_5  = CS5, 
00081         CHAR_SIZE_6  = CS6, 
00082         CHAR_SIZE_7  = CS7, 
00083         CHAR_SIZE_8  = CS8, 
00084         CHAR_SIZE_DEFAULT = CHAR_SIZE_8
00085     } ;
00086 
00087     enum StopBits {
00088         STOP_BITS_1,   
00089         STOP_BITS_2,   
00090         STOP_BITS_DEFAULT = STOP_BITS_1
00091     } ;
00092 
00093     enum Parity {
00094         PARITY_EVEN,     
00095         PARITY_ODD,      
00096         PARITY_NONE,     
00097         PARITY_DEFAULT = PARITY_NONE
00098     } ;
00099 
00100     enum FlowControl {
00101         FLOW_CONTROL_HARD,
00102         // FLOW_CONTROL_SOFT,
00103         FLOW_CONTROL_NONE,
00104         FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
00105     } ;
00106 
00107     class NotOpen : public std::logic_error
00108     {
00109     public:
00110         NotOpen(const std::string& whatArg) :
00111             logic_error(whatArg) { }
00112     } ;
00113 
00114     class OpenFailed : public std::runtime_error
00115     {
00116     public:
00117         OpenFailed(const std::string& whatArg) :
00118             runtime_error(whatArg) { }
00119     } ;
00120 
00121     class AlreadyOpen : public std::logic_error
00122     {
00123     public:
00124         AlreadyOpen( const std::string& whatArg ) :
00125             logic_error(whatArg) { }
00126     } ;
00127 
00128     class UnsupportedBaudRate : public std::runtime_error
00129     {
00130     public:
00131         UnsupportedBaudRate( const std::string& whatArg ) :
00132             runtime_error(whatArg) { }
00133     } ;
00134 
00135     class ReadTimeout : public std::runtime_error
00136     {
00137     public:
00138         ReadTimeout() : runtime_error( "Read timeout" ) { }
00139     } ;
00140 
00144     explicit SerialPort( const std::string& serialPortName ) ;
00145 
00149     ~SerialPort() throw() ;
00150 
00164     void
00165     Open( const BaudRate      baudRate    = BAUD_DEFAULT,
00166           const CharacterSize charSize    = CHAR_SIZE_DEFAULT,
00167           const Parity        parityType  = PARITY_DEFAULT,
00168           const StopBits      stopBits    = STOP_BITS_DEFAULT,
00169           const FlowControl   flowControl = FLOW_CONTROL_DEFAULT )
00170         throw( AlreadyOpen,
00171                OpenFailed,
00172                UnsupportedBaudRate,
00173                std::invalid_argument ) ;
00174 
00178     bool
00179     IsOpen() const ;
00180 
00189     void
00190     Close()
00191         throw(NotOpen) ;
00192 
00203     void
00204     SetBaudRate( const BaudRate baudRate )
00205         throw( UnsupportedBaudRate,
00206                NotOpen,
00207                std::invalid_argument ) ;
00208 
00215     BaudRate
00216     GetBaudRate() const
00217         throw( NotOpen,
00218                std::runtime_error ) ;
00219 
00229     void
00230     SetCharSize( const CharacterSize charSize )
00231         throw( NotOpen,
00232                std::invalid_argument ) ;
00240     CharacterSize
00241     GetCharSize() const
00242         throw(NotOpen) ;
00243 
00253     void
00254     SetParity( const Parity parityType )
00255         throw( NotOpen,
00256                std::invalid_argument ) ;
00257 
00265     Parity
00266     GetParity() const
00267         throw(NotOpen) ;
00268 
00278     void
00279     SetNumOfStopBits( const StopBits numOfStopBits )
00280         throw( NotOpen,
00281                std::invalid_argument ) ;
00282 
00291     StopBits
00292     GetNumOfStopBits() const
00293         throw(NotOpen) ;
00294 
00304     void
00305     SetFlowControl( const FlowControl   flowControl )
00306         throw( NotOpen,
00307                std::invalid_argument ) ;
00308 
00316     FlowControl
00317     GetFlowControl() const
00318         throw( NotOpen ) ;
00319 
00327     bool
00328     IsDataAvailable() const
00329         throw(NotOpen) ;
00330 
00337     unsigned char
00338     ReadByte( const unsigned int msTimeout = 0 )
00339         throw( NotOpen,
00340                ReadTimeout,
00341                std::runtime_error ) ;
00342 
00353     typedef std::vector<unsigned char> DataBuffer ;
00354     void
00355     Read( DataBuffer&        dataBuffer,
00356           const unsigned int numOfBytes = 0,
00357           const unsigned int msTimeout  = 0 )
00358         throw( NotOpen,
00359                ReadTimeout,
00360                std::runtime_error ) ;
00361 
00362 
00366     const std::string
00367     ReadLine( const unsigned int msTimeout = 0,
00368               const char         lineTerminator = '\n' )
00369         throw( NotOpen,
00370                ReadTimeout,
00371                std::runtime_error ) ;
00372 
00379     void
00380     WriteByte(const unsigned char dataByte)
00381         throw( NotOpen,
00382                std::runtime_error ) ;
00383 
00387     void
00388     Write(const DataBuffer& dataBuffer)
00389         throw( NotOpen,
00390                std::runtime_error ) ;
00391 
00395     void
00396     Write(const std::string& dataString)
00397         throw( NotOpen,
00398                std::runtime_error ) ;
00399 private:
00400     SerialPort( const SerialPort& otherSerialPort ) ;
00401     SerialPort& operator=(const SerialPort& otherSerialPort ) ;
00402     class SerialPortImpl ;
00403     SerialPortImpl* mSerialPortImpl ;
00404 } ;
00405 
00406 #endif // #ifndef _SerialPort_h_
00407 

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