Rev 391 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
158 | KeyOz | 1 | /******************************************************************************************************** |
2 | * PROGRAM : QSerialPortTerminal |
||
3 | * DATE - TIME : vendredi 03 octobre 2008 - 11h15 |
||
4 | * AUTHOR : VIANNEY-LIAUD Philippe ( philippe.vianney.liaud gmail.com ) |
||
5 | * FILENAME : ManageSerialPort.h |
||
6 | * LICENSE : GPL |
||
7 | * COMMENTARY : Manage qExtSerialPort |
||
8 | ********************************************************************************************************/ |
||
9 | #ifndef MANAGESERIALPORT_H |
||
10 | #define MANAGESERIALPORT_H |
||
11 | |||
12 | #include <QThread> |
||
13 | #include <QQueue> |
||
14 | #include <QMetaType> |
||
15 | #include <QMutex> |
||
16 | #include "qextserialport.h" |
||
17 | |||
18 | Q_DECLARE_METATYPE(BaudRateType); |
||
19 | Q_DECLARE_METATYPE(DataBitsType); |
||
20 | Q_DECLARE_METATYPE(ParityType); |
||
21 | Q_DECLARE_METATYPE(StopBitsType); |
||
22 | Q_DECLARE_METATYPE(FlowType); |
||
23 | |||
24 | class ThreadSend; |
||
25 | class ThreadReceive; |
||
26 | |||
27 | |||
28 | class ManageSerialPort : public QObject |
||
29 | { |
||
30 | Q_OBJECT |
||
31 | |||
32 | public: |
||
33 | //Constructeurs + destructeur |
||
34 | ManageSerialPort(); |
||
35 | ManageSerialPort(const QString &name, const BaudRateType baudRate, const DataBitsType dataBits,\ |
||
36 | const ParityType parity, const StopBitsType stopBits, \ |
||
37 | const FlowType flowControl, ulong seconds, ulong milliseconds); |
||
38 | ~ManageSerialPort(); |
||
39 | |||
40 | //General |
||
41 | bool open(); |
||
42 | bool open(const QString &name, const BaudRateType baudRate, const DataBitsType dataBits,\ |
||
43 | const ParityType parity, const StopBitsType stopBits, \ |
||
44 | const FlowType flowControl, ulong seconds, ulong milliseconds); |
||
45 | bool isOpen(); |
||
46 | void close(); |
||
47 | |||
48 | //Sets the name of the device associated with the object, e.g. "COM1", or "/dev/ttyS0" |
||
49 | void setPort(const QString &name); |
||
50 | QString getPort(); |
||
51 | |||
52 | /*Most used : '*' (POSTX & Windows) |
||
53 | RATE Windows Speed POSIX Speed |
||
54 | ----------- ------------- ----------- |
||
55 | enum BaudRateType{ |
||
56 | BAUD50 110 50 //POSIX ONLY |
||
57 | BAUD75 110 75 //POSIX ONLY |
||
58 | *BAUD110 110 110 |
||
59 | BAUD134 110 134.5 //POSIX ONLY |
||
60 | BAUD150 110 150 //POSIX ONLY |
||
61 | BAUD200 110 200 //POSIX ONLY |
||
62 | *BAUD300 300 300 |
||
63 | *BAUD600 600 600 |
||
64 | *BAUD1200 1200 1200 |
||
65 | BAUD1800 1200 1800 //POSIX ONLY |
||
66 | *BAUD2400 2400 2400 |
||
67 | *BAUD4800 4800 4800 |
||
68 | *BAUD9600 9600 9600 |
||
69 | BAUD14400 14400 9600 //WINDOWS ONLY |
||
70 | *BAUD19200 19200 19200 |
||
71 | *BAUD38400 38400 38400 |
||
72 | BAUD56000 56000 38400 //WINDOWS ONLY |
||
73 | *BAUD57600 57600 57600 |
||
74 | BAUD76800 57600 76800 //POSIX ONLY |
||
75 | *BAUD115200 115200 115200 |
||
76 | BAUD128000 128000 115200 //WINDOWS ONLY |
||
77 | BAUD256000 256000 115200 //WINDOWS ONLY |
||
78 | }*/ |
||
79 | void setBaudRate(const BaudRateType baudRate); |
||
80 | |||
81 | /*getBaudRate |
||
82 | return "50"; |
||
83 | return "75"; |
||
84 | return "110"; |
||
85 | return "134"; |
||
86 | return "150"; |
||
87 | return "200"; |
||
88 | return "300"; |
||
89 | return "600"; |
||
90 | return "1200"; |
||
91 | return "1800"; |
||
92 | return "2400"; |
||
93 | return "4800"; |
||
94 | return "9600"; |
||
95 | return "14400"; |
||
96 | return "19200"; |
||
97 | return "38400"; |
||
98 | return "56000"; |
||
99 | return "57600"; |
||
100 | return "76800"; |
||
101 | return "115200"; |
||
102 | return "128000"; |
||
103 | return "256000";*/ |
||
104 | QString getBaudRate(); |
||
105 | |||
106 | |||
107 | /*enum DataBitsType { |
||
108 | DATA_5 |
||
109 | DATA_6 |
||
110 | DATA_7 |
||
111 | DATA_8 |
||
112 | };*/ |
||
113 | void setDataBits(const DataBitsType dataBits); |
||
114 | |||
115 | /*getDataBits |
||
116 | return '5'; |
||
117 | return '6'; |
||
118 | return '7'; |
||
119 | return '8';*/ |
||
120 | QChar getDataBits(); |
||
121 | |||
122 | |||
123 | /*enum ParityType { |
||
124 | PAR_NONE //None means that no parity bit is sent at all |
||
125 | PAR_ODD //Odd |
||
126 | PAR_EVEN //Even |
||
127 | PAR_MARK //Windows only : Mark parity means that the parity bit is always set to the mark signal condition (logical 1) |
||
128 | PAR_SPACE //Space parity always sends the parity bit in the space signal condition |
||
129 | };*/ |
||
130 | void setParity(const ParityType parity); |
||
131 | |||
132 | /*getParity |
||
133 | return "None"; |
||
134 | return "Odd"; |
||
135 | return "Even"; |
||
136 | return "Mark"; Windows only |
||
137 | return "Space";*/ |
||
138 | QString getParity(); |
||
139 | |||
140 | |||
141 | /*enum StopBitsType { |
||
142 | STOP_1 |
||
143 | STOP_1_5 //WINDOWS ONLY |
||
144 | STOP_2 |
||
145 | };*/ |
||
146 | void setStopBits(const StopBitsType stopBits); |
||
147 | |||
148 | /*getStopBit |
||
149 | return "1" |
||
150 | return "1.5" |
||
151 | return "2"*/ |
||
152 | QString getStopBit(); |
||
153 | |||
154 | |||
155 | /*enum FlowType { |
||
156 | FLOW_OFF |
||
157 | FLOW_HARDWARE |
||
158 | FLOW_XONXOFF |
||
159 | };*/ |
||
160 | void setFlowControl(const FlowType flowControl); |
||
161 | |||
162 | /*getFlowControl |
||
163 | return "None" |
||
164 | return "Hardware" |
||
165 | return "Xon/Xoff"*/ |
||
166 | QString getFlowControl(); |
||
167 | |||
168 | |||
169 | void setTimeout(ulong seconds, ulong milliseconds); |
||
170 | |||
171 | /*getLastErrorToString |
||
172 | return "No Error has occured" |
||
173 | return "Invalid file descriptor (port was not opened correctly)" |
||
174 | return "Unable to allocate memory tables (POSIX)" |
||
175 | return "Caught a non-blocked signal (POSIX)" |
||
176 | return "Operation timed out (POSIX)" |
||
177 | return "The file opened by the port is not a character device (POSIX)" |
||
178 | return "The port detected a break condition" |
||
179 | return "The port detected a framing error (usually caused by incorrect baud rate settings)" |
||
180 | |||
181 | return "There was an I/O error while communicating with the port" |
||
182 | return "Character buffer overrun" |
||
183 | return "Receive buffer overflow" |
||
184 | return "The port detected a parity error in the received data" |
||
185 | return "Transmit buffer overflow" |
||
186 | return "General read operation failure" |
||
187 | return "General write operation failure"*/ |
||
188 | /*QString getLastErrorToString();*/ |
||
189 | |||
190 | /*getLastError |
||
191 | return 0 : No Error has occured |
||
192 | return 1 : Invalid file descriptor (port was not opened correctly) |
||
193 | return 2 : Unable to allocate memory tables (POSIX) |
||
194 | return 3 : Caught a non-blocked signal (POSIX) |
||
195 | return 4 : Operation timed out (POSIX) |
||
196 | return 5 : The file opened by the port is not a character device (POSIX) |
||
197 | return 6 : The port detected a break condition" |
||
198 | return 7 : The port detected a framing error (usually caused by incorrect baud rate settings) |
||
199 | |||
200 | return 8 : There was an I/O error while communicating with the port |
||
201 | return 9 : Character buffer overrun |
||
202 | return 10 : Receive buffer overflow |
||
203 | return 11 : The port detected a parity error in the received data |
||
204 | return 12 : Transmit buffer overflow |
||
205 | return 13 : General read operation failure |
||
206 | return 14 : General write operation failure*/ |
||
207 | /*ulong getLastError();*/ |
||
208 | |||
209 | |||
210 | |||
211 | //Emission |
||
212 | void enableSending(); |
||
213 | void disableSending(); |
||
214 | |||
215 | /*isSendingEnabled |
||
216 | return 0 : sending is not enable |
||
217 | return 1 : sending is enable*/ |
||
218 | bool isSendingEnabled(); |
||
219 | |||
220 | /*sendData |
||
221 | return 1 : add OK |
||
222 | return 2 : port is not open |
||
223 | return 3 : sending is not enable*/ |
||
224 | uchar sendData(QByteArray &dataToSend); |
||
225 | |||
226 | void stopSending(); |
||
227 | |||
228 | //Reception |
||
229 | void enableReceiving(); |
||
230 | void disableReceiving(); |
||
231 | |||
232 | /*isReceivingEnabled |
||
233 | return 0 : receiving is not enable |
||
234 | return 1 : receiving is enable*/ |
||
235 | bool isReceivingEnabled(); |
||
236 | |||
237 | /*receiveData |
||
238 | return 1 : start thread OK |
||
239 | return 2 : port is not open |
||
240 | return 3 : receiving is not enable*/ |
||
241 | uchar receiveData(); |
||
242 | |||
243 | void stopReceiving(); |
||
244 | |||
245 | signals: |
||
246 | void newDataReceived(const QByteArray &dataReceived); |
||
247 | |||
248 | private: |
||
249 | //Variables privees |
||
250 | QextSerialPort extSerialPort; |
||
251 | ThreadSend *threadSend; |
||
252 | ThreadReceive *threadReceive; |
||
253 | |||
254 | bool sendingEnabled; |
||
255 | bool receivingEnabled; |
||
256 | |||
257 | bool closeCalled; |
||
258 | bool saveStateSendEnabled; |
||
259 | bool saveStateReceivedEnabled; |
||
260 | bool saveStateReceiveData; |
||
261 | |||
262 | }; |
||
263 | |||
264 | #endif // MANAGESERIALPORT_H |
||
265 | |||
266 | |||
267 | |||
268 | |||
269 | |||
270 | |||
271 | |||
272 | |||
273 | |||
274 | class ThreadSend : public QThread |
||
275 | { |
||
276 | Q_OBJECT |
||
277 | |||
278 | public: |
||
279 | ThreadSend(QextSerialPort &addressSerialPort); |
||
280 | ~ThreadSend(); |
||
281 | void addDataToSend(QByteArray &dataToAdd); |
||
282 | void stopSending(); |
||
283 | |||
284 | protected: |
||
285 | void run(); |
||
286 | |||
287 | private: |
||
288 | QMutex mutexSend; |
||
289 | QextSerialPort &extSerialPort; |
||
290 | QQueue<QByteArray> dataToSend; |
||
291 | bool stopped; |
||
292 | }; |
||
293 | |||
294 | |||
295 | |||
296 | |||
297 | |||
298 | |||
299 | |||
300 | |||
301 | |||
302 | |||
303 | class ThreadReceive : public QThread |
||
304 | { |
||
305 | Q_OBJECT |
||
306 | |||
307 | public: |
||
308 | ThreadReceive(QextSerialPort &addressSerialPort); |
||
309 | ~ThreadReceive(); |
||
310 | void stopReceiving(); |
||
311 | |||
312 | protected: |
||
313 | void run(); |
||
314 | |||
315 | private : |
||
316 | QMutex mutexReceive; |
||
317 | QextSerialPort &extSerialPort; |
||
318 | bool stopped; |
||
319 | |||
320 | signals: |
||
321 | void newDataReceived(const QByteArray &dataReceived); |
||
322 | }; |