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 
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 
173 
175  void setParity( Parity parity );
176 
178  Parity parity() const;
179 
181  void setFlowControl( FlowControl flowControl );
182 
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  void clear();
200 
201  protected:
202  // inherit docs
203  void onClose();
204 
205  // inherit docs
206  void onSetTimeout(std::size_t timeout);
207 
208  // inherit docs
209  std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
210 
211  // inherit docs
212  std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
213 
214  // inherit docs
215  std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n);
216 
217  // inherit docs
218  std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n);
219 
220  // inherit docs
221  std::size_t onRead(char* buffer, std::size_t count, bool& eof);
222 
223  // inherit docs
224  std::size_t onWrite(const char* buffer, std::size_t count);
225 
226  // inherit docs
227  void onSync() const;
228 
229  // inherit docs
230  void onCancel();
231 
232  // inherit docs
233  bool onRun();
234 };
235 
236 } // namespace System
237 
238 } // namespace Pt
239 
240 #endif // PT_SYSTEM_SERIALDEVICE_H
Core module.
Definition: pt-gfx-images.dox:14
Parity parity() const
Gets the current parity.
Parity
Parity values.
Definition: SerialDevice.h:109
SerialDevice()
Default constructor.
void onCancel()
Blocks until operation has cancelled.
void setBaudRate(unsigned rate)
Sets the baud rate.
void setParity(Parity parity)
Sets the parity.
virtual ~SerialDevice()
Destructor.
SerialDevice(const char *file, std::ios::openmode mode)
Constructs a serial device and open the specified device file.
BaudRate
Baud rates.
Definition: SerialDevice.h:82
int charSize() const
Gets the current char size.
void setFlowControl(FlowControl flowControl)
Sets the flow control kind.
bool onRun()
Check if ready and run.
StopBits
Stop bits values.
Definition: SerialDevice.h:127
void open(const char *file, std::ios::openmode mode)
Open the specified device file.
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
Endpoint for I/O operations.
Definition: IODevice.h:56
Serial device.
Definition: SerialDevice.h:73
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
void open(const std::string &file, std::ios::openmode mode)
Open the specified device file.
SerialDevice(const std::string &file, std::ios::openmode mode)
Constructs a serial device and open the specified device file.
unsigned baudRate() const
Gets the baud rate.
StopBits stopBits() const
Gets the current number of stop bits.
void setCharSize(int size)
Sets the char size.
void setStopBits(StopBits bits)
Sets the number of stop bits.
FlowControl
Flow control values.
Definition: SerialDevice.h:118
FlowControl flowControl() const
Gets the current flow control kind.