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
37namespace Pt {
38
39namespace System {
40
68class PT_SYSTEM_API SerialDevice : public IODevice
69{
70 private:
71 class SerialDeviceImpl* _impl;
72 Pt::uint32_t _r0;
73
74 public:
78 {
79 BaudRate0 = 0,
80 BaudRate50 = 50,
81 BaudRate75 = 75,
82 BaudRate110 = 110,
83 BaudRate134 = 134,
84 BaudRate150 = 150,
85 BaudRate200 = 200,
86 BaudRate300 = 300,
87 BaudRate600 = 600,
88 BaudRate1200 = 1200,
89 BaudRate1800 = 1800,
90 BaudRate2400 = 2400,
91 BaudRate4800 = 4800,
92 BaudRate9600 = 9600,
93 BaudRate19200 = 19200,
94 BaudRate38400 = 38400,
95 BaudRate57600 = 57600,
96 BaudRate115200 = 115200
97 #ifdef B230400
98 , BaudRate230400 = 230400
99 #endif
100 };
101
105 {
106 ParityEven,
107 ParityOdd,
108 ParityNone
109 };
110
114 {
115 FlowControlHard,
116 FlowControlSoft,
117 FlowControlNone
118 };
119
123 {
124 OneStopBit,
125 One5StopBits,
126 TwoStopBits
127 };
128
129
132
135 SerialDevice(const std::string& file, std::ios::openmode mode);
136
139 SerialDevice(const char* file, std::ios::openmode mode);
140
142 virtual ~SerialDevice();
143
146 void open(const std::string& file, std::ios::openmode mode);
147
150 void open(const char* file, std::ios::openmode mode);
151
153 void setBaudRate( unsigned rate );
154
156 unsigned baudRate() const;
157
159 void setCharSize( int size );
160
162 int charSize() const;
163
165 void setStopBits( StopBits bits );
166
169
172
174 Parity parity() const;
175
178
181
184 void setRts(bool on);
185
188 void setDtr(bool on);
189
192 void setBreak(bool on);
193
196 void sendBreak(int duration = 0);
197
200 bool isCts() const;
201
204 bool isDsr() const;
205
208 void clear();
209
210 protected:
211 // inherit docs
212 void onClose();
213
214 // inherit docs
215 void onSetTimeout(std::size_t timeout);
216
217 // inherit docs
218 std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
219
220 // inherit docs
221 std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof);
222
223 // inherit docs
224 std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n);
225
226 // inherit docs
227 std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n);
228
229 // inherit docs
230 std::size_t onRead(char* buffer, std::size_t count, bool& eof);
231
232 // inherit docs
233 std::size_t onWrite(const char* buffer, std::size_t count);
234
235 // inherit docs
236 void onSync() const;
237
238 // inherit docs
239 void onCancel();
240
241 // inherit docs
242 bool onRun();
243};
244
245} // namespace System
246
247} // namespace Pt
248
249#endif // PT_SYSTEM_SERIALDEVICE_H
Event loop of a thread or process.
Definition EventLoop.h:83
IODevice()
Default Constructor.
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
void open(const char *file, std::ios::openmode mode)
Open the specified device file.
SerialDevice()
Default constructor.
void sendBreak(int duration=0)
Sends a break condition.
SerialDevice(const std::string &file, std::ios::openmode mode)
Constructs a serial device and open the specified device file.
StopBits
Stop bits values.
Definition SerialDevice.h:123
int charSize() const
Gets the current char size.
void setCharSize(int size)
Sets the char size.
StopBits stopBits() const
Gets the current number of stop bits.
void setFlowControl(FlowControl flowControl)
Sets the flow control kind.
void onCancel()
Blocks until operation has cancelled.
bool onRun()
Check if ready and run.
void setBreak(bool on)
Sets or clears a break condition.
BaudRate
Baud rates.
Definition SerialDevice.h:78
FlowControl flowControl() const
Gets the current flow control kind.
void setParity(Parity parity)
Sets the parity.
void open(const std::string &file, std::ios::openmode mode)
Open the specified device file.
void setBaudRate(unsigned rate)
Sets the baud rate.
Parity
Parity values.
Definition SerialDevice.h:105
unsigned baudRate() const
Gets the baud rate.
void clear()
Clears pending error flags.
bool isCts() const
Returns true if CTS is asserted.
Parity parity() const
Gets the current parity.
void setDtr(bool on)
Sets the DTR control line.
bool isDsr() const
Returns true if DSR is asserted.
void setStopBits(StopBits bits)
Sets the number of stop bits.
virtual ~SerialDevice()
Destructor.
FlowControl
Flow control values.
Definition SerialDevice.h:114
SerialDevice(const char *file, std::ios::openmode mode)
Constructs a serial device and open the specified device file.
void setRts(bool on)
Sets the RTS control line.
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33