/[projects]/smsdaemon/serialport/SerialPort.h
ViewVC logotype

Annotation of /smsdaemon/serialport/SerialPort.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (hide annotations) (download)
Thu Dec 18 06:53:29 2008 UTC (15 years, 5 months ago) by torben
File MIME type: text/plain
File size: 12514 byte(s)
Make pretty

astyle -t -b -N

1 torben 26 /***************************************************************************
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    
30     /**
31     *
32     * @note This class attaches a handler to the SIGIO signal to detect
33     * the data arriving at a serial port. However, this signal handler
34     * will also call any signal handler that is already attached to
35     * this signal. However, if other parts of the application attach a
36     * signal handler to SIGIO after constructing an instance of SIGIO,
37     * they must ensure that they call the existing signal handler.
38     * Otherwise, it may not be possible to receive any data through
39     * the serial port using this class.
40     *
41     * :FIXME: Provide examples of the above potential problem.
42     *
43 torben 196 * @todo The current implementation does not check if another process has
44 torben 26 * locked the serial port device and does not lock the serial port device after
45 torben 196 * opening it. This has been observed to cause problems while using this
46     * library while other programs such as minicom are also accessing the same device.
47 torben 26 * It will be useful to lock the serial port device when it is being used by
48 torben 196 * this class.
49 torben 26 */
50     class SerialPort
51     {
52     public:
53 torben 196 /**
54     * The allowed set of baud rates.
55     */
56     enum BaudRate
57     {
58     BAUD_50 = B50,
59     BAUD_75 = B75,
60     BAUD_110 = B110,
61     BAUD_134 = B134,
62     BAUD_150 = B150,
63     BAUD_200 = B200,
64     BAUD_300 = B300,
65     BAUD_600 = B600,
66     BAUD_1200 = B1200,
67     BAUD_1800 = B1800,
68     BAUD_2400 = B2400,
69     BAUD_4800 = B4800,
70     BAUD_9600 = B9600,
71     BAUD_19200 = B19200,
72     BAUD_38400 = B38400,
73     BAUD_57600 = B57600,
74     BAUD_115200 = B115200,
75     BAUD_230400 = B230400,
76     //
77     // Bug#1318912
78     // B460800 is defined on Linux but not on Mac OS X. What about other
79     // operating systems ?
80     //
81     #ifdef __linux__
82     BAUD_460800 = B460800,
83 torben 26 #endif
84 torben 196 BAUD_DEFAULT = BAUD_57600
85     } ;
86 torben 26
87 torben 196 enum CharacterSize
88     {
89     CHAR_SIZE_5 = CS5, //!< 5 bit characters.
90     CHAR_SIZE_6 = CS6, //!< 6 bit characters.
91     CHAR_SIZE_7 = CS7, //!< 7 bit characters.
92     CHAR_SIZE_8 = CS8, //!< 8 bit characters.
93     CHAR_SIZE_DEFAULT = CHAR_SIZE_8
94     } ;
95 torben 26
96 torben 196 enum StopBits
97     {
98     STOP_BITS_1, //! 1 stop bit.
99     STOP_BITS_2, //! 2 stop bits.
100     STOP_BITS_DEFAULT = STOP_BITS_1
101     } ;
102 torben 26
103 torben 196 enum Parity
104     {
105     PARITY_EVEN, //!< Even parity.
106     PARITY_ODD, //!< Odd parity.
107     PARITY_NONE, //!< No parity i.e. parity checking disabled.
108     PARITY_DEFAULT = PARITY_NONE
109     } ;
110 torben 26
111 torben 196 enum FlowControl
112     {
113     FLOW_CONTROL_HARD,
114     // FLOW_CONTROL_SOFT,
115     FLOW_CONTROL_NONE,
116     FLOW_CONTROL_DEFAULT = FLOW_CONTROL_NONE
117     } ;
118 torben 26
119 torben 196 class NotOpen : public std::logic_error
120     {
121     public:
122     NotOpen(const std::string& whatArg) :
123     logic_error(whatArg) { }
124     } ;
125 torben 26
126 torben 196 class OpenFailed : public std::runtime_error
127     {
128     public:
129     OpenFailed(const std::string& whatArg) :
130     runtime_error(whatArg) { }
131     } ;
132 torben 26
133 torben 196 class AlreadyOpen : public std::logic_error
134     {
135     public:
136     AlreadyOpen( const std::string& whatArg ) :
137     logic_error(whatArg) { }
138     } ;
139 torben 26
140 torben 196 class UnsupportedBaudRate : public std::runtime_error
141     {
142     public:
143     UnsupportedBaudRate( const std::string& whatArg ) :
144     runtime_error(whatArg) { }
145     } ;
146 torben 26
147 torben 196 class ReadTimeout : public std::runtime_error
148     {
149     public:
150     ReadTimeout() : runtime_error( "Read timeout" ) { }
151     } ;
152 torben 26
153 torben 196 /**
154     * Constructor for a serial port.
155     */
156     explicit SerialPort( const std::string& serialPortName ) ;
157 torben 26
158 torben 196 /**
159     * Destructor.
160     */
161     ~SerialPort() throw() ;
162 torben 26
163 torben 196 /**
164     * Open the serial port with the specified settings. A serial port
165     * cannot be used till it is open.
166     *
167     * @throw AlreadyOpen This exception is thrown if the serial port
168     * is already open.
169     *
170     * @throw OpenFailed This exception is thrown if the serial port
171     * could not be opened.
172     *
173     * @throw std::invalid_argument This exception is thrown if an
174     * invalid parameter value is specified.
175     */
176     void
177     Open( const BaudRate baudRate = BAUD_DEFAULT,
178     const CharacterSize charSize = CHAR_SIZE_DEFAULT,
179     const Parity parityType = PARITY_DEFAULT,
180     const StopBits stopBits = STOP_BITS_DEFAULT,
181     const FlowControl flowControl = FLOW_CONTROL_DEFAULT )
182     throw( AlreadyOpen,
183     OpenFailed,
184     UnsupportedBaudRate,
185     std::invalid_argument ) ;
186 torben 26
187 torben 196 /**
188     * Check if the serial port is open for I/O.
189     */
190     bool
191     IsOpen() const ;
192 torben 26
193 torben 196 /**
194     * Close the serial port. All settings of the serial port will be
195     * lost and no more I/O can be performed on the serial port.
196     *
197     * @throw NotOpen Thrown if this method is called while the serial
198     * port is not open.
199     *
200     */
201     void
202     Close()
203     throw(NotOpen) ;
204 torben 26
205 torben 196 /**
206     * Set the baud rate for the serial port to the specified value
207     * (baudRate).
208     *
209     * @throw NotOpen Thrown if this method is called while the serial
210     * port is not open.
211     *
212     * @throw std::invalid_argument Thrown if an invalid baud rate is
213     * specified.
214     */
215     void
216     SetBaudRate( const BaudRate baudRate )
217     throw( UnsupportedBaudRate,
218     NotOpen,
219     std::invalid_argument ) ;
220 torben 26
221 torben 196 /**
222     * Get the current baud rate for the serial port.
223     *
224     * @throw NotOpen Thrown if this method is called while the serial
225     * port is not open.
226     */
227     BaudRate
228     GetBaudRate() const
229     throw( NotOpen,
230     std::runtime_error ) ;
231 torben 26
232 torben 196 /**
233     * Set the character size for the serial port.
234     *
235     * @throw NotOpen Thrown if this method is called while the serial
236     * port is not open.
237     *
238     * @throw std::invalid_argument Thrown if an invalid character
239     * size is specified.
240     */
241     void
242     SetCharSize( const CharacterSize charSize )
243     throw( NotOpen,
244     std::invalid_argument ) ;
245     /**
246     * Get the current character size for the serial port.
247     *
248     * @throw NotOpen Thrown if this method is called while the serial
249     * port is not open.
250     *
251     */
252     CharacterSize
253     GetCharSize() const
254     throw(NotOpen) ;
255 torben 26
256 torben 196 /**
257     * Set the parity type for the serial port.
258     *
259     * @throw NotOpen Thrown if this method is called while the serial
260     * port is not open.
261     *
262     * @throw std::invalid_argument Thrown if an invalid parity is
263     * specified.
264     */
265     void
266     SetParity( const Parity parityType )
267     throw( NotOpen,
268     std::invalid_argument ) ;
269 torben 26
270 torben 196 /**
271     * Get the parity type for the serial port.
272     *
273     * @throw NotOpen Thrown if this method is called while the serial
274     * port is not open.
275     *
276     */
277     Parity
278     GetParity() const
279     throw(NotOpen) ;
280 torben 26
281 torben 196 /**
282     * Set the number of stop bits to be used with the serial port.
283     *
284     * @throw NotOpen Thrown if this method is called while the serial
285     * port is not open.
286     *
287     * @throw std::invalid_argument Thrown if an invalid number of
288     * stop bits is specified.
289     */
290     void
291     SetNumOfStopBits( const StopBits numOfStopBits )
292     throw( NotOpen,
293     std::invalid_argument ) ;
294 torben 26
295 torben 196 /**
296     * Get the number of stop bits currently being used by the serial
297     * port.
298     *
299     * @throw NotOpen Thrown if this method is called while the serial
300     * port is not open.
301     *
302     */
303     StopBits
304     GetNumOfStopBits() const
305     throw(NotOpen) ;
306 torben 26
307 torben 196 /**
308     * Set flow control.
309     *
310     * @throw NotOpen Thrown if this method is called while the serial
311     * port is not open.
312     *
313     * @throw std::invalid_argument Thrown if an invalid flow control
314     * is specified.
315     */
316     void
317     SetFlowControl( const FlowControl flowControl )
318     throw( NotOpen,
319     std::invalid_argument ) ;
320 torben 26
321 torben 196 /**
322     * Get the current flow control setting.
323     *
324     * @throw NotOpen Thrown if this method is called while the serial
325     * port is not open.
326     *
327     */
328     FlowControl
329     GetFlowControl() const
330     throw( NotOpen ) ;
331 torben 26
332 torben 196 /**
333     * Check if data is available at the input of the serial port.
334     *
335     * @throw NotOpen Thrown if this method is called while the serial
336     * port is not open.
337     *
338     */
339     bool
340     IsDataAvailable() const
341     throw(NotOpen) ;
342 torben 26
343 torben 196 /**
344     * Read a single byte from the serial port. If no data is
345     * available in the specified number of milliseconds (msTimeout),
346     * then this method will throw ReadTimeout exception. If msTimeout
347     * is 0, then this method will block till data is available.
348     */
349     unsigned char
350     ReadByte( const unsigned int msTimeout = 0 )
351     throw( NotOpen,
352     ReadTimeout,
353     std::runtime_error ) ;
354 torben 26
355 torben 196 /**
356     * Read the specified number of bytes from the serial port. The
357     * method will timeout if no data is received in the specified
358     * number of milliseconds (msTimeout). If msTimeout is 0, then
359     * this method will block till all requested bytes are
360     * received. If numOfBytes is zero, then this method will keep
361     * reading data till no more data is available at the serial
362     * port. In all cases, all read data is available in dataBuffer on
363     * return from this method.
364     */
365     typedef std::vector<unsigned char> DataBuffer ;
366     void
367     Read( DataBuffer& dataBuffer,
368     const unsigned int numOfBytes = 0,
369     const unsigned int msTimeout = 0 )
370     throw( NotOpen,
371     ReadTimeout,
372     std::runtime_error ) ;
373 torben 26
374    
375 torben 196 /**
376     * Read a line of characters from the serial port.
377     */
378     const std::string
379     ReadLine( const unsigned int msTimeout = 0,
380     const char lineTerminator = '\n' )
381     throw( NotOpen,
382     ReadTimeout,
383     std::runtime_error ) ;
384 torben 26
385 torben 196 /**
386     * Send a single byte to the serial port.
387     *
388     * @throw NotOpen Thrown if this method is called while the serial
389     * port is not open.
390     */
391     void
392     WriteByte(const unsigned char dataByte)
393     throw( NotOpen,
394     std::runtime_error ) ;
395 torben 26
396 torben 196 /**
397     * Write the data from the specified vector to the serial port.
398     */
399     void
400     Write(const DataBuffer& dataBuffer)
401     throw( NotOpen,
402     std::runtime_error ) ;
403 torben 26
404 torben 196 /**
405     * Write a string to the serial port.
406     */
407     void
408     Write(const std::string& dataString)
409     throw( NotOpen,
410     std::runtime_error ) ;
411 torben 26
412 torben 196 /**
413     * Set the DTR line to the specified value.
414     */
415     void
416     SetDtr( const bool dtrState = true )
417     throw( NotOpen,
418     std::runtime_error ) ;
419 torben 26
420 torben 196 /**
421     * Get the status of the DTR line.
422     */
423     bool
424     GetDtr() const
425     throw( NotOpen,
426     std::runtime_error ) ;
427 torben 26
428 torben 196 /**
429     * Set the RTS line to the specified value.
430     */
431     void
432     SetRts( const bool rtsState = true )
433     throw( NotOpen,
434     std::runtime_error ) ;
435 torben 26
436 torben 196 /**
437     * Get the status of the RTS line.
438     */
439     bool
440     GetRts() const
441     throw( NotOpen,
442     std::runtime_error ) ;
443 torben 26
444 torben 196 //void
445     //SetCts( const bool ctsState = true )
446     // throw( NotOpen,
447     // std::runtime_error ) ;
448 torben 26
449 torben 196 bool
450     GetCts() const
451     throw( NotOpen,
452     std::runtime_error ) ;
453    
454     //void
455     //SetDsr( const bool dsrState = true )
456     // throw( NotOpen,
457     // std::runtime_error ) ;
458    
459     bool
460     GetDsr() const
461     throw( NotOpen,
462     std::runtime_error ) ;
463 torben 26 private:
464     /**
465 torben 196 * Prevent copying of objects of this class by declaring the copy
466     * constructor private. This method is never defined.
467     */
468     SerialPort( const SerialPort& otherSerialPort ) ;
469    
470     /**
471     * Prevent copying of objects of this class by declaring the assignment
472     * operator private. This method is never defined.
473     */
474     SerialPort& operator=(const SerialPort& otherSerialPort ) ;
475    
476     /*
477     * Forward declaration of the implementation class folowing the
478     * PImpl idiom.
479     */
480     class SerialPortImpl ;
481    
482     /**
483     * Pointer to implementation class instance.
484     */
485     SerialPortImpl* mSerialPortImpl ;
486 torben 26 } ;
487    
488     #endif // #ifndef _SerialPort_h_

  ViewVC Help
Powered by ViewVC 1.1.20