Rev 158 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 158 | Rev 159 | ||
---|---|---|---|
1 | /******************************************************************************************************** |
1 | /******************************************************************************************************** |
2 | * PROGRAM : QSerialPortTerminal |
2 | * PROGRAM : QSerialPortTerminal |
3 | * DATE - TIME : vendredi 03 octobre 2008 - 11h15 |
3 | * DATE - TIME : vendredi 03 octobre 2008 - 11h15 |
4 | * AUTHOR : VIANNEY-LIAUD Philippe ( philippe.vianney.liaud gmail.com ) |
4 | * AUTHOR : VIANNEY-LIAUD Philippe ( philippe.vianney.liaud gmail.com ) |
5 | * FILENAME : ManageSerialPort.cpp |
5 | * FILENAME : ManageSerialPort.cpp |
6 | * LICENSE : GPL |
6 | * LICENSE : GPL |
7 | * COMMENTARY : |
7 | * COMMENTARY : |
8 | ********************************************************************************************************/ |
8 | ********************************************************************************************************/ |
9 | #include <QtDebug> |
9 | #include <QtDebug> |
10 | #include "ManageSerialPort.h" |
10 | #include "ManageSerialPort.h" |
11 | 11 | ||
12 | /******************************************************************************************************** |
12 | /******************************************************************************************************** |
13 | * Classe ManageSerialPort |
13 | * Classe ManageSerialPort |
14 | *****************************************************************************************************/ |
14 | *****************************************************************************************************/ |
15 | 15 | ||
16 | //Constructeur |
16 | //Constructeur |
17 | ManageSerialPort::ManageSerialPort() |
17 | ManageSerialPort::ManageSerialPort() |
18 | { |
18 | { |
19 | //Init pointeur a NULL |
19 | //Init pointeur a NULL |
20 | threadSend = NULL; |
20 | threadSend = NULL; |
21 | threadReceive = NULL; |
21 | threadReceive = NULL; |
22 | 22 | ||
23 | //Init des bool |
23 | //Init des bool |
24 | sendingEnabled = false; |
24 | sendingEnabled = false; |
25 | receivingEnabled = false; |
25 | receivingEnabled = false; |
26 | closeCalled = false; |
26 | closeCalled = false; |
27 | saveStateSendEnabled = false; |
27 | saveStateSendEnabled = false; |
28 | saveStateReceivedEnabled = false; |
28 | saveStateReceivedEnabled = false; |
29 | saveStateReceiveData = false; |
29 | saveStateReceiveData = false; |
30 | } |
30 | } |
31 | 31 | ||
32 | ManageSerialPort::ManageSerialPort(const QString &name, const BaudRateType baudRate, \ |
32 | ManageSerialPort::ManageSerialPort(const QString &name, const BaudRateType baudRate, \ |
33 | const DataBitsType dataBits, const ParityType parity, \ |
33 | const DataBitsType dataBits, const ParityType parity, \ |
34 | const StopBitsType stopBits, const FlowType flowControl, \ |
34 | const StopBitsType stopBits, const FlowType flowControl, \ |
35 | ulong seconds, ulong milliseconds) |
35 | ulong seconds, ulong milliseconds) |
36 | { |
36 | { |
37 | //Init pointeur a NULL |
37 | //Init pointeur a NULL |
38 | threadSend = NULL; |
38 | threadSend = NULL; |
39 | threadReceive = NULL; |
39 | threadReceive = NULL; |
40 | 40 | ||
41 | //Init des bool |
41 | //Init des bool |
42 | sendingEnabled = false; |
42 | sendingEnabled = false; |
43 | receivingEnabled = false; |
43 | receivingEnabled = false; |
44 | closeCalled = false; |
44 | closeCalled = false; |
45 | saveStateSendEnabled = false; |
45 | saveStateSendEnabled = false; |
46 | saveStateReceivedEnabled = false; |
46 | saveStateReceivedEnabled = false; |
47 | saveStateReceiveData = false; |
47 | saveStateReceiveData = false; |
48 | 48 | ||
49 | setPort(name); |
49 | setPort(name); |
50 | setBaudRate(baudRate); |
50 | setBaudRate(baudRate); |
51 | setDataBits(dataBits); |
51 | setDataBits(dataBits); |
52 | setParity(parity); |
52 | setParity(parity); |
53 | setStopBits(stopBits); |
53 | setStopBits(stopBits); |
54 | setFlowControl(flowControl); |
54 | setFlowControl(flowControl); |
55 | setTimeout(seconds, milliseconds); |
55 | setTimeout(seconds, milliseconds); |
56 | } |
56 | } |
57 | 57 | ||
58 | //Destructeur |
58 | //Destructeur |
59 | ManageSerialPort::~ManageSerialPort() |
59 | ManageSerialPort::~ManageSerialPort() |
60 | { |
60 | { |
61 | if (threadSend != NULL) |
61 | if (threadSend != NULL) |
62 | { |
62 | { |
63 | delete threadSend; |
63 | delete threadSend; |
64 | threadSend = NULL; |
64 | threadSend = NULL; |
65 | } |
65 | } |
66 | 66 | ||
67 | if (threadReceive != NULL) |
67 | if (threadReceive != NULL) |
68 | { |
68 | { |
69 | delete threadReceive; |
69 | delete threadReceive; |
70 | threadReceive = NULL; |
70 | threadReceive = NULL; |
71 | } |
71 | } |
72 | 72 | ||
73 | if (isOpen()) |
73 | if (isOpen()) |
74 | extSerialPort.close(); |
74 | extSerialPort.close(); |
75 | } |
75 | } |
76 | 76 | ||
77 | bool ManageSerialPort::open() |
77 | bool ManageSerialPort::open() |
78 | { |
78 | { |
79 | bool res = extSerialPort.open(QIODevice::ReadWrite); |
79 | bool res = extSerialPort.open(QIODevice::ReadWrite); |
80 | 80 | ||
81 | if (closeCalled) |
81 | if (closeCalled) |
82 | { |
82 | { |
83 | if (saveStateSendEnabled) |
83 | if (saveStateSendEnabled) |
84 | enableSending(); |
84 | enableSending(); |
85 | 85 | ||
86 | if (saveStateReceivedEnabled) |
86 | if (saveStateReceivedEnabled) |
87 | enableReceiving(); |
87 | enableReceiving(); |
88 | 88 | ||
89 | if (saveStateReceiveData) |
89 | if (saveStateReceiveData) |
90 | receiveData(); |
90 | receiveData(); |
91 | closeCalled = false; |
91 | closeCalled = false; |
92 | } |
92 | } |
93 | 93 | ||
94 | return res; |
94 | return res; |
95 | } |
95 | } |
96 | 96 | ||
97 | bool ManageSerialPort::open(const QString &name, const BaudRateType baudRate, \ |
97 | bool ManageSerialPort::open(const QString &name, const BaudRateType baudRate, \ |
98 | const DataBitsType dataBits,const ParityType parity, \ |
98 | const DataBitsType dataBits,const ParityType parity, \ |
99 | const StopBitsType stopBits, const FlowType flowControl, \ |
99 | const StopBitsType stopBits, const FlowType flowControl, \ |
100 | ulong seconds, ulong milliseconds) |
100 | ulong seconds, ulong milliseconds) |
101 | { |
101 | { |
102 | setPort(name); |
102 | setPort(name); |
103 | setBaudRate(baudRate); |
103 | setBaudRate(baudRate); |
104 | setDataBits(dataBits); |
104 | setDataBits(dataBits); |
105 | setParity(parity); |
105 | setParity(parity); |
106 | setStopBits(stopBits); |
106 | setStopBits(stopBits); |
107 | setFlowControl(flowControl); |
107 | setFlowControl(flowControl); |
108 | setTimeout(seconds, milliseconds); |
108 | setTimeout(seconds, milliseconds); |
109 | 109 | ||
110 | bool res = extSerialPort.open(QIODevice::ReadWrite); |
110 | bool res = extSerialPort.open(QIODevice::ReadWrite); |
111 | 111 | ||
112 | 112 | ||
113 | return res; |
113 | return res; |
114 | } |
114 | } |
115 | 115 | ||
116 | bool ManageSerialPort::isOpen() |
116 | bool ManageSerialPort::isOpen() |
117 | { |
117 | { |
118 | return extSerialPort.isOpen(); |
118 | return extSerialPort.isOpen(); |
119 | } |
119 | } |
120 | 120 | ||
121 | void ManageSerialPort::close() |
121 | void ManageSerialPort::close() |
122 | { |
122 | { |
123 | closeCalled = true; |
123 | closeCalled = true; |
124 | saveStateSendEnabled = isSendingEnabled(); |
124 | saveStateSendEnabled = isSendingEnabled(); |
125 | saveStateReceivedEnabled = isReceivingEnabled(); |
125 | saveStateReceivedEnabled = isReceivingEnabled(); |
126 | 126 | ||
127 | disableSending(); |
127 | disableSending(); |
128 | disableReceiving(); |
128 | disableReceiving(); |
129 | 129 | ||
130 | extSerialPort.close(); |
130 | extSerialPort.close(); |
131 | } |
131 | } |
132 | 132 | ||
133 | void ManageSerialPort::setPort(const QString &name) |
133 | void ManageSerialPort::setPort(const QString &name) |
134 | { |
134 | { |
135 | extSerialPort.setPortName(name); |
135 | extSerialPort.setPortName(name); |
136 | } |
136 | } |
137 | 137 | ||
138 | QString ManageSerialPort::getPort() |
138 | QString ManageSerialPort::getPort() |
139 | { |
139 | { |
140 | return extSerialPort.portName(); |
140 | return extSerialPort.portName(); |
141 | } |
141 | } |
142 | 142 | ||
143 | 143 | ||
144 | void ManageSerialPort::setBaudRate(const BaudRateType baudRate) |
144 | void ManageSerialPort::setBaudRate(const BaudRateType baudRate) |
145 | { |
145 | { |
146 | extSerialPort.setBaudRate(baudRate); |
146 | extSerialPort.setBaudRate(baudRate); |
147 | } |
147 | } |
148 | 148 | ||
149 | QString ManageSerialPort::getBaudRate() |
149 | QString ManageSerialPort::getBaudRate() |
150 | { |
150 | { |
151 | switch (extSerialPort.baudRate()) |
151 | switch (extSerialPort.baudRate()) |
152 | { |
152 | { |
153 | case BAUD50: |
153 | case BAUD50: |
154 | return QString("50"); |
154 | return QString("50"); |
155 | case BAUD75: |
155 | case BAUD75: |
156 | return QString("75"); |
156 | return QString("75"); |
157 | case BAUD110: |
157 | case BAUD110: |
158 | return QString("110"); |
158 | return QString("110"); |
159 | case BAUD134: |
159 | case BAUD134: |
160 | return QString("134"); |
160 | return QString("134"); |
161 | case BAUD150: |
161 | case BAUD150: |
162 | return QString("150"); |
162 | return QString("150"); |
163 | case BAUD200: |
163 | case BAUD200: |
164 | return QString("200"); |
164 | return QString("200"); |
165 | case BAUD300: |
165 | case BAUD300: |
166 | return QString("300"); |
166 | return QString("300"); |
167 | case BAUD600: |
167 | case BAUD600: |
168 | return QString("600"); |
168 | return QString("600"); |
169 | case BAUD1200: |
169 | case BAUD1200: |
170 | return QString("1200"); |
170 | return QString("1200"); |
171 | case BAUD1800: |
171 | case BAUD1800: |
172 | return QString("1800"); |
172 | return QString("1800"); |
173 | case BAUD2400: |
173 | case BAUD2400: |
174 | return QString("2400"); |
174 | return QString("2400"); |
175 | case BAUD4800: |
175 | case BAUD4800: |
176 | return QString("4800"); |
176 | return QString("4800"); |
177 | case BAUD9600: |
177 | case BAUD9600: |
178 | return QString("9600"); |
178 | return QString("9600"); |
179 | case BAUD14400: |
179 | case BAUD14400: |
180 | return QString("14400"); |
180 | return QString("14400"); |
181 | case BAUD19200: |
181 | case BAUD19200: |
182 | return QString("19200"); |
182 | return QString("19200"); |
183 | case BAUD38400: |
183 | case BAUD38400: |
184 | return QString("38400"); |
184 | return QString("38400"); |
185 | case BAUD56000: |
185 | case BAUD56000: |
186 | return QString("56000"); |
186 | return QString("56000"); |
187 | case BAUD57600: |
187 | case BAUD57600: |
188 | return QString("57600"); |
188 | return QString("57600"); |
189 | case BAUD76800: |
189 | case BAUD76800: |
190 | return QString("76800"); |
190 | return QString("76800"); |
191 | case BAUD115200: |
191 | case BAUD115200: |
192 | return QString("115200"); |
192 | return QString("115200"); |
193 | case BAUD128000: |
193 | case BAUD128000: |
194 | return QString("128000"); |
194 | return QString("128000"); |
195 | case BAUD256000: |
195 | case BAUD256000: |
196 | return QString("256000"); |
196 | return QString("256000"); |
197 | } |
197 | } |
198 | return 0; |
198 | return 0; |
199 | } |
199 | } |
200 | 200 | ||
201 | 201 | ||
202 | void ManageSerialPort::setDataBits(const DataBitsType dataBits) |
202 | void ManageSerialPort::setDataBits(const DataBitsType dataBits) |
203 | { |
203 | { |
204 | extSerialPort.setDataBits(dataBits); |
204 | extSerialPort.setDataBits(dataBits); |
205 | } |
205 | } |
206 | 206 | ||
207 | QChar ManageSerialPort::getDataBits() |
207 | QChar ManageSerialPort::getDataBits() |
208 | { |
208 | { |
209 | switch (extSerialPort.dataBits()) |
209 | switch (extSerialPort.dataBits()) |
210 | { |
210 | { |
211 | case DATA_5: |
211 | case DATA_5: |
212 | return QChar('5'); |
212 | return QChar('5'); |
213 | case DATA_6: |
213 | case DATA_6: |
214 | return QChar('6'); |
214 | return QChar('6'); |
215 | case DATA_7: |
215 | case DATA_7: |
216 | return QChar('7'); |
216 | return QChar('7'); |
217 | case DATA_8: |
217 | case DATA_8: |
218 | return QChar('8'); |
218 | return QChar('8'); |
219 | } |
219 | } |
220 | return 0; |
220 | return 0; |
221 | } |
221 | } |
222 | 222 | ||
223 | 223 | ||
224 | void ManageSerialPort::setParity(const ParityType parity) |
224 | void ManageSerialPort::setParity(const ParityType parity) |
225 | { |
225 | { |
226 | extSerialPort.setParity(parity); |
226 | extSerialPort.setParity(parity); |
227 | } |
227 | } |
228 | 228 | ||
229 | QString ManageSerialPort::getParity() |
229 | QString ManageSerialPort::getParity() |
230 | { |
230 | { |
231 | switch (extSerialPort.parity()) |
231 | switch (extSerialPort.parity()) |
232 | { |
232 | { |
233 | case PAR_NONE: |
233 | case PAR_NONE: |
234 | return QString(tr("None")); |
234 | return QString(tr("None")); |
235 | case PAR_ODD: |
235 | case PAR_ODD: |
236 | return QString(tr("Odd")); |
236 | return QString(tr("Odd")); |
237 | case PAR_EVEN: |
237 | case PAR_EVEN: |
238 | return QString(tr("Even")); |
238 | return QString(tr("Even")); |
239 | case PAR_MARK: |
239 | case PAR_MARK: |
240 | return QString(tr("Mark")); |
240 | return QString(tr("Mark")); |
241 | case PAR_SPACE: |
241 | case PAR_SPACE: |
242 | return QString(tr("Space")); |
242 | return QString(tr("Space")); |
243 | } |
243 | } |
244 | return 0; |
244 | return 0; |
245 | } |
245 | } |
246 | 246 | ||
247 | 247 | ||
248 | void ManageSerialPort::setStopBits(const StopBitsType stopBits) |
248 | void ManageSerialPort::setStopBits(const StopBitsType stopBits) |
249 | { |
249 | { |
250 | extSerialPort.setStopBits(stopBits); |
250 | extSerialPort.setStopBits(stopBits); |
251 | } |
251 | } |
252 | 252 | ||
253 | QString ManageSerialPort::getStopBit() |
253 | QString ManageSerialPort::getStopBit() |
254 | { |
254 | { |
255 | switch (extSerialPort.stopBits()) |
255 | switch (extSerialPort.stopBits()) |
256 | { |
256 | { |
257 | case STOP_1: |
257 | case STOP_1: |
258 | return QString("1"); |
258 | return QString("1"); |
259 | case STOP_1_5: |
259 | case STOP_1_5: |
260 | return QString("1.5"); |
260 | return QString("1.5"); |
261 | case STOP_2: |
261 | case STOP_2: |
262 | return QString("2"); |
262 | return QString("2"); |
263 | } |
263 | } |
264 | return 0; |
264 | return 0; |
265 | } |
265 | } |
266 | 266 | ||
267 | 267 | ||
268 | void ManageSerialPort::setFlowControl(const FlowType flowControl) |
268 | void ManageSerialPort::setFlowControl(const FlowType flowControl) |
269 | { |
269 | { |
270 | extSerialPort.setFlowControl(flowControl); |
270 | extSerialPort.setFlowControl(flowControl); |
271 | } |
271 | } |
272 | 272 | ||
273 | QString ManageSerialPort::getFlowControl() |
273 | QString ManageSerialPort::getFlowControl() |
274 | { |
274 | { |
275 | switch (extSerialPort.flowControl()) |
275 | switch (extSerialPort.flowControl()) |
276 | { |
276 | { |
277 | case FLOW_OFF: |
277 | case FLOW_OFF: |
278 | return QString(tr("None")); |
278 | return QString(tr("None")); |
279 | case FLOW_HARDWARE : |
279 | case FLOW_HARDWARE : |
280 | return QString(tr("Hardware")); |
280 | return QString(tr("Hardware")); |
281 | case FLOW_XONXOFF : |
281 | case FLOW_XONXOFF : |
282 | return QString(tr("Xon/Xoff")); |
282 | return QString(tr("Xon/Xoff")); |
283 | } |
283 | } |
284 | return 0; |
284 | return 0; |
285 | } |
285 | } |
286 | 286 | ||
287 | 287 | ||
288 | void ManageSerialPort::setTimeout(ulong seconds, ulong milliseconds) |
288 | void ManageSerialPort::setTimeout(ulong seconds, ulong milliseconds) |
289 | { |
289 | { |
290 | extSerialPort.setTimeout(seconds,milliseconds); |
290 | extSerialPort.setTimeout(seconds,milliseconds); |
291 | } |
291 | } |
292 | 292 | ||
293 | /* |
293 | /* |
294 | QString ManageSerialPort::getLastErrorToString() |
294 | QString ManageSerialPort::getLastErrorToString() |
295 | { |
295 | { |
296 | ulong res = extSerialPort.lastError(); |
296 | ulong res = extSerialPort.lastError(); |
297 | switch (res) |
297 | switch (res) |
298 | { |
298 | { |
299 | case E_NO_ERROR: |
299 | case E_NO_ERROR: |
300 | return QString(tr("No Error has occured")); |
300 | return QString(tr("No Error has occured")); |
301 | case E_INVALID_FD: |
301 | case E_INVALID_FD: |
302 | return QString(tr("Invalid file descriptor (port was not opened correctly)")); |
302 | return QString(tr("Invalid file descriptor (port was not opened correctly)")); |
303 | case E_NO_MEMORY: |
303 | case E_NO_MEMORY: |
304 | return QString(tr("Unable to allocate memory tables (POSIX)")); |
304 | return QString(tr("Unable to allocate memory tables (POSIX)")); |
305 | case E_CAUGHT_NON_BLOCKED_SIGNAL: |
305 | case E_CAUGHT_NON_BLOCKED_SIGNAL: |
306 | return QString(tr("Caught a non-blocked signal (POSIX)")); |
306 | return QString(tr("Caught a non-blocked signal (POSIX)")); |
307 | case E_PORT_TIMEOUT: |
307 | case E_PORT_TIMEOUT: |
308 | return QString(tr("Operation timed out (POSIX)")); |
308 | return QString(tr("Operation timed out (POSIX)")); |
309 | case E_INVALID_DEVICE: |
309 | case E_INVALID_DEVICE: |
310 | return QString(tr("The file opened by the port is not a character device (POSIX)")); |
310 | return QString(tr("The file opened by the port is not a character device (POSIX)")); |
311 | case E_BREAK_CONDITION: |
311 | case E_BREAK_CONDITION: |
312 | return QString(tr("The port detected a break condition")); |
312 | return QString(tr("The port detected a break condition")); |
313 | case E_FRAMING_ERROR: |
313 | case E_FRAMING_ERROR: |
314 | return QString(tr("The port detected a framing error (usually caused by incorrect baud rate settings)")); |
314 | return QString(tr("The port detected a framing error (usually caused by incorrect baud rate settings)")); |
315 | case E_IO_ERROR: |
315 | case E_IO_ERROR: |
316 | return QString(tr("There was an I/O error while communicating with the port")); |
316 | return QString(tr("There was an I/O error while communicating with the port")); |
317 | case E_BUFFER_OVERRUN: |
317 | case E_BUFFER_OVERRUN: |
318 | return QString(tr("Character buffer overrun")); |
318 | return QString(tr("Character buffer overrun")); |
319 | case E_RECEIVE_OVERFLOW: |
319 | case E_RECEIVE_OVERFLOW: |
320 | return QString(tr("Receive buffer overflow")); |
320 | return QString(tr("Receive buffer overflow")); |
321 | case E_RECEIVE_PARITY_ERROR: |
321 | case E_RECEIVE_PARITY_ERROR: |
322 | return QString(tr("The port detected a parity error in the received data")); |
322 | return QString(tr("The port detected a parity error in the received data")); |
323 | case E_TRANSMIT_OVERFLOW: |
323 | case E_TRANSMIT_OVERFLOW: |
324 | return QString(tr("Transmit buffer overflow")); |
324 | return QString(tr("Transmit buffer overflow")); |
325 | case E_READ_FAILED: |
325 | case E_READ_FAILED: |
326 | return QString(tr("General read operation failure")); |
326 | return QString(tr("General read operation failure")); |
327 | case E_WRITE_FAILED: |
327 | case E_WRITE_FAILED: |
328 | return QString(tr("General write operation failure")); |
328 | return QString(tr("General write operation failure")); |
329 | } |
329 | } |
330 | return 0; |
330 | return 0; |
331 | }*/ |
331 | }*/ |
332 | 332 | ||
333 | /* |
333 | /* |
334 | ulong ManageSerialPort::getLastError() |
334 | ulong ManageSerialPort::getLastError() |
335 | { |
335 | { |
336 | return extSerialPort.lastError(); |
336 | return extSerialPort.lastError(); |
337 | } |
337 | } |
338 | */ |
338 | */ |
339 | 339 | ||
340 | 340 | ||
341 | void ManageSerialPort::enableSending() |
341 | void ManageSerialPort::enableSending() |
342 | { |
342 | { |
343 | if (!sendingEnabled && threadSend == NULL) //Si l'envoi n'est pas active && si threadSend n'est pas alloue |
343 | if (!sendingEnabled && threadSend == NULL) //Si l'envoi n'est pas active && si threadSend n'est pas alloue |
344 | { |
344 | { |
345 | threadSend = new ThreadSend(extSerialPort); |
345 | threadSend = new ThreadSend(extSerialPort); |
346 | sendingEnabled = true; |
346 | sendingEnabled = true; |
347 | } |
347 | } |
348 | } |
348 | } |
349 | 349 | ||
350 | void ManageSerialPort::disableSending() |
350 | void ManageSerialPort::disableSending() |
351 | { |
351 | { |
352 | if (sendingEnabled && threadSend != NULL) //Si l'envoi est active && si threadSend est alloue |
352 | if (sendingEnabled && threadSend != NULL) //Si l'envoi est active && si threadSend est alloue |
353 | { |
353 | { |
354 | delete (threadSend); |
354 | delete (threadSend); |
355 | threadSend = NULL; |
355 | threadSend = NULL; |
356 | sendingEnabled = false; |
356 | sendingEnabled = false; |
357 | } |
357 | } |
358 | } |
358 | } |
359 | 359 | ||
360 | bool ManageSerialPort::isSendingEnabled() |
360 | bool ManageSerialPort::isSendingEnabled() |
361 | { |
361 | { |
362 | return sendingEnabled; |
362 | return sendingEnabled; |
363 | } |
363 | } |
364 | 364 | ||
365 | uchar ManageSerialPort::sendData(QByteArray &dataToSend) |
365 | uchar ManageSerialPort::sendData(QByteArray &dataToSend) |
366 | { |
366 | { |
367 | if (!isOpen()) //Si le port n'est pas ouvert |
367 | if (!isOpen()) //Si le port n'est pas ouvert |
368 | return 2; |
368 | return 2; |
369 | 369 | ||
370 | if (!sendingEnabled || threadSend == NULL) //Si l'envoi n'est pas active || si threadSend n'est pas alloue |
370 | if (!sendingEnabled || threadSend == NULL) //Si l'envoi n'est pas active || si threadSend n'est pas alloue |
371 | return 3; |
371 | return 3; |
372 | 372 | ||
373 | threadSend->addDataToSend(dataToSend); //Ajout des donnees a envoyer |
373 | threadSend->addDataToSend(dataToSend); //Ajout des donnees a envoyer |
374 | return 1; |
374 | return 1; |
375 | } |
375 | } |
376 | 376 | ||
377 | void ManageSerialPort::stopSending() |
377 | void ManageSerialPort::stopSending() |
378 | { |
378 | { |
379 | if (!sendingEnabled || threadSend == NULL) //Si l'envoi n'est pas active || si threadSend n'est pas été alloue |
379 | if (!sendingEnabled || threadSend == NULL) //Si l'envoi n'est pas active || si threadSend n'est pas été alloue |
380 | return; |
380 | return; |
381 | 381 | ||
382 | if (threadSend->isRunning()) //si un envoi est en cour |
382 | if (threadSend->isRunning()) //si un envoi est en cour |
383 | { |
383 | { |
384 | threadSend->stopSending(); //on donne l'ordre d'arreter l'envoi |
384 | threadSend->stopSending(); //on donne l'ordre d'arreter l'envoi |
385 | 385 | ||
386 | long tmp = ULONG_MAX; |
386 | long tmp = ULONG_MAX; |
387 | threadSend->wait(tmp); //on attend l'arret |
387 | threadSend->wait(tmp); //on attend l'arret |
388 | } |
388 | } |
389 | } |
389 | } |
390 | 390 | ||
391 | 391 | ||
392 | 392 | ||
393 | void ManageSerialPort::enableReceiving() |
393 | void ManageSerialPort::enableReceiving() |
394 | { |
394 | { |
395 | if (!receivingEnabled && threadReceive == NULL) //Si la reception n'est pas active && si threadReceive n'est pas alloue |
395 | if (!receivingEnabled && threadReceive == NULL) //Si la reception n'est pas active && si threadReceive n'est pas alloue |
396 | { |
396 | { |
397 | threadReceive = new ThreadReceive(extSerialPort); |
397 | threadReceive = new ThreadReceive(extSerialPort); |
398 | connect(threadReceive, SIGNAL(newDataReceived(const QByteArray &)), this, SIGNAL(newDataReceived(const QByteArray &))); |
398 | connect(threadReceive, SIGNAL(newDataReceived(const QByteArray &)), this, SIGNAL(newDataReceived(const QByteArray &))); |
399 | receivingEnabled = true; |
399 | receivingEnabled = true; |
400 | } |
400 | } |
401 | } |
401 | } |
402 | 402 | ||
403 | void ManageSerialPort::disableReceiving() |
403 | void ManageSerialPort::disableReceiving() |
404 | { |
404 | { |
405 | if (receivingEnabled && threadReceive != NULL) //Si la reception est pas active && si threadReceive est alloue |
405 | if (receivingEnabled && threadReceive != NULL) //Si la reception est pas active && si threadReceive est alloue |
406 | { |
406 | { |
407 | delete (threadReceive); |
407 | delete (threadReceive); |
408 | threadReceive = NULL; |
408 | threadReceive = NULL; |
409 | receivingEnabled = false; |
409 | receivingEnabled = false; |
410 | } |
410 | } |
411 | } |
411 | } |
412 | 412 | ||
413 | bool ManageSerialPort::isReceivingEnabled() |
413 | bool ManageSerialPort::isReceivingEnabled() |
414 | { |
414 | { |
415 | return receivingEnabled; |
415 | return receivingEnabled; |
416 | } |
416 | } |
417 | 417 | ||
418 | uchar ManageSerialPort::receiveData() |
418 | uchar ManageSerialPort::receiveData() |
419 | { |
419 | { |
420 | if (!isOpen()) //Si le port n'est pas ouvert |
420 | if (!isOpen()) //Si le port n'est pas ouvert |
421 | return 2; |
421 | return 2; |
422 | if (!receivingEnabled || threadReceive == NULL) //Si la reception n'est pas active || si threadReceive n'est pas été alloue |
422 | if (!receivingEnabled || threadReceive == NULL) //Si la reception n'est pas active || si threadReceive n'est pas été alloue |
423 | return 3; |
423 | return 3; |
424 | 424 | ||
425 | if (!threadReceive->isRunning()) |
425 | if (!threadReceive->isRunning()) |
426 | { |
426 | { |
427 | saveStateReceiveData = true; |
427 | saveStateReceiveData = true; |
428 | threadReceive->start(); //Demarrage du thread de reception |
428 | threadReceive->start(); //Demarrage du thread de reception |
429 | } |
429 | } |
430 | return 1; |
430 | return 1; |
431 | } |
431 | } |
432 | 432 | ||
433 | void ManageSerialPort::stopReceiving() |
433 | void ManageSerialPort::stopReceiving() |
434 | { |
434 | { |
435 | if (!receivingEnabled || threadReceive == NULL) //Si la reception n'est pas active || si threadReceive n'est pas alloue |
435 | if (!receivingEnabled || threadReceive == NULL) //Si la reception n'est pas active || si threadReceive n'est pas alloue |
436 | return; |
436 | return; |
437 | 437 | ||
438 | if (threadReceive->isRunning()) //Si le thread de reception est en fonctionnement |
438 | if (threadReceive->isRunning()) //Si le thread de reception est en fonctionnement |
439 | { |
439 | { |
440 | saveStateReceiveData = false; |
440 | saveStateReceiveData = false; |
441 | threadReceive->stopReceiving(); //on donne l'ordre d'arreter la reception |
441 | threadReceive->stopReceiving(); //on donne l'ordre d'arreter la reception |
442 | 442 | ||
443 | long tmp = ULONG_MAX; |
443 | long tmp = ULONG_MAX; |
444 | threadReceive->wait(tmp); //on attend l'arret |
444 | threadReceive->wait(tmp); //on attend l'arret |
445 | } |
445 | } |
446 | } |
446 | } |
447 | 447 | ||
448 | 448 | ||
449 | 449 | ||
450 | 450 | ||
451 | 451 | ||
452 | 452 | ||
453 | 453 | ||
454 | 454 | ||
455 | 455 | ||
456 | 456 | ||
457 | 457 | ||
458 | /******************************************************************************************************** |
458 | /******************************************************************************************************** |
459 | * Classe ThreadSend |
459 | * Classe ThreadSend |
460 | *****************************************************************************************************/ |
460 | *****************************************************************************************************/ |
461 | 461 | ||
462 | ThreadSend::ThreadSend(QextSerialPort &addressSerialPort) : extSerialPort(addressSerialPort) |
462 | ThreadSend::ThreadSend(QextSerialPort &addressSerialPort) : extSerialPort(addressSerialPort) |
463 | { |
463 | { |
464 | dataToSend.clear(); |
464 | dataToSend.clear(); |
465 | stopped=false; |
465 | stopped=false; |
466 | } |
466 | } |
467 | 467 | ||
468 | ThreadSend::~ThreadSend() |
468 | ThreadSend::~ThreadSend() |
469 | { |
469 | { |
470 | if (isRunning()) |
470 | if (isRunning()) |
471 | { |
471 | { |
472 | stopSending(); |
472 | stopSending(); |
473 | wait(); |
473 | wait(); |
474 | } |
474 | } |
475 | } |
475 | } |
476 | 476 | ||
477 | 477 | ||
478 | void ThreadSend::addDataToSend(QByteArray &dataToAdd) |
478 | void ThreadSend::addDataToSend(QByteArray &dataToAdd) |
479 | { |
479 | { |
480 | QMutexLocker locker(&mutexSend); |
480 | QMutexLocker locker(&mutexSend); |
481 | for (int i=0; i<dataToAdd.size(); i++) |
481 | for (int i=0; i<dataToAdd.size(); i++) |
482 | dataToSend.enqueue(QByteArray(1,dataToAdd.at(i))); |
482 | dataToSend.enqueue(QByteArray(1,dataToAdd.at(i))); |
483 | 483 | ||
484 | if (!isRunning()) |
484 | if (!isRunning()) |
485 | start(); |
485 | start(); |
486 | } |
486 | } |
487 | 487 | ||
488 | void ThreadSend::stopSending() |
488 | void ThreadSend::stopSending() |
489 | { |
489 | { |
490 | stopped=true; |
490 | stopped=true; |
491 | } |
491 | } |
492 | 492 | ||
493 | void ThreadSend::run() |
493 | void ThreadSend::run() |
494 | { |
494 | { |
495 | QByteArray byteArray; |
495 | QByteArray byteArray; |
496 | 496 | ||
497 | forever |
497 | forever |
498 | { |
498 | { |
499 | if (dataToSend.isEmpty() || stopped) |
499 | if (dataToSend.isEmpty() || stopped) |
500 | { |
500 | { |
501 | stopped = false; |
501 | stopped = false; |
502 | break; |
502 | break; |
503 | } |
503 | } |
504 | mutexSend.lock(); |
504 | mutexSend.lock(); |
505 | byteArray = dataToSend.dequeue(); |
505 | byteArray = dataToSend.dequeue(); |
506 | mutexSend.unlock(); |
506 | mutexSend.unlock(); |
507 | 507 | ||
508 | extSerialPort.write(byteArray, 1); |
508 | extSerialPort.write(byteArray, 1); |
509 | } |
509 | } |
510 | } |
510 | } |
511 | 511 | ||
512 | 512 | ||
513 | 513 | ||
514 | 514 | ||
515 | 515 | ||
516 | /******************************************************************************************************** |
516 | /******************************************************************************************************** |
517 | * Classe ThreadReceive - A TERMINER |
517 | * Classe ThreadReceive - A TERMINER |
518 | *****************************************************************************************************/ |
518 | *****************************************************************************************************/ |
519 | 519 | ||
520 | ThreadReceive::ThreadReceive(QextSerialPort &addressSerialPort) : extSerialPort(addressSerialPort) |
520 | ThreadReceive::ThreadReceive(QextSerialPort &addressSerialPort) : extSerialPort(addressSerialPort) |
521 | { |
521 | { |
522 | stopped=false; |
522 | stopped=false; |
523 | } |
523 | } |
524 | 524 | ||
525 | ThreadReceive::~ThreadReceive() |
525 | ThreadReceive::~ThreadReceive() |
526 | { |
526 | { |
527 | if (isRunning()) |
527 | if (isRunning()) |
528 | { |
528 | { |
529 | stopReceiving(); |
529 | stopReceiving(); |
530 | wait(); |
530 | wait(); |
531 | } |
531 | } |
532 | } |
532 | } |
533 | 533 | ||
534 | void ThreadReceive::stopReceiving() |
534 | void ThreadReceive::stopReceiving() |
535 | { |
535 | { |
536 | stopped = true; |
536 | stopped = true; |
537 | } |
537 | } |
538 | 538 | ||
539 | void ThreadReceive::run() |
539 | void ThreadReceive::run() |
540 | { |
540 | { |
541 | int numBytes=0; |
541 | int numBytes=0; |
542 | char data[1024]; |
542 | char data[1024]; |
543 | QByteArray dataReceived; |
543 | QByteArray dataReceived; |
544 | 544 | ||
545 | forever |
545 | forever |
546 | { |
546 | { |
547 | if (stopped) |
547 | if (stopped) |
548 | { |
548 | { |
549 | stopped = false; |
549 | stopped = false; |
550 | break; |
550 | break; |
551 | } |
551 | } |
552 | 552 | ||
553 | mutexReceive.lock(); |
553 | mutexReceive.lock(); |
554 | 554 | ||
555 | numBytes = extSerialPort.bytesAvailable(); |
555 | numBytes = extSerialPort.bytesAvailable(); |
556 | if (numBytes > 0) |
556 | if (numBytes > 0) |
557 | { |
557 | { |
558 | extSerialPort.read(data, numBytes); |
558 | extSerialPort.read(data, numBytes); |
559 | 559 | ||
560 | for (int xy=1; xy < numBytes; xy++) |
560 | for (int xy=1; xy < numBytes; xy++) |
561 | { |
561 | { |
562 | if (data[xy] == 0) |
562 | if (data[xy] == 0) |
563 | { |
563 | { |
564 | data[xy] = 127; |
564 | data[xy] = 'a'; |
565 | } |
565 | } |
566 | } |
566 | } |
567 | 567 | ||
568 | data[numBytes]='\0'; |
568 | data[numBytes]='\0'; |
569 | 569 | ||
570 | dataReceived = data; |
570 | dataReceived = data; |
571 | 571 | ||
572 | emit newDataReceived(dataReceived); |
572 | emit newDataReceived(dataReceived); |
573 | } |
573 | } |
574 | mutexReceive.unlock(); |
574 | mutexReceive.unlock(); |
575 | } |
575 | } |
576 | } |
576 | } |
577 | 577 | ||
578 | 578 |