-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialdevice.cpp
592 lines (498 loc) · 13.5 KB
/
serialdevice.cpp
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* Copyright (C) 2023 Debayan Sutradhar (rnayabed) ([email protected])
*
* This program 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.
* This program 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.
*/
#include "serialdevice.h"
SerialDevice::SerialDevice(const std::filesystem::path& devicePath,
const DeviceProperties& deviceProperties,
const int32_t& readTimeout)
: m_error{Error::NONE},
m_devicePath{devicePath},
m_deviceProperties{deviceProperties},
m_readTimeout{readTimeout}
{}
const SerialDevice::Error &SerialDevice::error()
{
return m_error;
}
std::string SerialDevice::errorStr()
{
switch (m_error)
{
case NONE:
return "None";
case FAILED_TO_OPEN_DEVICE:
return "Failed to open device";
case FAILED_TO_GET_FD_ATTRS:
return "Failed to get file descriptor attributes";
case FAILED_TO_SET_FD_ATTRS:
return "Failed to set file descriptor attributes";
case INVALID_BAUD_RATE:
return "Invalid baud rate";
case NOT_SUPPORTED:
return "Operation not supported";
case READ_FAILED:
return "Read failed";
case WRITE_FAILED:
return "Write failed";
case DEVICE_NOT_OPEN:
return "Device not open";
}
return "Unknown error " + std::to_string(m_error);
}
#ifdef __linux
bool SerialDevice::open()
{
m_linuxFD = ::open(m_devicePath.c_str(),
O_RDWR | O_NOCTTY);
if (m_linuxFD < 0)
{
m_error = Error::FAILED_TO_OPEN_DEVICE;
return false;
}
struct termios tty;
if (ioctl(m_linuxFD, TCGETS, &tty) < 0)
{
m_error = Error::FAILED_TO_GET_FD_ATTRS;
return false;
}
// Parity
if (m_deviceProperties.parity)
tty.c_cflag |= PARENB;
else
tty.c_cflag &= ~PARENB;
// Stop Bit
if (m_deviceProperties.stopBits == 1)
tty.c_cflag &= ~CSTOPB;
else
tty.c_cflag |= CSTOPB;
//Bits per byte
tty.c_cflag &= ~CSIZE;
if (m_deviceProperties.bits == 5)
tty.c_cflag |= CS5;
else if (m_deviceProperties.bits == 6)
tty.c_cflag |= CS6;
else if (m_deviceProperties.bits == 7)
tty.c_cflag |= CS7;
else if (m_deviceProperties.bits == 8)
tty.c_cflag |= CS8;
// RTS/CTS
if (m_deviceProperties.rtsCts)
tty.c_cflag |= CRTSCTS;
else
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;
// CREAD = allows us to read data
// CLOCAL = disables "carrier detect"
// disables SIGHUP signal to be sent when disconnected
// Local modes flags
// Disable Canonical mode
// Prevents presence of backspace to erase previous byte
// Prevents taking newline as signal to process input
tty.c_lflag &= ~ICANON;
// Disabling canon mode is equivalent to disabling 3
// below. but need to check
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
// Input modes flags
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
// Output modes flags
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// Line discpline not covered
// Control characters (cc) (VMIN, VTIME)
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = m_readTimeout / 100; // milli -> deci
// For some reason cfsetspeed does not work well with integers
speed_t speed;
switch(m_deviceProperties.baudRate)
{
case 50:
speed = B50; break;
case 75:
speed = B75; break;
case 110:
speed = B110; break;
case 134:
speed = B134; break;
case 150:
speed = B150; break;
case 200:
speed = B200; break;
case 300:
speed = B300; break;
case 600:
speed = B600; break;
case 1200:
speed = B1200; break;
case 1800:
speed = B1800; break;
case 2400:
speed = B2400; break;
case 4800:
speed = B4800; break;
case 9600:
speed = B9600; break;
case 19200:
speed = B19200; break;
case 38400:
speed = B38400; break;
case 57600:
speed = B57600; break;
case 115200:
speed = B115200; break;
case 230400:
speed = B230400; break;
case 460800:
speed = B460800; break;
default:
m_error = Error::INVALID_BAUD_RATE;
return false;
}
cfsetspeed(&tty, speed);
if (!ioctl(m_linuxFD, TCSETS, &tty))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}
bool SerialDevice::close()
{
ioctl(m_linuxFD, TCFLSH, 2);
if (!::close(m_linuxFD))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}
bool SerialDevice::read(std::span<unsigned char> bytes)
{
if (m_linuxFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::read(m_linuxFD, bytes.data(), bytes.size()) != -1)
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::READ_FAILED;
return false;
}
}
}
bool SerialDevice::write(std::span<const unsigned char> bytes)
{
if (m_linuxFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::write(m_linuxFD, bytes.data(), bytes.size()) == static_cast<ssize_t>(bytes.size()))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::WRITE_FAILED;
return false;
}
}
m_error = Error::NOT_SUPPORTED;
return false;
}
#elif __WIN32
bool SerialDevice::open()
{
m_winHandle = CreateFile(m_devicePath.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (m_winHandle == INVALID_HANDLE_VALUE)
{
m_error = Error::FAILED_TO_OPEN_DEVICE;
return false;
}
DCB params;
if(GetCommState(m_winHandle, ¶ms) == FALSE)
{
m_error = Error::FAILED_TO_GET_FD_ATTRS;
return false;
}
params.BaudRate = m_deviceProperties.baudRate;
params.ByteSize = m_deviceProperties.bits;
if (m_deviceProperties.stopBits == 1)
{
params.StopBits = ONESTOPBIT;
}
else
{
params.StopBits = TWOSTOPBITS;
}
params.Parity = m_deviceProperties.parity;
if (SetCommState(m_winHandle, ¶ms) == FALSE)
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = m_readTimeout;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (SetCommTimeouts(m_winHandle, &timeouts) == FALSE)
{
// FIXME: This needs to be it's own enum entry
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
m_error = Error::NONE;
return true;
}
bool SerialDevice::close()
{
m_error = Error::NOT_SUPPORTED;
return false;
}
bool SerialDevice::read(std::span<unsigned char> bytes)
{
if (ReadFile(m_winHandle, bytes.data(), bytes.size(), NULL, NULL))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::READ_FAILED;
return false;
}
}
bool SerialDevice::write(std::span<const unsigned char> bytes)
{
unsigned long written;
bool result = WriteFile(m_winHandle, bytes.data(), bytes.size(), &written, NULL);
if (result && bytes.size() == written)
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::WRITE_FAILED;
return false;
}
}
#elif __APPLE__
bool SerialDevice::open()
{
m_macFD = ::open(m_devicePath.c_str(),
(O_RDWR | O_NOCTTY));
if (m_macFD < 0)
{
m_error = Error::FAILED_TO_OPEN_DEVICE;
return false;
}
struct termios tty;
if (ioctl(m_macFD, TIOCGETA, &tty) < 0)
{
m_error = Error::FAILED_TO_GET_FD_ATTRS;
return false;
}
// Parity
if (m_deviceProperties.parity)
tty.c_cflag |= PARENB;
else
tty.c_cflag &= ~PARENB;
// Stop Bit
if (m_deviceProperties.stopBits == 1)
tty.c_cflag &= ~CSTOPB;
else
tty.c_cflag |= CSTOPB;
//Bits per byte
tty.c_cflag &= ~CSIZE;
if (m_deviceProperties.bits == 5)
tty.c_cflag |= CS5;
else if (m_deviceProperties.bits == 6)
tty.c_cflag |= CS6;
else if (m_deviceProperties.bits == 7)
tty.c_cflag |= CS7;
else if (m_deviceProperties.bits == 8)
tty.c_cflag |= CS8;
// RTS/CTS
if (m_deviceProperties.rtsCts)
tty.c_cflag |= CRTSCTS;
else
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;
// CREAD = allows us to read data
// CLOCAL = disables "carrier detect"
// disables SIGHUP signal to be sent when disconnected
// Local modes flags
// Disable Canonical mode
// Prevents presence of backspace to erase previous byte
// Prevents taking newline as signal to process input
tty.c_lflag &= ~ICANON;
// Disabling canon mode is equivalent to disabling 3
// below. but need to check
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
// Input modes flags
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
// Output modes flags
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// Line discpline not covered
// Control characters (cc) (VMIN, VTIME)
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = m_readTimeout / 100; // milli -> deci
// For some reason cfsetspeed does not work well with integers
speed_t speed;
switch(m_deviceProperties.baudRate)
{
case 50:
speed = B50; break;
case 75:
speed = B75; break;
case 110:
speed = B110; break;
case 134:
speed = B134; break;
case 150:
speed = B150; break;
case 200:
speed = B200; break;
case 300:
speed = B300; break;
case 600:
speed = B600; break;
case 1200:
speed = B1200; break;
case 1800:
speed = B1800; break;
case 2400:
speed = B2400; break;
case 4800:
speed = B4800; break;
case 9600:
speed = B9600; break;
case 19200:
speed = B19200; break;
case 38400:
speed = B38400; break;
case 57600:
speed = B57600; break;
case 115200:
speed = B115200; break;
case 230400:
speed = B230400; break;
default:
m_error = Error::INVALID_BAUD_RATE;
return false;
}
cfsetspeed(&tty, speed);
if (!ioctl(m_macFD, TIOCSETA, &tty))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}
bool SerialDevice::close()
{
ioctl(m_macFD, TIOCFLUSH, 2);
if (!::close(m_macFD))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::FAILED_TO_SET_FD_ATTRS;
return false;
}
}
bool SerialDevice::read(std::span<unsigned char> bytes)
{
if (m_macFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::read(m_macFD, bytes.data(), bytes.size()) != -1)
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::READ_FAILED;
return false;
}
}
}
bool SerialDevice::write(std::span<const unsigned char> bytes)
{
if (m_macFD == -1)
{
m_error = Error::DEVICE_NOT_OPEN;
return false;
}
else
{
if (::write(m_macFD, bytes.data(), bytes.size()) == static_cast<ssize_t>(bytes.size()))
{
m_error = Error::NONE;
return true;
}
else
{
m_error = Error::WRITE_FAILED;
return false;
}
}
m_error = Error::NOT_SUPPORTED;
return false;
}
#endif