[a0a02d]: runtime / wserial.cpp  Maximize  Restore  History

Download this file

467 lines (387 with data), 11.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (C) 2008-2011 David Sugar, Tycho Softworks.
//
// This file is part of GNU Bayonne.
//
// GNU Bayonne is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// GNU Bayonne is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GNU Bayonne. If not, see <http://www.gnu.org/licenses/>.
#include <bayonne-config.h>
#include <ucommon/ucommon.h>
#include <ucommon/export.h>
#include <bayonne.h>
#ifdef _MSWINDOWS_
#include <io.h>
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
using namespace BAYONNE_NAMESPACE;
using namespace UCOMMON_NAMESPACE;
class __LOCAL serial : public Serial
{
private:
HANDLE fd;
DCB original, current;
void restore(void);
bool set(const char *format);
size_t get(void *addr, size_t len);
size_t put(void *addr, size_t len);
void dtr(timeout_t toggle_time);
void bin(size_t size, timeout_t timeout);
void text(char nl1, char nl2);
void clear(void);
bool flush(timeout_t timeout);
bool wait(timeout_t timeout);
void sync(void);
operator bool();
bool operator!();
public:
serial(const char *name);
~serial();
};
serial::serial(const char *name) : Serial()
{
fd = CreateFile(name,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING,
NULL);
if(fd == INVALID_HANDLE_VALUE) {
return;
}
current.DCBlength = sizeof(DCB);
original.DCBlength = sizeof(DCB);
GetCommState(fd, &original);
GetCommState(fd, &current);
current.DCBlength = sizeof(DCB);
current.BaudRate = 1200;
current.Parity = NOPARITY;
current.ByteSize = 8;
current.XonChar = ASCII_XON;
current.XoffChar = ASCII_XOFF;
current.XonLim = 100;
current.XoffLim = 100;
current.fOutxDsrFlow = 0;
current.fDtrControl = DTR_CONTROL_ENABLE;
current.fOutxCtsFlow = 1;
current.fRtsControl = RTS_CONTROL_ENABLE;
current.fInX = current.fOutX = 0;
current.fBinary = true;
current.fParity = true;
SetCommState(fd, &current);
}
serial::~serial()
{
if(fd == INVALID_HANDLE_VALUE)
return;
restore();
CloseHandle(fd);
fd = INVALID_HANDLE_VALUE;
}
serial::operator bool()
{
if(fd == INVALID_HANDLE_VALUE)
return false;
return true;
}
bool serial::operator!()
{
if(fd == INVALID_HANDLE_VALUE)
return true;
return false;
}
void serial::restore(void)
{
if(fd == INVALID_HANDLE_VALUE)
return;
memcpy(&current, &original, sizeof(current));
SetCommState(fd, &current);
}
bool serial::set(const char *format)
{
assert(format != NULL);
if(fd == INVALID_HANDLE_VALUE)
return false;
unsigned long opt;
char buf[256];
String::set(buf, sizeof(buf), format);
char *cp = strtok(buf, ",");
while(cp) {
switch(*cp) {
case 'n':
case 'N':
current.Parity = NOPARITY;
break;
case 'e':
case 'E':
current.Parity = EVENPARITY;
break;
case 'o':
case 'O':
current.Parity = ODDPARITY;
break;
case 's':
case 'S':
current.fInX = current.fOutX = TRUE;
current.fOutxCtsFlow = FALSE;
current.fRtsControl = FALSE;
break;
case 'H':
case 'h':
current.fInX = current.fOutX = FALSE;
current.fOutxCtsFlow = TRUE;
current.fRtsControl = RTS_CONTROL_HANDSHAKE;
break;
case 'b':
case 'B':
current.fInX = current.fOutX = TRUE;
current.fOutxCtsFlow = TRUE;
current.fRtsControl = RTS_CONTROL_HANDSHAKE;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
opt = atol(cp);
switch(opt) {
case 1:
current.StopBits = ONESTOPBIT;
break;
case 2:
current.StopBits = TWOSTOPBITS;
break;
case 5:
case 6:
case 7:
case 8:
current.ByteSize = (unsigned char)opt;
break;
case 110:
case 300:
case 600:
case 1200:
case 2400:
case 4800:
case 9600:
case 14400:
case 19200:
case 38400:
case 57600:
case 115200:
case 128000:
case 256000:
current.BaudRate = opt;
break;
default:
error = EINVAL;
return false;
}
break;
default:
error = EINVAL;
return false;
}
cp = strtok(NULL, ",");
}
current.DCBlength = sizeof(DCB);
SetCommState(fd, &current);
return true;
}
size_t serial::get(void *data, size_t len)
{
DWORD dwLength = 0, dwError, dwReadLength;
COMSTAT cs;
OVERLAPPED ol;
// Return zero if handle is invalid
if(fd == INVALID_HANDLE_VALUE)
return 0;
// Read max length or only what is available
ClearCommError(fd, &dwError, &cs);
// If not requiring an exact byte count, get whatever is available
if(len > (size_t)cs.cbInQue)
dwReadLength = cs.cbInQue;
else
dwReadLength = len;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(dwReadLength > 0) {
if(ReadFile(fd, data, dwReadLength, &dwLength, &ol) == FALSE) {
if(GetLastError() == ERROR_IO_PENDING) {
WaitForSingleObject(ol.hEvent, INFINITE);
GetOverlappedResult(fd, &ol, &dwLength, TRUE);
}
else
ClearCommError(fd, &dwError, &cs);
}
}
if(ol.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(ol.hEvent);
return dwLength;
}
size_t serial::put(void *data, size_t len)
{
COMSTAT cs;
unsigned long dwError = 0;
OVERLAPPED ol;
// Clear the com port of any error condition prior to read
ClearCommError(fd, &dwError, &cs);
unsigned long retSize = 0;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(WriteFile(fd, data, len, &retSize, &ol) == FALSE) {
if(GetLastError() == ERROR_IO_PENDING) {
WaitForSingleObject(ol.hEvent, INFINITE);
GetOverlappedResult(fd, &ol, &retSize, TRUE);
}
else
ClearCommError(fd, &dwError, &cs);
}
if(ol.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(ol.hEvent);
return retSize;
}
bool serial::wait(timeout_t timeout)
{
unsigned long dwError;
COMSTAT cs;
if(fd == INVALID_HANDLE_VALUE)
return false;
ClearCommError(fd, &dwError, &cs);
if(timeout == 0)
return (0 != cs.cbInQue);
OVERLAPPED ol;
DWORD dwMask;
DWORD dwEvents = 0;
BOOL suc;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
dwMask = EV_RXCHAR;
SetCommMask(fd, dwMask);
// let's wait for event or timeout
if((suc = WaitCommEvent(fd, &dwEvents, &ol)) == FALSE) {
if(GetLastError() == ERROR_IO_PENDING) {
DWORD transferred;
dwError = WaitForSingleObject(ol.hEvent, timeout);
if (dwError != WAIT_OBJECT_0)
SetCommMask(fd, 0);
suc = GetOverlappedResult(fd, &ol, &transferred, TRUE);
if (suc)
suc = (dwEvents & dwMask) ? TRUE : FALSE;
}
else
ClearCommError(fd, &dwError, &cs);
}
if(ol.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(ol.hEvent);
if(suc == FALSE)
return false;
return true;
}
bool serial::flush(timeout_t timeout)
{
unsigned long dwError;
COMSTAT cs;
if(fd == INVALID_HANDLE_VALUE)
return false;
ClearCommError(fd, &dwError, &cs);
if(timeout == 0 && cs.cbOutQue > 0) {
PurgeComm(fd, PURGE_TXABORT | PURGE_TXCLEAR);
return true;
}
if(cs.cbOutQue == 0)
return false;
OVERLAPPED ol;
DWORD dwMask;
DWORD dwEvents = 0;
BOOL suc;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
dwMask = EV_TXEMPTY;
SetCommMask(fd, dwMask);
// let's wait for event or timeout
if((suc = WaitCommEvent(fd, &dwEvents, &ol)) == FALSE) {
if(GetLastError() == ERROR_IO_PENDING) {
DWORD transferred;
dwError = WaitForSingleObject(ol.hEvent, timeout);
if (dwError != WAIT_OBJECT_0)
SetCommMask(fd, 0);
suc = GetOverlappedResult(fd, &ol, &transferred, TRUE);
if (suc)
suc = (dwEvents & dwMask) ? TRUE : FALSE;
}
else
ClearCommError(fd, &dwError, &cs);
}
if(ol.hEvent != INVALID_HANDLE_VALUE)
CloseHandle(ol.hEvent);
if(suc == TRUE)
return false;
PurgeComm(fd, PURGE_TXABORT | PURGE_TXCLEAR);
return true;
}
void serial::clear(void)
{
PurgeComm(fd, PURGE_RXABORT | PURGE_RXCLEAR);
}
void serial::sync(void)
{
}
void serial::bin(size_t size, timeout_t timeout)
{
}
void serial::text(char nl1, char nl2)
{
}
void serial::dtr(timeout_t timeout)
{
EscapeCommFunction(fd, CLRDTR);
if(timeout) {
Thread::sleep(timeout);
EscapeCommFunction(fd, SETDTR);
}
}
Serial *Serial::create(const char *name)
{
char buf[65];
String::set(buf, sizeof(buf), name);
char *cp = strchr(buf, ':');
if(cp)
*cp = 0;
serial_t dev = new serial(buf);
name = strchr(name, ':');
if(dev && name)
dev->set(++name);
return dev;
}
stringlist_t *Serial::list(void)
{
DWORD index = 0;
TCHAR keyname[4096];
DWORD size = sizeof(keyname);
stringlist_t *list = new stringlist_t;
HKEY reg;
FILETIME fTime;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &reg) == ERROR_SUCCESS) {
while(RegEnumKeyEx(reg, index++, keyname, &size, NULL, NULL, NULL, &fTime) == ERROR_SUCCESS) {
list->add(keyname);
}
RegCloseKey(reg);
}
return list;
}
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks