libserial  0.6.0rc1
SerialPort.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Manish Pagey *
3  * crayzeewulf@users.sourceforge.net
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 #ifndef _SerialPort_h_
21 #define _SerialPort_h_
22 
23 
24 #include <string>
25 #include <vector>
26 #include <stdexcept>
27 #include <termios.h>
28 
29 
51 {
52 public:
56  enum BaudRate {
57  BAUD_50 = B50,
58  BAUD_75 = B75,
59  BAUD_110 = B110,
60  BAUD_134 = B134,
61  BAUD_150 = B150,
62  BAUD_200 = B200,
63  BAUD_300 = B300,
64  BAUD_600 = B600,
65  BAUD_1200 = B1200,
66  BAUD_1800 = B1800,
67  BAUD_2400 = B2400,
68  BAUD_4800 = B4800,
69  BAUD_9600 = B9600,
70  BAUD_19200 = B19200,
71  BAUD_38400 = B38400,
72  BAUD_57600 = B57600,
73  BAUD_115200 = B115200,
74  BAUD_230400 = B230400,
75  //
76  // Bug#1318912: B460800 is defined on Linux but not on Mac OS
77  // X. What about other operating systems ?
78  //
79 #ifdef __linux__
80  BAUD_460800 = B460800,
81 #endif
83  } ;
84 
86  CHAR_SIZE_5 = CS5,
87  CHAR_SIZE_6 = CS6,
88  CHAR_SIZE_7 = CS7,
89  CHAR_SIZE_8 = CS8,
91  } ;
92 
93  enum StopBits {
97  } ;
98 
99  enum Parity {
104  } ;
105 
106  enum FlowControl {
111  } ;
112 
113  class NotOpen : public std::logic_error
114  {
115  public:
116  NotOpen(const std::string& whatArg) :
117  logic_error(whatArg) { }
118  } ;
119 
120  class OpenFailed : public std::runtime_error
121  {
122  public:
123  OpenFailed(const std::string& whatArg) :
124  runtime_error(whatArg) { }
125  } ;
126 
127  class AlreadyOpen : public std::logic_error
128  {
129  public:
130  AlreadyOpen( const std::string& whatArg ) :
131  logic_error(whatArg) { }
132  } ;
133 
134  class UnsupportedBaudRate : public std::runtime_error
135  {
136  public:
137  UnsupportedBaudRate( const std::string& whatArg ) :
138  runtime_error(whatArg) { }
139  } ;
140 
141  class ReadTimeout : public std::runtime_error
142  {
143  public:
144  ReadTimeout() : runtime_error( "Read timeout" ) { }
145  } ;
146 
150  explicit SerialPort( const std::string& serialPortName ) ;
151 
155  virtual ~SerialPort() throw() ;
156 
170  void
171  Open( const BaudRate baudRate = BAUD_DEFAULT,
172  const CharacterSize charSize = CHAR_SIZE_DEFAULT,
173  const Parity parityType = PARITY_DEFAULT,
174  const StopBits stopBits = STOP_BITS_DEFAULT,
175  const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
176  throw( AlreadyOpen,
177  OpenFailed,
178  UnsupportedBaudRate,
179  std::invalid_argument ) ;
180 
184  bool
185  IsOpen() const ;
186 
195  void
196  Close()
197  throw(NotOpen) ;
198 
209  void
210  SetBaudRate( const BaudRate baudRate )
211  throw( UnsupportedBaudRate,
212  NotOpen,
213  std::invalid_argument ) ;
214 
221  BaudRate
222  GetBaudRate() const
223  throw( NotOpen,
224  std::runtime_error ) ;
225 
235  void
236  SetCharSize( const CharacterSize charSize )
237  throw( NotOpen,
238  std::invalid_argument ) ;
247  GetCharSize() const
248  throw(NotOpen) ;
249 
259  void
260  SetParity( const Parity parityType )
261  throw( NotOpen,
262  std::invalid_argument ) ;
263 
271  Parity
272  GetParity() const
273  throw(NotOpen) ;
274 
284  void
285  SetNumOfStopBits( const StopBits numOfStopBits )
286  throw( NotOpen,
287  std::invalid_argument ) ;
288 
297  StopBits
298  GetNumOfStopBits() const
299  throw(NotOpen) ;
300 
310  void
311  SetFlowControl( const FlowControl flowControl )
312  throw( NotOpen,
313  std::invalid_argument ) ;
314 
323  GetFlowControl() const
324  throw( NotOpen ) ;
325 
333  bool
334  IsDataAvailable() const
335  throw(NotOpen) ;
336 
343  unsigned char
344  ReadByte( const unsigned int msTimeout = 0 )
345  throw( NotOpen,
346  ReadTimeout,
347  std::runtime_error ) ;
348 
359  typedef std::vector<unsigned char> DataBuffer ;
360  void
361  Read( DataBuffer& dataBuffer,
362  const unsigned int numOfBytes = 0,
363  const unsigned int msTimeout = 0 )
364  throw( NotOpen,
365  ReadTimeout,
366  std::runtime_error ) ;
367 
368 
372  const std::string
373  ReadLine( const unsigned int msTimeout = 0,
374  const char lineTerminator = '\n' )
375  throw( NotOpen,
376  ReadTimeout,
377  std::runtime_error ) ;
378 
385  void
386  WriteByte(const unsigned char dataByte)
387  throw( NotOpen,
388  std::runtime_error ) ;
389 
393  void
394  Write(const DataBuffer& dataBuffer)
395  throw( NotOpen,
396  std::runtime_error ) ;
397 
401  void
402  Write(const std::string& dataString)
403  throw( NotOpen,
404  std::runtime_error ) ;
405 
409  void
410  SetDtr( const bool dtrState = true )
411  throw( NotOpen,
412  std::runtime_error ) ;
413 
417  bool
418  GetDtr() const
419  throw( NotOpen,
420  std::runtime_error ) ;
421 
425  void
426  SetRts( const bool rtsState = true )
427  throw( NotOpen,
428  std::runtime_error ) ;
429 
433  bool
434  GetRts() const
435  throw( NotOpen,
436  std::runtime_error ) ;
437 
438  //void
439  //SetCts( const bool ctsState = true )
440  // throw( NotOpen,
441  // std::runtime_error ) ;
442 
443  bool
444  GetCts() const
445  throw( NotOpen,
446  std::runtime_error ) ;
447 
448  //void
449  //SetDsr( const bool dsrState = true )
450  // throw( NotOpen,
451  // std::runtime_error ) ;
452 
453  bool
454  GetDsr() const
455  throw( NotOpen,
456  std::runtime_error ) ;
457 private:
462  SerialPort( const SerialPort& otherSerialPort ) ;
463 
468  SerialPort& operator=(const SerialPort& otherSerialPort ) ;
469 
470  /*
471  * Forward declaration of the implementation class folowing the
472  * PImpl idiom.
473  */
474  class SerialPortImpl ;
475 
479  SerialPortImpl* mSerialPortImpl ;
480 } ;
481 
482 #endif // #ifndef _SerialPort_h_
483