SerialDevice.h
1 /*
2  * Copyright (C) 2007 Marc Boris Drner
3  * Copyright (C) 2007 Laurentiu-Gheorghe Crisan
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef PT_SYSTEM_SERIALDEVICE_H
31 #define PT_SYSTEM_SERIALDEVICE_H
32 
33 #include <Pt/System/Api.h>
34 #include <Pt/System/IODevice.h>
35 #include <Pt/Types.h>
36 
37 namespace Pt {
38 
39 namespace System {
40 
72 class PT_SYSTEM_API SerialDevice : public IODevice
73 {
74  private:
75  class SerialDeviceImpl* _impl;
76  Pt::uint32_t _r0;
77 
78  public:
81  enum BaudRate
82  {
83  BaudRate0 = 0,
84  BaudRate50 = 50,
85  BaudRate75 = 75,
86  BaudRate110 = 110,
87  BaudRate134 = 134,
88  BaudRate150 = 150,
89  BaudRate200 = 200,
90  BaudRate300 = 300,
91  BaudRate600 = 600,
92  BaudRate1200 = 1200,
93  BaudRate1800 = 1800,
94  BaudRate2400 = 2400,
95  BaudRate4800 = 4800,
96  BaudRate9600 = 9600,
97  BaudRate19200 = 19200,
98  BaudRate38400 = 38400,
99  BaudRate57600 = 57600,
100  BaudRate115200 = 115200
101  #ifdef B230400
102  , BaudRate230400 = 230400
103  #endif
104  };
105 
108  enum Parity
109  {
110  ParityEven,
111  ParityOdd,
112  ParityNone
113  };
114 
118  {
119  FlowControlHard,
120  FlowControlSoft,
121  FlowControlNone
122  };
123 
126  enum StopBits
127  {
128  OneStopBit,
129  One5StopBits,
130  TwoStopBits
131  };
132 
133 
135  SerialDevice();
136 
139  SerialDevice(const std::string& file, std::ios::openmode mode);
140 
143  SerialDevice(const char* file, std::ios::openmode mode);
144 
146  virtual ~SerialDevice();
147 
150  void open(const std::string& file, std::ios::openmode mode);
151 
154  void open(const char* file, std::ios::openmode mode);
155 
157  void setBaudRate( unsigned rate );
158 
160  unsigned baudRate() const;
161 
163  void setCharSize( int size );
164 
166  int charSize() const;
167 
169  void setStopBits( StopBits bits );
170 
172  StopBits stopBits() const;
173 
175  void setParity( Parity parity );
176 
178  Parity parity() const;
179 
181  void setFlowControl( FlowControl flowControl );
182 
184  FlowControl flowControl() const;
185 
187  void setRts(bool on);
188 
189  void setDtr(bool on);
190 
191  void setBreak(bool on);
192 
193  void sendBreak(int duration = 0);
194 
195  bool isCts() const;
196 
197  bool isDsr() const;
198 
199  protected:
200  // inherit docs
201  void onClose();
202 
203  // inherit docs
204  void onSetTimeout(std::size_t timeout);
205 
206  // inherit docs
207  std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
208 
209  // inherit docs
210  std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
211 
212  // inherit docs
213  std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n);
214 
215  // inherit docs
216  std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n);
217 
218  // inherit docs
219  std::size_t onRead(char* buffer, std::size_t count, bool& eof);
220 
221  // inherit docs
222  std::size_t onWrite(const char* buffer, std::size_t count);
223 
224  // inherit docs
225  void onSync() const;
226 
227  // inherit docs
228  void onCancel();
229 
230  // inherit docs
231  bool onRun();
232 };
233 
234 } // namespace System
235 
236 } // namespace Pt
237 
238 #endif // PT_SYSTEM_SERIALDEVICE_H
Parity
Parity values.
Definition: SerialDevice.h:108
BaudRate
Baud rates.
Definition: SerialDevice.h:81
Endpoint for I/O operations.
Definition: IODevice.h:55
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
StopBits
Stop bits values.
Definition: SerialDevice.h:126
Serial device.
Definition: SerialDevice.h:72
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
FlowControl
Flow control values.
Definition: SerialDevice.h:117