-
Notifications
You must be signed in to change notification settings - Fork 2
/
RS232Port.h
1213 lines (1096 loc) · 36.5 KB
/
RS232Port.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
/***************************************************************************************************************:')
RS232Port.h
RS232 port handling via local COM port or IP converter.
Fabrice Le Bars
Created : 2013-08-16
***************************************************************************************************************:)*/
// 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 RS232PORT_H
#define RS232PORT_H
#include "OSComputerRS232Port.h"
#include "OSNet.h"
/*
Debug macros specific to RS232Port.
*/
#ifdef _DEBUG_MESSAGES
# define _DEBUG_MESSAGES_RS232PORT
#endif // _DEBUG_MESSAGES
#ifdef _DEBUG_WARNINGS
# define _DEBUG_WARNINGS_RS232PORT
#endif // _DEBUG_WARNINGS
#ifdef _DEBUG_ERRORS
# define _DEBUG_ERRORS_RS232PORT
#endif // _DEBUG_ERRORS
#ifdef _DEBUG_MESSAGES_RS232PORT
# define PRINT_DEBUG_MESSAGE_RS232PORT(params) PRINT_DEBUG_MESSAGE(params)
#else
# define PRINT_DEBUG_MESSAGE_RS232PORT(params)
#endif // _DEBUG_MESSAGES_RS232PORT
#ifdef _DEBUG_WARNINGS_RS232PORT
# define PRINT_DEBUG_WARNING_RS232PORT(params) PRINT_DEBUG_WARNING(params)
#else
# define PRINT_DEBUG_WARNING_RS232PORT(params)
#endif // _DEBUG_WARNINGS_RS232PORT
#ifdef _DEBUG_ERRORS_RS232PORT
# define PRINT_DEBUG_ERROR_RS232PORT(params) PRINT_DEBUG_ERROR(params)
#else
# define PRINT_DEBUG_ERROR_RS232PORT(params)
#endif // _DEBUG_ERRORS_RS232PORT
#define MAX_TIMEOUT_RS232PORT 25500
#define MAX_NAME_LENGTH_RS232PORT (128-8)
#define LOCAL_TYPE_RS232PORT OTHER_DEV_TYPE_OSNET
#define TCP_CLIENT_TYPE_RS232PORT TCP_CLIENT_DEV_TYPE_OSNET
#define TCP_SERVER_TYPE_RS232PORT TCP_SERVER_DEV_TYPE_OSNET
#define UDP_CLIENT_TYPE_RS232PORT UDP_CLIENT_DEV_TYPE_OSNET
#define UDP_SERVER_TYPE_RS232PORT UDP_SERVER_DEV_TYPE_OSNET
//#define UE9_TYPE_RS232PORT 5
struct RS232PORT
{
HANDLE hDev;
SOCKET s;
SOCKET s_srv;
//struct sockaddr_storage addr; // For UDP...
//socklen_t addrlen; // For UDP...
char szDevPath[256];
char address[256];
char port[256];
int DevType;
};
typedef struct RS232PORT RS232PORT;
/*
Open a RS232 port. Use CloseRS232Port() to close it at the end.
RS232PORT* pRS232Port : (INOUT) Valid pointer that will receive a structure
corresponding to a RS232 port.
char* szDevPath : (IN) Server TCP port (e.g. :4001), client IP address and TCP port (e.g. 127.0.0.1:4001),
server UDP port (udp:4001), client IP address and UDP port (e.g. udp://127.0.0.1:4001) or local RS232 port.
Return : EXIT_SUCCESS, EXIT_INVALID_PARAMETER or EXIT_FAILURE if there is an error.
*/
inline int OpenRS232Port(RS232PORT* pRS232Port, char* szDevPath)
{
int iResult = EXIT_FAILURE;
memset(pRS232Port->szDevPath, 0, sizeof(pRS232Port->szDevPath));
memset(pRS232Port->address, 0, sizeof(pRS232Port->address));
memset(pRS232Port->port, 0, sizeof(pRS232Port->port));
pRS232Port->DevType = -1;
// Try to determine whether it is a server TCP port (e.g. :4001), client IP address and TCP port (e.g. 127.0.0.1:4001),
// server UDP port (udp:4001), client IP address and UDP port (e.g. udp://127.0.0.1:4001) or local RS232 port.
if (GetAddrPortTypeFromDevPath(szDevPath, pRS232Port->address, sizeof(pRS232Port->address), pRS232Port->port, sizeof(pRS232Port->port), &pRS232Port->DevType) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_INVALID_PARAMETER],
szDevPath));
return EXIT_INVALID_PARAMETER;
}
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
if (inittcpcli(&pRS232Port->s, pRS232Port->address, pRS232Port->port) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"inittcpcli failed. ",
szDevPath));
return EXIT_FAILURE;
}
break;
case TCP_SERVER_TYPE_RS232PORT:
if (strlen(pRS232Port->address) == 0)
{
if (inittcpsrv(&pRS232Port->s_srv, "0.0.0.0", pRS232Port->port, 1, DEFAULT_SOCK_TIMEOUT) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"inittcpsrv failed. ",
szDevPath));
return EXIT_FAILURE;
}
}
else
{
if (inittcpsrv(&pRS232Port->s_srv, pRS232Port->address, pRS232Port->port, 1, DEFAULT_SOCK_TIMEOUT) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"inittcpsrv failed. ",
szDevPath));
return EXIT_FAILURE;
}
}
iResult = waitforclifortcpsrv(pRS232Port->s_srv, &pRS232Port->s, DEFAULT_SOCK_TIMEOUT);
switch (iResult)
{
case EXIT_SUCCESS:
break;
case EXIT_TIMEOUT:
default:
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"waitforclifortcpsrv failed or timed out. ",
szDevPath));
releasetcpsrv(pRS232Port->s_srv);
return EXIT_FAILURE;
}
break;
case UDP_CLIENT_TYPE_RS232PORT:
if (initudpcli(&pRS232Port->s, pRS232Port->address, pRS232Port->port) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"initudpcli failed. ",
szDevPath));
return EXIT_FAILURE;
}
break;
case UDP_SERVER_TYPE_RS232PORT:
if (strlen(pRS232Port->address) == 0)
{
if (initudpsrv(&pRS232Port->s_srv, "0.0.0.0", pRS232Port->port, DEFAULT_SOCK_TIMEOUT) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"initudpsrv failed. ",
szDevPath));
return EXIT_FAILURE;
}
}
else
{
if (initudpsrv(&pRS232Port->s_srv, pRS232Port->address, pRS232Port->port, DEFAULT_SOCK_TIMEOUT) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"initudpsrv failed. ",
szDevPath));
return EXIT_FAILURE;
}
}
iResult = waitforcliforudpsrv(pRS232Port->s_srv, &pRS232Port->s, DEFAULT_SOCK_TIMEOUT);
switch (iResult)
{
case EXIT_SUCCESS:
break;
case EXIT_TIMEOUT:
default:
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"waitforcliforudpsrv failed or timed out. ",
szDevPath));
releasetcpsrv(pRS232Port->s_srv);
return EXIT_FAILURE;
}
break;
case LOCAL_TYPE_RS232PORT:
if (OpenComputerRS232Port(&pRS232Port->hDev, szDevPath) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"OpenComputerRS232Port failed. ",
szDevPath));
return EXIT_FAILURE;
}
if (PurgeComputerRS232Port(pRS232Port->hDev) != EXIT_SUCCESS)
{
PRINT_DEBUG_WARNING_RS232PORT(("OpenRS232Port warning (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"PurgeComputerRS232Port failed. ",
szDevPath));
}
break;
default:
PRINT_DEBUG_ERROR_RS232PORT(("OpenRS232Port error (%s) : %s"
"(szDevPath=%s)\n",
strtime_m(),
"Invalid device type. ",
szDevPath));
return EXIT_FAILURE;
}
sprintf(pRS232Port->szDevPath, "%.255s", szDevPath);
return EXIT_SUCCESS;
}
/*
Close a RS232 port opened by OpenRS232Port().
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int CloseRS232Port(RS232PORT* pRS232Port)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
return releasetcpcli(pRS232Port->s);
case TCP_SERVER_TYPE_RS232PORT:
if (disconnectclifromtcpsrv(pRS232Port->s) != EXIT_SUCCESS)
{
PRINT_DEBUG_WARNING_RS232PORT(("CloseRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Error disconnecting a client. ",
pRS232Port));
}
return releasetcpsrv(pRS232Port->s_srv);
case UDP_CLIENT_TYPE_RS232PORT:
return releaseudpcli(pRS232Port->s);
case UDP_SERVER_TYPE_RS232PORT:
if (disconnectclifromudpsrv(pRS232Port->s) != EXIT_SUCCESS)
{
PRINT_DEBUG_WARNING_RS232PORT(("CloseRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Error disconnecting a client. ",
pRS232Port));
}
return releaseudpsrv(pRS232Port->s_srv);
case LOCAL_TYPE_RS232PORT:
return CloseComputerRS232Port(pRS232Port->hDev);
default:
PRINT_DEBUG_ERROR_RS232PORT(("CloseRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port));
return EXIT_FAILURE;
}
}
/*
Set the options of a RS232 port.
Warning : this function has no effect for a remote RS232 port as these options usually depends on the
configuration of the hardware or software RS232 to TCP/IP converter used.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
UINT BaudRate : (IN) Baud rate at which the device connected to the 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_TIMEOUT_RS232PORT).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SetOptionsRS232Port(RS232PORT* pRS232Port, UINT BaudRate, BYTE ParityMode, BOOL bCheckParity, BYTE nbDataBits,
BYTE StopBitsMode, UINT timeout)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
PRINT_DEBUG_WARNING_RS232PORT(("SetOptionsRS232Port warning (%s) : %s"
"(pRS232Port=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Please check the configuration of the hardware or software RS232 to IP converter. ",
pRS232Port, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
break;
case LOCAL_TYPE_RS232PORT:
if (SetOptionsComputerRS232Port(pRS232Port->hDev, BaudRate, ParityMode, bCheckParity, nbDataBits, StopBitsMode, timeout) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_RS232PORT(("SetOptionsRS232Port error (%s) : %s"
"(pRS232Port=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"SetOptionsComputerRS232Port failed. ",
pRS232Port, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
if (PurgeComputerRS232Port(pRS232Port->hDev) != EXIT_SUCCESS)
{
PRINT_DEBUG_WARNING_RS232PORT(("SetOptionsRS232Port warning (%s) : %s"
"(pRS232Port=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"PurgeComputerRS232Port failed. ",
pRS232Port, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
}
break;
default:
PRINT_DEBUG_ERROR_RS232PORT(("SetOptionsRS232Port error (%s) : %s"
"(pRS232Port=%#x, BaudRate=%u, ParityMode=%u, bCheckParity=%u, "
"nbDataBits=%u, StopBitsMode=%u, timeout=%u)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, BaudRate, (UINT)ParityMode, (UINT)bCheckParity, (UINT)nbDataBits, (UINT)StopBitsMode, timeout));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Discard non-transmitted output data and non-read input data on a RS232 port.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int PurgeRS232Port(RS232PORT* pRS232Port)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
return flushsocket(pRS232Port->s);
case LOCAL_TYPE_RS232PORT:
return PurgeComputerRS232Port(pRS232Port->hDev);
default:
PRINT_DEBUG_ERROR_RS232PORT(("PurgeRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port));
return EXIT_FAILURE;
}
}
#ifdef ENABLE_DRAINRS232PORT
/*
Empty the OS internal output buffer and wait until all output written to the serial
port has been transmitted (synchronous operation subject to flow control). Will not
return until all pending write operations have been transmitted.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int DrainRS232Port(RS232PORT* pRS232Port)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
return flushsocket(pRS232Port->s);
case LOCAL_TYPE_RS232PORT:
return DrainComputerRS232Port(pRS232Port->hDev);
default:
PRINT_DEBUG_ERROR_RS232PORT(("DrainRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port));
return EXIT_FAILURE;
}
}
#endif // ENABLE_DRAINRS232PORT
/*
Check for any data available to read on a RS232 port.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
int* pNbBytesAvail : (INOUT) Valid pointer that will receive the number of bytes currently
available. Ignored if NULL.
Return : EXIT_SUCCESS if there is data to read, EXIT_TIMEOUT if there is currently no data
available or EXIT_FAILURE if there is an error.
*/
inline int CheckAvailBytesRS232Port(RS232PORT* pRS232Port, int* pNbBytesAvail)
{
#ifdef DISABLE_IOCTLSOCKET
int ret = EXIT_FAILURE;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
#endif // DISABLE_IOCTLSOCKET
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
#ifdef DISABLE_IOCTLSOCKET
ret = waitforsocket(pRS232Port->s, tv);
if ((ret == EXIT_SUCCESS)&&(pNbBytesAvail)) *pNbBytesAvail = 1;
return ret;
#else
return checkavailbytessocket(pRS232Port->s, pNbBytesAvail);
#endif // DISABLE_IOCTLSOCKET
case LOCAL_TYPE_RS232PORT:
return CheckAvailBytesComputerRS232Port(pRS232Port->hDev, pNbBytesAvail);
default:
PRINT_DEBUG_ERROR_RS232PORT(("CheckAvailableBytesRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port));
return EXIT_FAILURE;
}
}
// Deprecated, kept for compatibility...
inline int CheckAvailableBytesRS232Port(RS232PORT* pRS232Port)
{
#ifdef DISABLE_IOCTLSOCKET
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
#endif // DISABLE_IOCTLSOCKET
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
#ifdef DISABLE_IOCTLSOCKET
return waitforsocket(pRS232Port->s, tv);
#else
return checkavailablebytessocket(pRS232Port->s);
#endif // DISABLE_IOCTLSOCKET
case LOCAL_TYPE_RS232PORT:
return CheckAvailableBytesComputerRS232Port(pRS232Port->hDev);
default:
PRINT_DEBUG_ERROR_RS232PORT(("CheckAvailableBytesRS232Port error (%s) : %s(pRS232Port=%#x)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port));
return EXIT_FAILURE;
}
}
/*
Wait for data to read on a RS232 port.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
int timeout : (IN) Max time to wait before returning in ms.
int checkingperiod : (IN) Checking period in ms.
Return : EXIT_SUCCESS if there is data to read, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int WaitForRS232Port(RS232PORT* pRS232Port, int timeout, int checkingperiod)
{
struct timeval tv;
//CHRONO chrono;
tv.tv_sec = (long)(timeout/1000);
tv.tv_usec = (long)((timeout%1000)*1000);
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
return waitforsocket(pRS232Port->s, tv);
case LOCAL_TYPE_RS232PORT:
//StartChrono(&chrono);
//do
//{
// int ret = CheckAvailableBytesComputerRS232Port(pRS232Port->hDev);
// switch (ret)
// {
// case EXIT_SUCCESS:
// StopChronoQuick(&chrono);
// return EXIT_SUCCESS;
// case EXIT_TIMEOUT:
// mSleep(checkingperiod);
// break;
// default:
// StopChronoQuick(&chrono);
// return ret;
// }
//} while (GetTimeElapsedChronoQuick(&chrono) <= timeout);
//StopChronoQuick(&chrono);
//return EXIT_TIMEOUT;
return WaitForComputerRS232Port(pRS232Port->hDev, timeout, checkingperiod);
default:
PRINT_DEBUG_ERROR_RS232PORT(("WaitForRS232Port error (%s) : %s"
"(pRS232Port=%#x, timeout=%d, checkingperiod=%d)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, timeout, checkingperiod));
return EXIT_FAILURE;
}
}
/*
Write data to a RS232 port.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* writebuf : (IN) Valid pointer to the data to write.
int writebuflen : (IN) Number of bytes to write.
int* pWrittenBytes : (INOUT) Valid pointer that will receive the number of bytes written.
Return : EXIT_SUCCESS if some bytes are written, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int WriteRS232Port(RS232PORT* pRS232Port, uint8* writebuf, int writebuflen, int* pWrittenBytes)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
{
#ifdef _DEBUG_MESSAGES_RS232PORT
int i = 0;
#endif // _DEBUG_MESSAGES_RS232PORT
*pWrittenBytes = send(pRS232Port->s, (char*)writebuf, writebuflen, 0);
if (*pWrittenBytes >= 0)
{
if (*pWrittenBytes == 0)
{
PRINT_DEBUG_WARNING_RS232PORT(("WriteRS232Port warning (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, writebuf, writebuflen));
return EXIT_TIMEOUT;
}
else
{
#ifdef _DEBUG_MESSAGES_RS232PORT
for (i = 0; i < *pWrittenBytes; i++)
{
PRINT_DEBUG_MESSAGE_RS232PORT(("%.2x ", (int)writebuf[i]));
}
PRINT_DEBUG_MESSAGE_RS232PORT(("\n"));
#endif // _DEBUG_MESSAGES
PRINT_DEBUG_MESSAGE_RS232PORT(("Bytes written : %d\n", *pWrittenBytes));
}
}
else
{
PRINT_DEBUG_ERROR_RS232PORT(("WriteRS232Port error (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d)\n",
strtime_m(),
WSAGetLastErrorMsg(),
pRS232Port, writebuf, writebuflen));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
case LOCAL_TYPE_RS232PORT:
return WriteComputerRS232Port(pRS232Port->hDev, writebuf, (int)writebuflen, pWrittenBytes);
default:
PRINT_DEBUG_ERROR_RS232PORT(("WriteRS232Port error (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, writebuf, writebuflen));
return EXIT_FAILURE;
}
}
/*
Read data from a RS232 port.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* readbuf : (INOUT) Valid pointer that will receive the data read.
int readbuflen : (IN) Number of bytes to read.
int* pReadBytes : (INOUT) Valid pointer that will receive the number of bytes read.
Return : EXIT_SUCCESS if some bytes are read, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int ReadRS232Port(RS232PORT* pRS232Port, uint8* readbuf, int readbuflen, int* pReadBytes)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
{
#ifdef _DEBUG_MESSAGES_RS232PORT
int i = 0;
#endif // _DEBUG_MESSAGES_RS232PORT
*pReadBytes = recv(pRS232Port->s, (char*)readbuf, readbuflen, 0);
if (*pReadBytes >= 0)
{
if (*pReadBytes == 0)
{
PRINT_DEBUG_WARNING_RS232PORT(("ReadRS232Port warning (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, readbuf, readbuflen));
return EXIT_TIMEOUT;
}
else
{
#ifdef _DEBUG_MESSAGES_RS232PORT
for (i = 0; i < *pReadBytes; i++)
{
PRINT_DEBUG_MESSAGE_RS232PORT(("%.2x ", (int)readbuf[i]));
}
PRINT_DEBUG_MESSAGE_RS232PORT(("\n"));
#endif // _DEBUG_MESSAGES
PRINT_DEBUG_MESSAGE_RS232PORT(("Bytes read : %d\n", *pReadBytes));
}
}
else
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
WSAGetLastErrorMsg(),
pRS232Port, readbuf, readbuflen));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
case LOCAL_TYPE_RS232PORT:
return ReadComputerRS232Port(pRS232Port->hDev, readbuf, (int)readbuflen, pReadBytes);
default:
PRINT_DEBUG_ERROR_RS232PORT(("ReadRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, readbuf, readbuflen));
return EXIT_FAILURE;
}
}
/*
Write data to a RS232 port. Retry automatically if all the bytes were not written.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* writebuf : (IN) Valid pointer to the data to write.
int writebuflen : (IN) Number of bytes to write.
Return : EXIT_SUCCESS if all the bytes are written, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int WriteAllRS232Port(RS232PORT* pRS232Port, uint8* writebuf, int writebuflen)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
return sendall(pRS232Port->s, (char*)writebuf, writebuflen);
case LOCAL_TYPE_RS232PORT:
return WriteAllComputerRS232Port(pRS232Port->hDev, writebuf, (int)writebuflen);
default:
PRINT_DEBUG_ERROR_RS232PORT(("WriteAllRS232Port error (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, writebuf, writebuflen));
return EXIT_FAILURE;
}
}
/*
Write data to a RS232 port with a delay between each byte. Retry automatically if all
the bytes were not written.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* writebuf : (IN) Valid pointer to the data to write.
int writebuflen : (IN) Number of bytes to write.
int bytedelayus : (IN) Delay in us between each byte.
Return : EXIT_SUCCESS if all the bytes are written, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int WriteAllWithByteDelayRS232Port(RS232PORT* pRS232Port, uint8* writebuf, int writebuflen, int bytedelayus)
{
int i = 0;
for (;;)
{
switch (WriteAllRS232Port(pRS232Port, &(writebuf[i]), 1))
{
case EXIT_SUCCESS:
i++;
if (i < writebuflen) uSleep(bytedelayus); else return EXIT_SUCCESS;
break;
case EXIT_TIMEOUT:
PRINT_DEBUG_WARNING_RS232PORT(("WriteAllWithByteDelayRS232Port warning (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d, bytedelayus=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, writebuf, writebuflen, bytedelayus));
return EXIT_TIMEOUT;
default:
PRINT_DEBUG_ERROR_RS232PORT(("WriteAllWithByteDelayRS232Port error (%s) : %s"
"(pRS232Port=%#x, writebuf=%#x, writebuflen=%d, bytedelayus=%d)\n",
strtime_m(),
"WriteAllRS232Port failed. ",
pRS232Port, writebuf, writebuflen, bytedelayus));
return EXIT_FAILURE;
}
}
}
/*
Read data from a RS232 port. Retry automatically if all the bytes were not read.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* readbuf : (INOUT) Valid pointer that will receive the data read.
int readbuflen : (IN) Number of bytes to read.
Return : EXIT_SUCCESS if all the bytes are read, EXIT_TIMEOUT if a timeout occurs or
EXIT_FAILURE if there is an error.
*/
inline int ReadAllRS232Port(RS232PORT* pRS232Port, uint8* readbuf, int readbuflen)
{
switch (pRS232Port->DevType)
{
case TCP_CLIENT_TYPE_RS232PORT:
case TCP_SERVER_TYPE_RS232PORT:
case UDP_CLIENT_TYPE_RS232PORT:
case UDP_SERVER_TYPE_RS232PORT:
return recvall(pRS232Port->s, (char*)readbuf, readbuflen);
case LOCAL_TYPE_RS232PORT:
return ReadAllComputerRS232Port(pRS232Port->hDev, readbuf, (int)readbuflen);
default:
PRINT_DEBUG_ERROR_RS232PORT(("ReadAllRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
"Invalid device type. ",
pRS232Port, readbuf, readbuflen));
return EXIT_FAILURE;
}
}
inline int ReadLatestRS232Port(RS232PORT* pRS232Port, uint8* readbuf, int readbuflen)
{
int BytesReceived = 0;
int Bytes = 0;
uint8* savebuf = NULL;
savebuf = (uint8*)calloc(readbuflen, sizeof(uint8));
if (savebuf == NULL)
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadLatestRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_OUT_OF_MEMORY],
pRS232Port, readbuf, readbuflen));
return EXIT_OUT_OF_MEMORY;
}
if (ReadRS232Port(pRS232Port, readbuf, readbuflen, &Bytes) == EXIT_SUCCESS)
{
if (Bytes == 0)
{
PRINT_DEBUG_WARNING_RS232PORT(("ReadLatestRS232Port warning (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, readbuf, readbuflen));
free(savebuf);
return EXIT_TIMEOUT;
}
else
{
}
}
else
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadLatestRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
"ReadRS232Port failed. ",
pRS232Port, readbuf, readbuflen));
free(savebuf);
return EXIT_FAILURE;
}
BytesReceived += Bytes;
while (Bytes == readbuflen)
{
memcpy(savebuf, readbuf, Bytes);
if (ReadRS232Port(pRS232Port, readbuf, readbuflen, &Bytes) == EXIT_SUCCESS)
{
if (Bytes == 0)
{
PRINT_DEBUG_WARNING_RS232PORT(("ReadLatestRS232Port warning (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, readbuf, readbuflen));
free(savebuf);
return EXIT_TIMEOUT;
}
else
{
}
}
else
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadLatestRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, readbuflen=%d)\n",
strtime_m(),
"ReadRS232Port failed. ",
pRS232Port, readbuf, readbuflen));
free(savebuf);
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
if (BytesReceived < readbuflen)
{
int iResult = ReadAllRS232Port(pRS232Port, readbuf+BytesReceived, readbuflen-BytesReceived);
if (iResult != EXIT_SUCCESS)
{
free(savebuf);
return iResult;
}
}
else
{
memmove(readbuf+readbuflen-Bytes, readbuf, Bytes);
memcpy(readbuf, savebuf+Bytes, readbuflen-Bytes);
}
free(savebuf);
return EXIT_SUCCESS;
}
/*
Receive data from a RS232 port until a specific end string is read.
If this string is found (and the maximum number of bytes to read has not
been reached), it is not necessarily the last read bytes in the buffer
(other bytes might have been read after).
Fail when a timeout occurs.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* readbuf : (INOUT) Valid pointer that will receive the data read
(should be null-terminated, but the search does not include terminating null characters).
char* endstr : (IN) End string to wait for (should be null-terminated, but
the search does not include terminating null characters).
int maxreadbuflen : (IN) Maximum number of bytes to read.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int ReadAtLeastUntilStrRS232Port(RS232PORT* pRS232Port, uint8* readbuf, char* endstr, int maxreadbuflen)
{
int BytesReceived = 0;
int Bytes = 0;
for (;;)
{
if (BytesReceived >= maxreadbuflen)
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadAtLeastUntilRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, endstr=%s, maxreadbuflen=%d)\n",
strtime_m(),
"readbuf full. ",
pRS232Port, readbuf, endstr, maxreadbuflen));
PRINT_DEBUG_MESSAGE_RS232PORT(("Total bytes received : %d\n", BytesReceived));
return EXIT_OUT_OF_MEMORY;
}
if (ReadRS232Port(pRS232Port, readbuf + BytesReceived, maxreadbuflen - BytesReceived, &Bytes) == EXIT_SUCCESS)
{
if (Bytes == 0)
{
PRINT_DEBUG_WARNING_RS232PORT(("ReadAtLeastUntilRS232Port warning (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, endstr=%s, maxreadbuflen=%d)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_TIMEOUT],
pRS232Port, readbuf, endstr, maxreadbuflen));
PRINT_DEBUG_MESSAGE_RS232PORT(("Total bytes received : %d\n", BytesReceived));
return EXIT_TIMEOUT;
}
else
{
PRINT_DEBUG_MESSAGE_RS232PORT(("Bytes received : %d\n", Bytes));
// Look for endstr in the bytes received.
if (strstr((char*)readbuf, endstr))
{
break;
}
}
}
else
{
PRINT_DEBUG_ERROR_RS232PORT(("ReadAtLeastUntilRS232Port error (%s) : %s"
"(pRS232Port=%#x, readbuf=%#x, endstr=%s, maxreadbuflen=%d)\n",
strtime_m(),
"ReadRS232Port failed. ",
pRS232Port, readbuf, endstr, maxreadbuflen));
PRINT_DEBUG_MESSAGE_RS232PORT(("Total bytes received : %d\n", BytesReceived));
return EXIT_FAILURE;
}
BytesReceived += Bytes;
}
PRINT_DEBUG_MESSAGE_RS232PORT(("Total bytes received : %d\n", BytesReceived));
return EXIT_SUCCESS;
}
/*
Receive data from a RS232 port until a specific end character is read.
If this character is found (and the maximum number of bytes to read has not
been reached), it is not necessarily the last read byte in the buffer
(other bytes might have been read after).
Fail when a timeout occurs.
RS232PORT* pRS232Port : (INOUT) Valid pointer to a structure corresponding to
a RS232 port.
uint8* readbuf : (INOUT) Valid pointer that will receive the data read.
char endchar : (IN) End character to wait for.