-
Notifications
You must be signed in to change notification settings - Fork 2
/
OSComputerRS232Port.h
1878 lines (1698 loc) · 52 KB
/
OSComputerRS232Port.h
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************************************************:')
OSComputerRS232Port.h
Computer RS232 port handling (open, close, read, write,...).
Fabrice Le Bars
Created : 2009-03-28
***************************************************************************************************************:)*/
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef OSCOMPUTERRS232PORT_H
#define OSCOMPUTERRS232PORT_H
#include "OSTime.h"
/*
Debug macros specific to OSComputerRS232Port.
*/
#ifdef _DEBUG_MESSAGES_OSUTILS
# define _DEBUG_MESSAGES_OSCOMPUTERRS232PORT
#endif // _DEBUG_MESSAGES_OSUTILS
#ifdef _DEBUG_WARNINGS_OSUTILS
# define _DEBUG_WARNINGS_OSCOMPUTERRS232PORT
#endif // _DEBUG_WARNINGS_OSUTILS
#ifdef _DEBUG_ERRORS_OSUTILS
# define _DEBUG_ERRORS_OSCOMPUTERRS232PORT
#endif // _DEBUG_ERRORS_OSUTILS
#ifdef _DEBUG_MESSAGES_OSCOMPUTERRS232PORT
# define PRINT_DEBUG_MESSAGE_OSCOMPUTERRS232PORT(params) PRINT_DEBUG_MESSAGE(params)
#else
# define PRINT_DEBUG_MESSAGE_OSCOMPUTERRS232PORT(params)
#endif // _DEBUG_MESSAGES_OSCOMPUTERRS232PORT
#ifdef _DEBUG_WARNINGS_OSCOMPUTERRS232PORT
# define PRINT_DEBUG_WARNING_OSCOMPUTERRS232PORT(params) PRINT_DEBUG_WARNING(params)
#else
# define PRINT_DEBUG_WARNING_OSCOMPUTERRS232PORT(params)
#endif // _DEBUG_WARNINGS_OSCOMPUTERRS232PORT
#ifdef _DEBUG_ERRORS_OSCOMPUTERRS232PORT
# define PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(params) PRINT_DEBUG_ERROR(params)
#else
# define PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(params)
#endif // _DEBUG_ERRORS_OSCOMPUTERRS232PORT
#ifdef _WIN32
#else
#ifndef DISABLE_SELECT_OSCOMPUTERRS232PORT
#include <sys/select.h>
#endif // !DISABLE_SELECT_OSCOMPUTERRS232PORT
#if !defined(DISABLE_FORCE_CLEAR_DTR) || defined(ENABLE_DTR_FUNCTIONS)
#include <sys/ioctl.h>
#endif // !defined(DISABLE_FORCE_CLEAR_DTR) || defined(ENABLE_DTR_FUNCTIONS)
#ifndef DISABLE_CUSTOM_BAUDRATE
#include <sys/ioctl.h>
#ifdef __APPLE__
#include <IOKit/serial/ioss.h>
#endif // __APPLE__
#ifdef __linux__
#include <linux/serial.h>
#pragma push_macro("termios")
#undef termios
#define termios termbits_termios
#include <asm/termbits.h> // Not compatible with termios.h, see https://stackoverflow.com/questions/37710525/including-termios-h-and-asm-termios-h-in-the-same-project...
#undef termios
#pragma pop_macro("termios")
#endif // __linux__
#endif // !DISABLE_CUSTOM_BAUDRATE
#include <termios.h>
#endif // _WIN32
#ifndef _WIN32
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
#define ONESTOPBIT 0
#define TWOSTOPBITS 2
#endif // !_WIN32
#define MAX_COMPUTERRS232PORT_TIMEOUT 25500
#define MAX_COMPUTERRS232PORT_NAME_LENGTH (128-8)
inline UINT _BaudRate2Constant(UINT BaudRate)
{
#ifdef _WIN32
switch (BaudRate)
{
case 50:
return 50;
case 75:
return 75;
case 110:
return CBR_110;
case 134:
return 134;
case 150:
return 150;
case 200:
return 200;
case 300:
return CBR_300;
case 600:
return CBR_600;
case 1200:
return CBR_1200;
case 1800:
return 1800;
case 2400:
return CBR_2400;
case 4800:
return CBR_4800;
case 9600:
return CBR_9600;
case 14400:
return CBR_14400;
case 19200:
return CBR_19200;
case 38400:
return CBR_38400;
case 56000:
return CBR_56000;
case 57600:
return CBR_57600;
case 111100:
return 111100;
case 115200:
return CBR_115200;
case 128000:
return CBR_128000;
case 230400:
return 230400;
case 256000:
return CBR_256000;
case 460800:
return 460800;
case 500000:
return 500000;
case 576000:
return 576000;
case 921600:
return 921600;
case 1000000:
return 1000000;
case 1152000:
return 1152000;
case 1500000:
return 1500000;
case 2000000:
return 2000000;
case 2500000:
return 2500000;
case 3000000:
return 3000000;
case 3500000:
return 3500000;
case 4000000:
return 4000000;
default:
return 0;
}
#else
switch (BaudRate)
{
case 50:
return B50;
case 75:
return B75;
case 110:
return B110;
case 134:
return B134;
case 150:
return B150;
case 200:
return B200;
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 1800:
return B1800;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
#ifndef __APPLE__
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
#endif // !__APPLE__
default:
return 0;
}
#endif // _WIN32
}
inline UINT _Constant2BaudRate(UINT Constant)
{
#ifdef _WIN32
switch (Constant)
{
case 50:
return 50;
case 75:
return 75;
case CBR_110:
return 110;
case 134:
return 134;
case 150:
return 150;
case 200:
return 200;
case CBR_300:
return 300;
case CBR_600:
return 600;
case CBR_1200:
return 1200;
case 1800:
return 1800;
case CBR_2400:
return 2400;
case CBR_4800:
return 4800;
case CBR_9600:
return 9600;
case CBR_14400:
return 14400;
case CBR_19200:
return 19200;
case CBR_38400:
return 38400;
case CBR_56000:
return 56000;
case CBR_57600:
return 57600;
case 111100:
return 111100;
case CBR_115200:
return 115200;
case CBR_128000:
return 128000;
case 230400:
return 230400;
case CBR_256000:
return 256000;
case 460800:
return 460800;
case 500000:
return 500000;
case 576000:
return 576000;
case 921600:
return 921600;
case 1000000:
return 1000000;
case 1152000:
return 1152000;
case 1500000:
return 1500000;
case 2000000:
return 2000000;
case 2500000:
return 2500000;
case 3000000:
return 3000000;
case 3500000:
return 3500000;
case 4000000:
return 4000000;
default:
return 0;
}
#else
switch (Constant)
{
case B50:
return 50;
case B75:
return 75;
case B110:
return 110;
case B134:
return 134;
case B150:
return 150;
case B200:
return 200;
case B300:
return 300;
case B600:
return 600;
case B1200:
return 1200;
case B1800:
return 1800;
case B2400:
return 2400;
case B4800:
return 4800;
case B9600:
return 9600;
case B19200:
return 19200;
case B38400:
return 38400;
case B57600:
return 57600;
case B115200:
return 115200;
case B230400:
return 230400;
#ifndef __APPLE__
case B460800:
return 460800;
case B500000:
return 500000;
case B576000:
return 576000;
case B921600:
return 921600;
case B1000000:
return 1000000;
case B1152000:
return 1152000;
case B1500000:
return 1500000;
case B2000000:
return 2000000;
case B2500000:
return 2500000;
case B3000000:
return 3000000;
case B3500000:
return 3500000;
case B4000000:
return 4000000;
#endif // !__APPLE__
default:
return 0;
}
#endif // _WIN32
}
/*
Open a computer RS232 port. Use CloseComputerRS232Port() to close it at the end.
HANDLE* phDev : (INOUT) Valid pointer that will receive an identifier of the
computer RS232 port opened.
char* szDevice : (IN) Computer RS232 port to open.
Return : EXIT_SUCCESS, EXIT_NAME_TOO_LONG or EXIT_FAILURE.
*/
inline int OpenComputerRS232Port(HANDLE* phDev, char* szDevice)
{
#ifdef _WIN32
HANDLE hDev = INVALID_HANDLE_VALUE;
char szDeviceTemp[2*(MAX_COMPUTERRS232PORT_NAME_LENGTH+8)];
TCHAR tstr[2*(MAX_COMPUTERRS232PORT_NAME_LENGTH+8)];
if (strlen(szDevice) > MAX_COMPUTERRS232PORT_NAME_LENGTH)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("OpenComputerRS232Port error (%s) : %s"
"(szDevice=%s)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_NAME_TOO_LONG],
szDevice));
return EXIT_NAME_TOO_LONG;
}
// To be able to use COM10 and greater we need to add "\\.\" (that becomes "\\\\.\\"
// in C because the '\' is a special character).
memset(szDeviceTemp, 0, sizeof(szDeviceTemp));
#ifdef WINCE
strcpy(szDeviceTemp, szDevice);
#else
sprintf(szDeviceTemp, "\\\\.\\%s", szDevice);
#endif // WINCE
#ifdef UNICODE
mbstowcs(tstr, szDeviceTemp, sizeof(szDeviceTemp)/2);
#else
memcpy(tstr, szDeviceTemp, sizeof(szDeviceTemp)/2);
#endif // UNICODE
tstr[sizeof(tstr)/sizeof(TCHAR)-1] = 0;
hDev = CreateFile(
tstr,
GENERIC_READ|GENERIC_WRITE,
0, // Must be opened with exclusive-access.
NULL, // No security attributes.
OPEN_EXISTING, // Must use OPEN_EXISTING.
0, // Not overlapped I/O. Should use FILE_FLAG_WRITE_THROUGH and maybe also FILE_FLAG_NO_BUFFERING?
NULL // hTemplate must be NULL for comm devices.
);
if (hDev == INVALID_HANDLE_VALUE)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("OpenComputerRS232Port error (%s) : %s"
"(szDevice=%s)\n",
strtime_m(),
GetLastErrorMsg(),
szDevice));
return EXIT_FAILURE;
}
#ifndef DISABLE_FORCE_CLEAR_DTR
if (!EscapeCommFunction(hDev, CLRDTR))
{
PRINT_DEBUG_WARNING_OSCOMPUTERRS232PORT(("OpenComputerRS232Port warning (%s) : %s"
"(szDevice=%s)\n",
strtime_m(),
GetLastErrorMsg(),
szDevice));
}
#endif // !DISABLE_FORCE_CLEAR_DTR
*phDev = hDev;
#else
// The O_NOCTTY flag tells UNIX that this program does not want to be the
// "controlling terminal" for that port. If you don't specify this then any input (such
// as keyboard abort signals and so forth) will affect your process. Programs like
// getty(1M/8) use this feature when starting the login process, but normally a
// user program does not want this behavior.
// The O_NDELAY flag tells UNIX that this program does not care what state the
// DCD signal line is in - whether the other end of the port is up and running. If
// you do not specify this flag, your process will be put to sleep until the DCD
// signal line is the space voltage.
// Should use O_SYNC and maybe also O_DIRECT?
int fd = open(szDevice, O_RDWR|O_NOCTTY|O_NDELAY);
#ifndef DISABLE_FORCE_CLEAR_DTR
int dtr_bit = TIOCM_DTR;
#endif // !DISABLE_FORCE_CLEAR_DTR
if (fd == -1)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("OpenComputerRS232Port error (%s) : %s"
"(szDevice=%s)\n",
strtime_m(),
GetLastErrorMsg(),
szDevice));
return EXIT_FAILURE;
}
// The read function can be made to return immediately by
// doing the following :
// fcntl(fd, F_SETFL, FNDELAY);
// The FNDELAY option causes the read function to return 0 if no characters are
// available on the port. To restore normal (blocking) behavior, call fcntl() without
// the FNDELAY option:
if (fcntl(fd, F_SETFL, 0) == (-1))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("OpenComputerRS232Port error (%s) : (F_SETFL) %s"
"(szDevice=%s)\n",
strtime_m(),
GetLastErrorMsg(),
szDevice));
close(fd);
return EXIT_FAILURE;
}
#ifndef DISABLE_FORCE_CLEAR_DTR
if (ioctl(fd, TIOCMBIC, &dtr_bit) != EXIT_SUCCESS)
{
PRINT_DEBUG_WARNING_OSCOMPUTERRS232PORT(("OpenComputerRS232Port warning (%s) : (TIOCMBIC) %s"
"(szDevice=%s)\n",
strtime_m(),
GetLastErrorMsg(),
szDevice));
}
#endif // !DISABLE_FORCE_CLEAR_DTR
#ifndef DISABLE_IGNORE_SIGPIPE
// See https://stackoverflow.com/questions/17332646/server-dies-on-send-if-client-was-closed-with-ctrlc...
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
PRINT_DEBUG_WARNING_OSCOMPUTERRS232PORT(("OpenComputerRS232Port warning (%s) : (SIGPIPE, SIG_IGN) %s"
"(szDevice=%s)\n",
strtime_m(),
"signal failed ",
szDevice));
}
#endif // DISABLE_IGNORE_SIGPIPE
*phDev = (HANDLE)(intptr_t)fd;
#endif // _WIN32
return EXIT_SUCCESS;
}
/*
Close a computer RS232 port opened by OpenComputerRS232Port().
HANDLE hDev : (IN) Identifier of the computer RS232 port.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int CloseComputerRS232Port(HANDLE hDev)
{
#ifdef _WIN32
if (!CloseHandle(hDev))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("CloseComputerRS232Port error (%s) : %s(hDev=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
hDev));
return EXIT_FAILURE;
}
#else
if (close((intptr_t)hDev) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("CloseComputerRS232Port error (%s) : %s(hDev=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
hDev));
return EXIT_FAILURE;
}
#endif // _WIN32
return EXIT_SUCCESS;
}
/*
Set the options of a computer RS232 port.
HANDLE hDev : (IN) Identifier of the computer RS232 port.
UINT BaudRate : (IN) Baud rate at which the device connected to the computer RS232 port operates.
BYTE ParityMode : (IN) Parity mode. Should be either NOPARITY, ODDPARITY, EVENPARITY, MARKPARITY or SPACEPARITY.
BOOL bCheckParity : (IN) If TRUE, enable input parity checking.
BYTE nbDataBits : (IN) Number of bits of the data bytes.
BYTE StopBitsMode : (IN) Stop bits mode. Should be either ONESTOPBIT or TWOSTOPBITS.
UINT timeout : (IN) Time to wait to get at least 1 byte in ms (near 1000 ms for example, max is
MAX_COMPUTERRS232PORT_TIMEOUT).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetOptionsComputerRS232Port(HANDLE hDev, UINT BaudRate, BYTE ParityMode, BOOL bCheckParity, BYTE nbDataBits,
BYTE StopBitsMode, UINT timeout)
{
// Should we do something like PurgeComm(g_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR) / tcflush
// in the beginning of this function?
#ifdef _WIN32
DCB dcb;
COMMTIMEOUTS timeouts;
memset(&dcb, 0, sizeof(DCB));
if (!GetCommState(hDev, &dcb))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
memset(&timeouts, 0, sizeof(COMMTIMEOUTS));
if (!GetCommTimeouts(hDev, &timeouts))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// Binary mode.
dcb.fBinary = TRUE;
// No flow control.
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
dcb.BaudRate = _BaudRate2Constant(BaudRate);
if (dcb.BaudRate == 0)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Invalid BaudRate. ",
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// An even parity bit will be set to "1" if the number of 1's + 1 is even, and an odd
// parity bit will be set to "1" if the number of 1's +1 is odd.
// If the parity bit is present but not used, it may be referred to as mark parity
// (when the parity bit is always 1) or space parity (the bit is always 0).
// None parity means that no parity bit is sent at all.
dcb.Parity = ParityMode;
dcb.fParity = bCheckParity;
dcb.fErrorChar = FALSE; // Indicates whether bytes received with parity errors are
// replaced with the character specified by the ErrorChar member.
dcb.ByteSize = nbDataBits;
dcb.StopBits = StopBitsMode;
// The SetCommState() function reconfigures the communications resource, but it does not affect
// the internal output and input buffers of the specified driver. The buffers are not flushed,
// and pending read and write operations are not terminated prematurely.
if (!SetCommState(hDev, &dcb))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// Special mode for read timeouts.
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = timeout;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
//timeouts.WriteTotalTimeoutConstant = 0; // Linux does not seem to have options for write timeouts, so disable for Windows...?
timeouts.WriteTotalTimeoutConstant = timeout;
timeouts.WriteTotalTimeoutMultiplier = 0;
if (!SetCommTimeouts(hDev, &timeouts))
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
#else
struct termios options;
speed_t speed = 0;
BOOL bCustomBaudrate = FALSE;
memset(&options, 0, sizeof(options));
if (tcgetattr((intptr_t)hDev, &options) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// Basic raw/non-canonical setup.
// The terminal attributes are set as follows :
// termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
// termios_p->c_oflag &= ~OPOST;
// termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
// termios_p->c_cflag &= ~(CSIZE | PARENB);
// termios_p->c_cflag |= CS8;
cfmakeraw(&options);
// The c_cflag member contains two options that should always be enabled,
// CLOCAL and CREAD. These will ensure that your program does not become
// the 'owner' of the port subject to sporatic job control and hangup signals, and
// also that the serial interface driver will read incoming data bytes.
options.c_cflag |= (CLOCAL | CREAD);
// Raw input, no echo, no signals.
options.c_lflag &= ~(ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL | ISIG);
// Raw input.
options.c_iflag &= ~(IGNBRK | BRKINT | INLCR | IGNCR | ICRNL);
// Raw output.
options.c_oflag &= ~(OPOST | ONLCR | ONOCR | ONLRET | OCRNL);
// Disable hardware flow control.
#ifdef CRTSCTS
options.c_cflag &= ~CRTSCTS;
#endif // CRTSCTS
#ifdef CNEW_RTSCTS
options.c_cflag &= ~CNEW_RTSCTS;
#endif // CNEW_RTSCTS
// Disable software flow control.
options.c_iflag &= ~(IXON | IXOFF | IXANY);
speed = _BaudRate2Constant(BaudRate);
if (speed == 0)
{
// Baud rate is probably non-standard...
bCustomBaudrate = TRUE;
// See https://stackoverflow.com/questions/19440268/how-to-set-a-non-standard-baudrate-on-a-serial-port-device-on-linux.
speed = _BaudRate2Constant(38400);
// Linux-only, not compatible with all devices...?
//options.c_cflag &= ~(CBAUD | CBAUDEX);
//options.c_cflag |= speed;
//tty.alt_speed = BaudRate; // ???
}
if (cfsetospeed(&options, speed) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
if (cfsetispeed(&options, speed) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// An even parity bit will be set to "1" if the number of 1's + 1 is even, and an odd
// parity bit will be set to "1" if the number of 1's +1 is odd.
// If the parity bit is present but not used, it may be referred to as mark parity
// (when the parity bit is always 1) or space parity (the bit is always 0).
// None parity means that no parity bit is sent at all.
switch (ParityMode)
{
case NOPARITY:
#ifndef __APPLE__
options.c_cflag &= ~CMSPAR;
#endif // !__APPLE__
options.c_cflag &= ~PARENB;
options.c_cflag &= ~PARODD;
break;
case MARKPARITY:
#ifndef __APPLE__
options.c_cflag |= CMSPAR;
#endif // !__APPLE__
options.c_cflag &= ~PARENB;
options.c_cflag |= PARODD;
break;
case SPACEPARITY:
#ifndef __APPLE__
options.c_cflag |= CMSPAR;
#endif // !__APPLE__
options.c_cflag &= ~PARENB;
options.c_cflag &= ~PARODD;
break;
case ODDPARITY:
#ifndef __APPLE__
options.c_cflag &= ~CMSPAR;
#endif // !__APPLE__
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
break;
case EVENPARITY:
#ifndef __APPLE__
options.c_cflag &= ~CMSPAR;
#endif // !__APPLE__
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
break;
default:
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Invalid parity mode. ",
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// The most likely scenario where this flag is useful is if the communication channel is
// configured for parity and seven bits per character. In this case, the eigth bit on every
// received character is a parity bit, not part of the data payload. The user program does
// not need to know the value of the parity bit.
options.c_iflag &= ~ISTRIP;
options.c_iflag &= ~PARMRK; // Never mark a framing or parity error with prefix bytes.
options.c_iflag |= IGNPAR; // A character with a framing or parity error will be discarded.
// This is only valid (at least for parity errors) if parity checking is enabled.
if (bCheckParity)
{
options.c_iflag |= INPCK;
}
else
{
options.c_iflag &= ~INPCK;
}
options.c_cflag &= ~CSIZE; // Erase the previous flag for the number of data bits.
switch (nbDataBits)
{
case 8:
options.c_cflag |= CS8;
break;
case 7:
options.c_cflag |= CS7;
break;
case 6:
options.c_cflag |= CS6;
break;
case 5:
options.c_cflag |= CS5;
break;
default :
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Invalid number of data bits. ",
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
switch (StopBitsMode)
{
case ONESTOPBIT:
options.c_cflag &= ~CSTOPB;
break;
case TWOSTOPBITS:
options.c_cflag |= CSTOPB;
break;
default :
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Invalid stop bits mode. ",
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
if (timeout/100 > 255)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Too high timeout value. ",
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
// Timeouts.
options.c_cc[VMIN] = 0; // Minimum number of characters to read.
options.c_cc[VTIME] = timeout/100; // Time to wait for every character read in tenths of seconds.
if (tcsetattr((intptr_t)hDev, TCSADRAIN, &options) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
if (bCustomBaudrate)
{
#ifndef DISABLE_CUSTOM_BAUDRATE
// See https://github.com/wjwwood/serial/blob/master/src/impl/unix.cc,
// https://stackoverflow.com/questions/4968529/how-to-set-baud-rate-to-307200-on-linux/7152671,
// https://stackoverflow.com/questions/23492088/c-code-for-non-standard-baud-rate-on-debian-raspberry-pi.
#if defined(__APPLE__) && defined(IOSSIOSPEED)
// See https://developer.apple.com/library/archive/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html.
// Starting with Tiger, the IOSSIOSPEED ioctl can be used to set arbitrary baud rates
// other than those specified by POSIX. The driver for the underlying serial hardware
// ultimately determines which baud rates can be used. This ioctl sets both the input
// and output speed.
speed_t new_baud = (speed_t)BaudRate;
if (ioctl((intptr_t)hDev, IOSSIOSPEED, &new_baud) == -1)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : (IOSSIOSPEED) %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
#elif defined(__linux__) && defined(TCSETS2)
// See https://stackoverflow.com/questions/12646324/how-can-i-set-a-custom-baud-rate-on-linux.
struct termios2 options2;
memset(&options2, 0, sizeof(struct termios2));
if (ioctl((intptr_t)hDev, TCGETS2, &options2) == -1)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : (TCGETS2) %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
options2.c_cflag &= ~CBAUD; // Remove current BAUD rate.
options2.c_cflag |= BOTHER; // Allow custom BAUD rate using int input.
options2.c_ispeed = BaudRate; // Set the input BAUD rate.
options2.c_ospeed = BaudRate; // Set the output BAUD rate.
if (ioctl((intptr_t)hDev, TCSETS2, &options2) == -1)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : (TCSETS2) %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
#elif defined(__linux__) && defined(TIOCSSERIAL)
struct serial_struct ser;
memset(&ser, 0, sizeof(struct serial_struct));
if (ioctl((intptr_t)hDev, TIOCGSERIAL, &ser) == -1)
{
PRINT_DEBUG_ERROR_OSCOMPUTERRS232PORT(("SetOptionsComputerRS232Port error (%s) : (TIOCGSERIAL) %s"
"(hDev=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
GetLastErrorMsg(),
hDev, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;