libserial  0.6.0rc3
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  BAUD_500000 = B500000,
82  BAUD_576000 = B576000,
83  BAUD_921600 = B921600,
84  BAUD_1000000 = B1000000,
85  BAUD_1152000 = B1152000,
86  BAUD_1500000 = B1500000,
87  BAUD_2000000 = B2000000,
88  BAUD_2500000 = B2500000,
89  BAUD_3000000 = B3000000,
90  BAUD_3500000 = B3500000,
91  BAUD_4000000 = B4000000,
92 #endif
94  } ;
95 
97  CHAR_SIZE_5 = CS5,
98  CHAR_SIZE_6 = CS6,
99  CHAR_SIZE_7 = CS7,
100  CHAR_SIZE_8 = CS8,
102  } ;
103 
104  enum StopBits {
108  } ;
109 
110  enum Parity {
115  } ;
116 
117  enum FlowControl {
122  } ;
123 
124  class NotOpen : public std::logic_error
125  {
126  public:
127  NotOpen(const std::string& whatArg) :
128  logic_error(whatArg) { }
129  } ;
130 
131  class OpenFailed : public std::runtime_error
132  {
133  public:
134  OpenFailed(const std::string& whatArg) :
135  runtime_error(whatArg) { }
136  } ;
137 
138  class AlreadyOpen : public std::logic_error
139  {
140  public:
141  AlreadyOpen( const std::string& whatArg ) :
142  logic_error(whatArg) { }
143  } ;
144 
145  class UnsupportedBaudRate : public std::runtime_error
146  {
147  public:
148  UnsupportedBaudRate( const std::string& whatArg ) :
149  runtime_error(whatArg) { }
150  } ;
151 
152  class ReadTimeout : public std::runtime_error
153  {
154  public:
155  ReadTimeout() : runtime_error( "Read timeout" ) { }
156  } ;
157 
161  explicit SerialPort( const std::string& serialPortName ) ;
162 
166  virtual ~SerialPort() throw() ;
167 
181  void
182  Open( const BaudRate baudRate = BAUD_DEFAULT,
183  const CharacterSize charSize = CHAR_SIZE_DEFAULT,
184  const Parity parityType = PARITY_DEFAULT,
185  const StopBits stopBits = STOP_BITS_DEFAULT,
186  const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
187  throw( AlreadyOpen,
188  OpenFailed,
189  UnsupportedBaudRate,
190  std::invalid_argument ) ;
191 
195  bool
196  IsOpen() const ;
197 
206  void
207  Close()
208  throw(NotOpen) ;
209 
220  void
221  SetBaudRate( const BaudRate baudRate )
222  throw( UnsupportedBaudRate,
223  NotOpen,
224  std::invalid_argument ) ;
225 
232  BaudRate
233  GetBaudRate() const
234  throw( NotOpen,
235  std::runtime_error ) ;
236 
246  void
247  SetCharSize( const CharacterSize charSize )
248  throw( NotOpen,
249  std::invalid_argument ) ;
258  GetCharSize() const
259  throw(NotOpen) ;
260 
270  void
271  SetParity( const Parity parityType )
272  throw( NotOpen,
273  std::invalid_argument ) ;
274 
282  Parity
283  GetParity() const
284  throw(NotOpen) ;
285 
295  void
296  SetNumOfStopBits( const StopBits numOfStopBits )
297  throw( NotOpen,
298  std::invalid_argument ) ;
299 
308  StopBits
309  GetNumOfStopBits() const
310  throw(NotOpen) ;
311 
321  void
322  SetFlowControl( const FlowControl flowControl )
323  throw( NotOpen,
324  std::invalid_argument ) ;
325 
334  GetFlowControl() const
335  throw( NotOpen ) ;
336 
344  bool
345  IsDataAvailable() const
346  throw(NotOpen) ;
347 
354  unsigned char
355  ReadByte( const unsigned int msTimeout = 0 )
356  throw( NotOpen,
357  ReadTimeout,
358  std::runtime_error ) ;
359 
370  typedef std::vector<unsigned char> DataBuffer ;
371  void
372  Read( DataBuffer& dataBuffer,
373  const unsigned int numOfBytes = 0,
374  const unsigned int msTimeout = 0 )
375  throw( NotOpen,
376  ReadTimeout,
377  std::runtime_error ) ;
378 
379 
383  const std::string
384  ReadLine( const unsigned int msTimeout = 0,
385  const char lineTerminator = '\n' )
386  throw( NotOpen,
387  ReadTimeout,
388  std::runtime_error ) ;
389 
396  void
397  WriteByte(const unsigned char dataByte)
398  throw( NotOpen,
399  std::runtime_error ) ;
400 
404  void
405  Write(const DataBuffer& dataBuffer)
406  throw( NotOpen,
407  std::runtime_error ) ;
408 
412  void
413  Write(const std::string& dataString)
414  throw( NotOpen,
415  std::runtime_error ) ;
416 
420  void
421  SetDtr( const bool dtrState = true )
422  throw( NotOpen,
423  std::runtime_error ) ;
424 
428  bool
429  GetDtr() const
430  throw( NotOpen,
431  std::runtime_error ) ;
432 
436  void
437  SetRts( const bool rtsState = true )
438  throw( NotOpen,
439  std::runtime_error ) ;
440 
444  bool
445  GetRts() const
446  throw( NotOpen,
447  std::runtime_error ) ;
448 
449  //void
450  //SetCts( const bool ctsState = true )
451  // throw( NotOpen,
452  // std::runtime_error ) ;
453 
454  bool
455  GetCts() const
456  throw( NotOpen,
457  std::runtime_error ) ;
458 
459  //void
460  //SetDsr( const bool dsrState = true )
461  // throw( NotOpen,
462  // std::runtime_error ) ;
463 
464  bool
465  GetDsr() const
466  throw( NotOpen,
467  std::runtime_error ) ;
468 
485  int GetFileDescriptor() const ;
486 
487 private:
492  SerialPort( const SerialPort& otherSerialPort ) ;
493 
498  SerialPort& operator=(const SerialPort& otherSerialPort ) ;
499 
500  /*
501  * Forward declaration of the implementation class folowing the
502  * PImpl idiom.
503  */
504  class SerialPortImpl ;
505 
509  SerialPortImpl* mSerialPortImpl ;
510 } ;
511 
512 #endif // #ifndef _SerialPort_h_
513 
AlreadyOpen(const std::string &whatArg)
Definition: SerialPort.h:141
void Close()
Close the serial port.
std::vector< unsigned char > DataBuffer
Read the specified number of bytes from the serial port.
Definition: SerialPort.h:370
UnsupportedBaudRate(const std::string &whatArg)
Definition: SerialPort.h:148
unsigned char ReadByte(const unsigned int msTimeout=0)
Read a single byte from the serial port.
OpenFailed(const std::string &whatArg)
Definition: SerialPort.h:134
FlowControl GetFlowControl() const
Get the current flow control setting.
Parity GetParity() const
Get the parity type for the serial port.
void Read(DataBuffer &dataBuffer, const unsigned int numOfBytes=0, const unsigned int msTimeout=0)
BaudRate GetBaudRate() const
Get the current baud rate for the serial port.
void SetRts(const bool rtsState=true)
Set the RTS line to the specified value.
bool IsOpen() const
Check if the serial port is open for I/O.
int GetFileDescriptor() const
Get the low-level file descriptor associated with the serial port.
void Write(const DataBuffer &dataBuffer)
Write the data from the specified vector to the serial port.
7 bit characters.
Definition: SerialPort.h:99
bool GetDtr() const
Get the status of the DTR line.
No parity i.e. parity checking disabled.
Definition: SerialPort.h:113
bool GetRts() const
Get the status of the RTS line.
void SetBaudRate(const BaudRate baudRate)
Set the baud rate for the serial port to the specified value (baudRate).
5 bit characters.
Definition: SerialPort.h:97
void Open(const BaudRate baudRate=BAUD_DEFAULT, const CharacterSize charSize=CHAR_SIZE_DEFAULT, const Parity parityType=PARITY_DEFAULT, const StopBits stopBits=STOP_BITS_DEFAULT, const FlowControl flowControl=FLOW_CONTROL_DEFAULT)
Open the serial port with the specified settings.
void SetDtr(const bool dtrState=true)
Set the DTR line to the specified value.
CharacterSize GetCharSize() const
Get the current character size for the serial port.
8 bit characters.
Definition: SerialPort.h:100
void SetNumOfStopBits(const StopBits numOfStopBits)
Set the number of stop bits to be used with the serial port.
void SetFlowControl(const FlowControl flowControl)
Set flow control.
6 bit characters.
Definition: SerialPort.h:98
StopBits GetNumOfStopBits() const
Get the number of stop bits currently being used by the serial port.
BaudRate
The allowed set of baud rates.
Definition: SerialPort.h:56
bool GetCts() const
SerialPort(const std::string &serialPortName)
Constructor for a serial port.
virtual ~SerialPort()
Destructor.
void WriteByte(const unsigned char dataByte)
Send a single byte to the serial port.
NotOpen(const std::string &whatArg)
Definition: SerialPort.h:127
bool IsDataAvailable() const
Check if data is available at the input of the serial port.
void SetCharSize(const CharacterSize charSize)
Set the character size for the serial port.
void SetParity(const Parity parityType)
Set the parity type for the serial port.
bool GetDsr() const
SerialPortImpl * mSerialPortImpl
Pointer to implementation class instance.
Definition: SerialPort.h:504
const std::string ReadLine(const unsigned int msTimeout=0, const char lineTerminator= '\n')
Read a line of characters from the serial port.