-
Notifications
You must be signed in to change notification settings - Fork 12
/
SerialPort.cpp
1262 lines (1015 loc) · 33.2 KB
/
SerialPort.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
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
/*
* Copyright 2020 C Moss G6AMU
*
* This 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, or (at your option)
* any later version.
*
* This software 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 this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
// overlapped info .. https://msdn.microsoft.com/en-us/library/ff802693.aspx
#ifdef _MSC_VER
#include "StdAfx.h"
#endif
#define VC_EXTRALEAN
#define WIN32_EXTRA_LEAN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <Windows.hpp>
#include <initguid.h>
//#include "Dbt.h"
#include <setupapi.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#ifdef _DEBUG
#ifdef _MSC_VER
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "SerialPort.h"
#ifdef __BORLANDC__
#pragma package(smart_init)
#endif
const GUID GUID_DEVINTERFACE_COMPORT = {0x86E0D1E0, 0x8089, 0x11D0, {0x9C, 0xE4, 0x08, 0x00, 0x3E, 0x30, 0x1F, 0x73}}; // NanoVNA V1
// ***********************************************************
void __fastcall GetLastErrorStr(DWORD err, char *err_str, int max_size)
{
if (!err_str || max_size <= 0)
return;
memset(err_str, 0, max_size);
char *buf = NULL;
WORD prevErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS); // ignore critical errors
HMODULE wnet_handle = ::GetModuleHandle(_T("wininet.dll"));
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;
if (wnet_handle) flags |= FORMAT_MESSAGE_FROM_HMODULE; // retrieve message from specified DLL
DWORD res = ::FormatMessageA(flags, wnet_handle, err, 0, (LPSTR)&buf, 0, NULL);
if (wnet_handle)
::FreeLibrary(wnet_handle);
if (buf != NULL)
{
#if (__BORLANDC__ < 0x0600)
if (res > 0)
sprintf(err_str, "[%d] %s", err, buf);
#else
if (res > 0)
sprintf_s(err_str, max_size, "[%d] %s", err, buf);
#endif
::LocalFree(buf);
}
::SetErrorMode(prevErrorMode);
}
// ********************************************
CSerialPort::CSerialPort()
{
m_last_error = ERROR_SUCCESS;
memset(m_last_error_str, 0, sizeof(m_last_error_str));
memset(&m_rx.overlapped, 0, sizeof(OVERLAPPED));
memset(&m_tx.overlapped, 0, sizeof(OVERLAPPED));
m_rx.overlapped.hEvent = INVALID_HANDLE_VALUE;
m_tx.overlapped.hEvent = INVALID_HANDLE_VALUE;
m_rx.overlapped_waiting = false;
m_tx.overlapped_waiting = false;
m_device_handle = INVALID_HANDLE_VALUE;
GetSerialPortList();
memset(&m_dcb, 0, sizeof(m_dcb));
m_dcb.DCBlength = sizeof(m_dcb);
m_dcb.BaudRate = 115200;
m_dcb.fBinary = true;
m_dcb.fParity = false;
m_dcb.fOutxCtsFlow = false;
m_dcb.fOutxDsrFlow = false;
m_dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR_CONTROL_DISABLE, DTR_CONTROL_ENABLE, DTR_CONTROL_HANDSHAKE
// m_dcb.fDsrSensitivity:1; // DSR sensitivity
// m_dcb.fTXContinueOnXoff:1; // XOFF continues Tx
m_dcb.fOutX = false;
m_dcb.fInX = false;
// m_dcb.fErrorChar: 1; // enable error replacement
m_dcb.fNull = false;
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // RTS_CONTROL_DISABLE, RTS_CONTROL_ENABLE, RTS_CONTROL_HANDSHAKE
m_dcb.fAbortOnError = false;
// m_dcb.fDummy2:17; // reserved
// m_dcb.XonLim;
// m_dcb.XoffLim;
m_dcb.ByteSize = 8; // 8 bits per byte - when was it ever any different ??????
m_dcb.Parity = NOPARITY;
m_dcb.StopBits = ONESTOPBIT;
// m_dcb.XonChar;
// m_dcb.XoffChar;
// m_dcb.ErrorChar;
// m_dcb.EofChar;
// m_dcb.EvtChar;
memset(&m_timeouts, 0, sizeof(m_timeouts));
m_timeouts.ReadIntervalTimeout = MAXDWORD;
m_timeouts.ReadTotalTimeoutMultiplier = 0;
m_timeouts.ReadTotalTimeoutConstant = 0;
m_timeouts.WriteTotalTimeoutMultiplier = 0;
m_timeouts.WriteTotalTimeoutConstant = 0;
m_rx.queue_size = 65536;
m_tx.queue_size = 65536;
m_buffer.resize(m_rx.queue_size);
m_rx.buffer.resize(1000000);
m_rx.buffer_rd = 0;
m_rx.buffer_wr = 0;
m_tx.buffer.resize(1000000);
m_tx.buffer_rd = 0;
m_tx.buffer_wr = 0;
m_rx.overlapped_waiting = false;
m_tx.overlapped_waiting = false;
m_thread = new CSerialPortThread(&threadProcess);
}
CSerialPort::~CSerialPort()
{
if (m_thread != NULL)
{
m_thread->Terminate();
m_thread->WaitFor();
delete m_thread;
m_thread = NULL;
}
Disconnect();
}
void __fastcall CSerialPort::clearErrors()
{
CCriticalSection cs(m_errors.cs);
m_errors.list.resize(0);
}
void __fastcall CSerialPort::pushError(const DWORD error, SERIALPORT_STRING leading_text)
{
CCriticalSection cs(m_errors.cs);
m_last_error = error;
GetLastErrorStr(error, m_last_error_str, sizeof(m_last_error_str));
t_serial_port_error err = {error, leading_text + SERIALPORT_STRING(m_last_error_str)};
m_errors.list.push_back(err);
}
unsigned int __fastcall CSerialPort::errorCount()
{
// CCriticalSection cs(m_errors.cs);
return m_errors.list.size();
}
t_serial_port_error __fastcall CSerialPort::pullError()
{
t_serial_port_error err = {ERROR_SUCCESS, ""};
CCriticalSection cs(m_errors.cs);
if (!m_errors.list.empty())
{
err = m_errors.list[0];
m_errors.list.erase(m_errors.list.begin() + 0);
}
return err;
}
void __fastcall CSerialPort::getState()
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return;
if (::GetCommModemStatus(m_device_handle, &m_modem_state_read) == FALSE)
pushError(::GetLastError(), "GetCommModemStatus ");
if (::GetCommState(m_device_handle, &m_dcb) == FALSE)
pushError(::GetLastError(), "GetCommState ");
if (::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read) == FALSE)
pushError(::GetLastError(), "ClearCommError ");
}
void __fastcall CSerialPort::GetSerialPortList()
{ // enumerate the com-ports - non-WMI
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
DWORD err;
m_serialPortList.resize(0);
HKEY hKey = NULL;
err = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
if (err != ERROR_SUCCESS || hKey == NULL)
{
pushError(err, "reg ");
if (hKey != NULL)
::RegCloseKey(hKey);
return;
}
// TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
// DWORD cbName; // size of name string
char achClass[MAX_PATH]; // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
char achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
// get the class name and the value count
err = ::RegQueryInfoKeyA(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
if (err != ERROR_SUCCESS)
{
pushError(err, "reg ");
::RegCloseKey(hKey);
return;
}
if (cValues <= 0)
{ // no serial ports
::RegCloseKey(hKey);
return;
}
for (int i = 0; i < (int)cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
err = ::RegEnumValueA(hKey, i, achValue, &cchValue, NULL, NULL, NULL, NULL);
if (err != ERROR_SUCCESS)
{
pushError(err, "reg ");
break;
// continue;
}
DWORD type;
char str[MAX_PATH];
DWORD size = sizeof(str);
err = ::RegQueryValueExA(hKey, achValue, NULL, &type, (BYTE *)str, &size);
if (err != ERROR_SUCCESS)
{
pushError(err, "reg ");
break;
// continue;
}
// check to see if we already have it
unsigned int k = 0;
while (k < m_serialPortList.size())
{
if (SERIALPORT_STRING(m_serialPortList[k].name).LowerCase() == SERIALPORT_STRING(str).LowerCase())
break; // already in the list
k++;
}
// save it into our list if it's not already there
if (k >= m_serialPortList.size())
{
T_SerialPortInfo spi;
spi.name = SERIALPORT_STRING(str);
spi.deviceID = SERIALPORT_STRING(achValue);
spi.vid = -1;
spi.pid = -1;
m_serialPortList.push_back(spi);
}
}
::RegCloseKey(hKey);
// ****************************
// fetch the VID/PID (if any)
//const GUID guid = GUID_DEVINTERFACE_USB_DEVICE;
const GUID guid = GUID_DEVINTERFACE_COMPORT;
//HDEVINFO dev_info = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
HDEVINFO dev_info = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (dev_info != INVALID_HANDLE_VALUE)
{
int devIndex = -1;
std::vector <uint8_t> buffer;
while (true)
{
BOOL res;
char str[512];
struct
{
int index;
SERIALPORT_STRING path;
SERIALPORT_STRING description;
//SERIALPORT_STRING enumerator_name;
SERIALPORT_STRING friendly_name;
//SERIALPORT_STRING location_information;
SERIALPORT_STRING mfg;
SERIALPORT_STRING service;
int vid;
int pid;
} usb_device;
usb_device.index = ++devIndex;
usb_device.vid = -1;
usb_device.pid = -1;
SP_INTERFACE_DEVICE_DATA ifData;
memset(&ifData, 0, sizeof(SP_INTERFACE_DEVICE_DATA));
ifData.cbSize = sizeof(ifData);
res = ::SetupDiEnumDeviceInterfaces(dev_info, NULL, &guid, devIndex, &ifData);
if (!res)
{ // not found
::SetupDiDestroyDeviceInfoList(dev_info);
//dev_info = NULL;
break;
}
SP_DEVINFO_DATA did;
memset(&did, 0, sizeof(SP_DEVINFO_DATA));
did.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD needed = 0;
::SetupDiGetDeviceInterfaceDetail(dev_info, &ifData, NULL, 0, &needed, &did);
//res = ::SetupDiGetDeviceInterfaceDetail(dev_info, &ifData, NULL, 0, &needed, NULL);
if (needed <= 0)
continue;
buffer.resize(needed);
memset(&buffer[0], 0, needed);
SP_INTERFACE_DEVICE_DETAIL_DATA *detail = (SP_INTERFACE_DEVICE_DETAIL_DATA *)&buffer[0];
detail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
//SP_DEVINFO_DATA did;
//memset(&did, 0, sizeof(SP_DEVINFO_DATA));
//did.cbSize = sizeof(SP_DEVINFO_DATA);
//res = ::SetupDiGetDeviceInterfaceDetail(dev_info, &ifData, detail, needed, NULL, &did);
res = ::SetupDiGetDeviceInterfaceDetail(dev_info, &ifData, detail, needed, NULL, NULL);
if (!res)
continue;
usb_device.path = SERIALPORT_STRING(detail->DevicePath);
memset(str, 0, sizeof(str));
res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_DEVICEDESC, NULL, (PBYTE)str, sizeof(str), NULL);
if (res)
usb_device.description = SERIALPORT_STRING(str);
//memset(str, 0, sizeof(str));
//res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_ENUMERATOR_NAME, NULL, str, sizeof(str), NULL);
//if (res)
// usb_device.enumerator_name = SERIALPORT_STRING(str);
memset(str, 0, sizeof(str));
res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_FRIENDLYNAME, NULL, (PBYTE)str, sizeof(str), NULL);
if (res)
usb_device.friendly_name = SERIALPORT_STRING(str);
//memset(str, 0, sizeof(str));
//res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_LOCATION_INFORMATION, NULL, str, sizeof(str), NULL);
//if (res)
// usb_device.location_information = SERIALPORT_STRING(str);
memset(str, 0, sizeof(str));
res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_MFG, NULL, (PBYTE)str, sizeof(str), NULL);
if (res)
usb_device.mfg = SERIALPORT_STRING(str);
memset(str, 0, sizeof(str));
res = ::SetupDiGetDeviceRegistryProperty(dev_info, &did, SPDRP_SERVICE, NULL, (PBYTE)str, sizeof(str), NULL);
if (res)
usb_device.service = SERIALPORT_STRING(str);
if (!usb_device.path.IsEmpty())
{
int i;
SERIALPORT_STRING s = usb_device.path.LowerCase();
i = s.Pos("vid_");
if (i > 0)
{
SERIALPORT_STRING str = s.SubString(i + 4, 5);
i = str.Pos('&');
if (i > 0)
str = str.SubString(1, i - 1);
if (!TryStrToInt("0x" + str, usb_device.vid))
usb_device.vid = -1;
}
i = s.Pos("pid_");
if (i > 0)
{
SERIALPORT_STRING str = s.SubString(i + 4, 5);
i = str.Pos('#');
if (i > 0)
str = str.SubString(1, i - 1);
if (!TryStrToInt("0x" + str, usb_device.pid))
usb_device.pid = -1;
}
}
// device path: \\?\usb#vid_0483&pid_5740#400#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
// device name: STMicroelectronics Virtual COM Port (COM25)
//
// device path: \\?\ports#vid_04b4&pid_0008#demo#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
// device name: USB Serial Port (COM12)
if (usb_device.vid >= 0 && usb_device.pid >= 0)
{
for (unsigned int i = 0; i < m_serialPortList.size(); i++)
{
T_SerialPortInfo *spi = &m_serialPortList[i];
//SERIALPORT_STRING s = "(" + spi->name + ")";
SERIALPORT_STRING s = spi->name;
if (usb_device.friendly_name.LowerCase().Pos(s.LowerCase()) > 0)
{ // found it
spi->description = usb_device.description;
spi->path = usb_device.path;
spi->friendly_name = usb_device.friendly_name;
spi->mfg = usb_device.mfg;
spi->service = usb_device.service;
spi->vid = usb_device.vid;
spi->pid = usb_device.pid;
break;
}
}
}
//pushCommMessage(" device path: " + usb_device.path);
//pushCommMessage(" device desc: " + usb_device.description);
//pushCommMessage(" device enum: " + usb_device.enumerator_name);
//pushCommMessage(" device name: " + usb_device.friendly_name);
//pushCommMessage("device loc info: " + usb_device.location_information);
//pushCommMessage(" device mfg: " + usb_device.mfg);
//pushCommMessage(" device service: " + usb_device.service);
}
}
// ****************************
}
void __fastcall CSerialPort::GetSerialPortList(std::vector <T_SerialPortInfo> &serialPortList)
{
GetSerialPortList();
serialPortList.clear();
for (int i = 0; i < (int)m_serialPortList.size(); i++)
serialPortList.push_back(m_serialPortList[i]);
}
bool __fastcall CSerialPort::Connected()
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return false;
if (::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read) == FALSE)
{ // close the comm port
pushError(::GetLastError(), "connected ClearCommError ");
// if (m_last_error == ERROR_ACCESS_DENIED)
// { // tends to mean they have unplugged the USB serial port, or we're no longer connected
// }
Disconnect();
}
return (m_device_handle != INVALID_HANDLE_VALUE) ? true : false;
}
int __fastcall CSerialPort::Connect(SERIALPORT_STRING device_name)
{
HANDLE device_handle;
if (m_device_handle != INVALID_HANDLE_VALUE)
Disconnect();
clearErrors();
memset(&m_rx.overlapped, 0, sizeof(m_rx.overlapped));
memset(&m_tx.overlapped, 0, sizeof(m_tx.overlapped));
m_rx.overlapped.hEvent = INVALID_HANDLE_VALUE;
m_tx.overlapped.hEvent = INVALID_HANDLE_VALUE;
m_rx.overlapped_waiting = false;
m_tx.overlapped_waiting = false;
m_rx.buffer_rd = 0;
m_rx.buffer_wr = 0;
m_tx.buffer_rd = 0;
m_tx.buffer_wr = 0;
if (device_name.IsEmpty())
return -1;
m_device_name = device_name;
SERIALPORT_STRING name = SERIALPORT_STRING("\\\\.\\") + device_name;
#if defined(__BORLANDC__)
#ifdef SERIAL_OVERLAPPED
device_handle = ::CreateFileA(AnsiString(name).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0 | FILE_FLAG_OVERLAPPED, NULL);
#else
device_handle = ::CreateFileA(AnsiString(name).c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#endif
#elif defined(_MSC_VER)
#ifdef SERIAL_OVERLAPPED
device_handle = ::CreateFileA(name.GetString(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0 | FILE_FLAG_OVERLAPPED, NULL);
#else
device_handle = ::CreateFileA(name.GetString(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#endif
#else
#error "FIX ME"
#endif
if (device_handle == INVALID_HANDLE_VALUE)
{
pushError(::GetLastError(), "connect CreateFile ");
return m_last_error;
}
if (::GetCommState(device_handle, &m_original_dcb) == FALSE)
pushError(::GetLastError(), "connect GetCommState ");
if (::GetCommTimeouts(device_handle, &m_original_timeouts) == FALSE)
pushError(::GetLastError(), "connect GetCommTimeouts ");
if (::GetCommModemStatus(device_handle, &m_modem_state_read) == FALSE)
pushError(::GetLastError(), "connect GetCommModemStatus ");
if (::SetCommState(device_handle, &m_dcb) == FALSE)
pushError(::GetLastError(), "connect SetCommState ");
if (::SetCommTimeouts(device_handle, &m_timeouts) == FALSE)
pushError(::GetLastError(), "connect SetCommTimeouts ");
if (::SetupComm(device_handle, m_rx.queue_size, m_tx.queue_size) == FALSE)
pushError(::GetLastError(), "connect SetupComm ");
#ifdef SERIAL_OVERLAPPED
m_rx.overlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_rx.overlapped.hEvent == NULL || m_rx.overlapped.hEvent == INVALID_HANDLE_VALUE)
pushError(::GetLastError(), "connect CreateEvent ");
m_tx.overlapped.hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_tx.overlapped.hEvent == NULL || m_tx.overlapped.hEvent == INVALID_HANDLE_VALUE)
pushError(::GetLastError(), "connect CreateEvent ");
//if (::SetCommMask(m_device_handle, EV_TXEMPTY | EV_RXCHAR) == FALSE)
//if (::SetCommMask(m_device_handle, EV_RXCHAR | EV_BREAK) == FALSE)
// pushError(::GetLastError(), "connect SetCommMask ");
#endif
// flush the Tx/Rx buffers
::PurgeComm(device_handle, PURGE_RXABORT | PURGE_TXABORT | PURGE_RXCLEAR | PURGE_TXCLEAR);
::ClearCommError(device_handle, &m_errors_read, &m_stat_read);
m_device_handle = device_handle;
return ERROR_SUCCESS; // OK
}
void __fastcall CSerialPort::Disconnect()
{
const HANDLE handle = m_device_handle;
m_device_handle = INVALID_HANDLE_VALUE; // stops the thread accessing the serial port
m_rx.buffer_rd = 0;
m_rx.buffer_wr = 0;
m_tx.buffer_rd = 0;
m_tx.buffer_wr = 0;
if (m_rx.overlapped.hEvent != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_rx.overlapped.hEvent);
memset(&m_rx.overlapped, 0, sizeof(m_rx.overlapped));
m_rx.overlapped.hEvent = INVALID_HANDLE_VALUE;
}
if (m_tx.overlapped.hEvent != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_tx.overlapped.hEvent);
memset(&m_tx.overlapped, 0, sizeof(m_tx.overlapped));
m_tx.overlapped.hEvent = INVALID_HANDLE_VALUE;
}
if (handle != INVALID_HANDLE_VALUE)
{
::PurgeComm(handle, PURGE_RXABORT | PURGE_RXCLEAR);
::ClearCommError(handle, NULL, NULL);
::SetCommState(handle, &m_original_dcb);
::SetCommTimeouts(handle, &m_original_timeouts);
::ClearCommError(handle, NULL, NULL);
::CloseHandle(handle);
}
}
void __fastcall CSerialPort::processTx()
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return;
DWORD bytes_written = 0;
const int wr = m_tx.buffer_wr;
int rd = m_tx.buffer_rd;
int len = (wr >= rd) ? wr - rd : m_tx.buffer.size() - rd;
if (m_tx.overlapped.hEvent == INVALID_HANDLE_VALUE)
{ // non-overlapped
if (len > 0)
{
if (::WriteFile(m_device_handle, &m_tx.buffer[rd], len, &bytes_written, NULL) == FALSE)
{
pushError(::GetLastError(), "tx WriteFile ");
//::ClearCommBreak(m_device_handle);
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
return;
}
}
}
else
{ // overlapped
if (!m_tx.overlapped_waiting)
{
if (len > 0)
{
if (::WriteFile(m_device_handle, &m_tx.buffer[rd], len, &bytes_written, &m_tx.overlapped) == FALSE)
{
const DWORD error = ::GetLastError();
if (error != ERROR_IO_PENDING)
{ // error
//::ClearCommBreak(m_device_handle);
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
pushError(error, "tx overlapped WriteFile ");
return;
}
m_tx.overlapped_waiting = true;
}
}
}
if (m_tx.overlapped_waiting)
{
if (::GetOverlappedResult(m_device_handle, &m_tx.overlapped, &bytes_written, FALSE) == FALSE)
{
const DWORD error = ::GetLastError();
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
//if (error == ERROR_SEM_TIMEOUT)
// m_tx.overlapped_waiting = false;
if (error != ERROR_IO_INCOMPLETE)
{
pushError(error, "tx GetOverlappedResult ");
m_rx.overlapped_waiting = false;
}
}
else
m_tx.overlapped_waiting = false;
}
}
if (bytes_written > 0)
{
rd += bytes_written;
if (rd >= (int)m_tx.buffer.size())
rd -= m_tx.buffer.size();
m_tx.buffer_rd = rd;
}
}
void __fastcall CSerialPort::processRx()
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return;
DWORD bytes_available = 0;
DWORD bytes_read = 0;
if (::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read) == FALSE)
pushError(::GetLastError(), "rx ClearCommError ");
else
bytes_available = m_stat_read.cbInQue;
if (m_buffer.size() < bytes_available)
m_buffer.resize(bytes_available * 4);
if (m_rx.overlapped.hEvent == INVALID_HANDLE_VALUE)
{ // non-overlapped
if (bytes_available > 0)
{
if (!m_buffer.empty())
{
if (::ReadFile(m_device_handle, &m_buffer[0], m_buffer.size(), &bytes_read, NULL) == FALSE)
// if (::ReadFile(m_device_handle, &m_buffer[0], bytes_available, &bytes_read, NULL) == FALSE)
{
const DWORD err = ::GetLastError();
// if (err != ERROR_INVALID_PARAMETER)
pushError(err, "rx ReadFile ");
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
return;
}
}
}
}
else
{ // overlapped
if (!m_rx.overlapped_waiting)
{
if (bytes_available > 0)
{
if (!m_buffer.empty())
{
if (::ReadFile(m_device_handle, &m_buffer[0], m_buffer.size(), &bytes_read, &m_rx.overlapped) == FALSE)
{
const DWORD error = ::GetLastError();
if (error != ERROR_IO_PENDING)
{
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
pushError(error, "rx overlapped ReadFile ");
return;
}
m_rx.overlapped_waiting = true;
}
}
}
}
if (m_rx.overlapped_waiting)
{
if (::GetOverlappedResult(m_device_handle, &m_rx.overlapped, &bytes_read, FALSE) == FALSE)
{
const DWORD error = ::GetLastError();
::ClearCommError(m_device_handle, &m_errors_read, &m_stat_read);
//if (error == ERROR_SEM_TIMEOUT)
// m_rx.overlapped_waiting = false;
if (error != ERROR_IO_INCOMPLETE)
{
pushError(error, "rx GetOverlappedResult ");
m_rx.overlapped_waiting = false;
}
}
else
m_rx.overlapped_waiting = false;
}
}
if (bytes_read > 0)
{ // copy the received bytes into our rx buffer
int loops = 0;
int rd = 0;
while (m_thread != NULL && loops++ < 100 && rd < (int)bytes_read)
{
int buf_rd = m_rx.buffer_rd;
int buf_wr = m_rx.buffer_wr;
int len = (buf_wr >= buf_rd) ? (int)m_rx.buffer.size() - buf_wr : buf_rd - buf_wr;
if (len > ((int)bytes_read - rd))
len = (int)bytes_read - rd;
if (len > 0)
{
memcpy(&m_rx.buffer[buf_wr], &m_buffer[rd], len);
rd += len;
buf_wr += len;
if (buf_wr >= (int)m_rx.buffer.size())
buf_wr -= m_rx.buffer.size();
m_rx.buffer_wr = buf_wr;
}
else
{ // wait a bit for the exec to free up some buffer space
m_thread->Sleep(1);
}
}
}
}
void __fastcall CSerialPort::threadProcess()
{
if (m_device_handle == INVALID_HANDLE_VALUE || m_thread == NULL)
return;
processTx();
processRx();
}
int __fastcall CSerialPort::RxBytesAvailable()
{ // return number of available received bytes
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
const int wr = m_rx.buffer_wr;
const int rd = m_rx.buffer_rd;
return (wr >= rd) ? wr - rd : (m_rx.buffer.size() - rd) + wr;
}
int __fastcall CSerialPort::RxByte()
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
const int wr = m_rx.buffer_wr;
int rd = m_rx.buffer_rd;
if (rd == wr)
return -2;
const int i = m_rx.buffer[rd];
if (++rd >= (int)m_rx.buffer.size())
rd -= m_rx.buffer.size();
m_rx.buffer_rd = rd;
return i;
}
int __fastcall CSerialPort::RxBytePeek()
{ // fetch a byte without removing it from the rx buffer
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
const int wr = m_rx.buffer_wr;
int rd = m_rx.buffer_rd;
if (rd == wr)
return -2;
return (int)m_rx.buffer[rd];
}
int __fastcall CSerialPort::RxBytes(void *buf, int size)
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
uint8_t *buffer = (uint8_t *)buf;
int bytes_written = 0;
while (bytes_written < size)
{
int buf_wr = m_rx.buffer_wr;
int buf_rd = m_rx.buffer_rd;
int len = (buf_wr >= buf_rd) ? buf_wr - buf_rd : m_rx.buffer.size() - buf_rd;
if (len > (size - bytes_written))
len = size - bytes_written;
if (len <= 0)
break;
memcpy(&buffer[bytes_written], &m_rx.buffer[buf_rd], len);
bytes_written += len;
buf_rd += len;
if (buf_rd >= (int)m_rx.buffer.size())
buf_rd -= m_rx.buffer.size();
m_rx.buffer_rd = buf_rd;
}
return bytes_written;
}
int __fastcall CSerialPort::RxBytesPeek(void *buf, int size)
{ // fetch bytes without removing them from the rx buffer
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
uint8_t *buffer = (uint8_t *)buf;
int bytes_written = 0;
int buf_rd = m_rx.buffer_rd;
while (bytes_written < size)
{
int buf_wr = m_rx.buffer_wr;
int len = (buf_wr >= buf_rd) ? buf_wr - buf_rd : m_rx.buffer.size() - buf_rd;
if (len > (size - bytes_written))
len = size - bytes_written;
if (len <= 0)
break;
memcpy(&buffer[bytes_written], &m_rx.buffer[buf_rd], len);
bytes_written += len;
buf_rd += len;
if (buf_rd >= (int)m_rx.buffer.size())
buf_rd -= m_rx.buffer.size();
}
return bytes_written;
}
bool __fastcall CSerialPort::TxEmpty()
{
return (m_device_handle == INVALID_HANDLE_VALUE || m_tx.buffer_rd == m_tx.buffer_wr) ? true : false;
}
int __fastcall CSerialPort::TxBytesWaiting()
{ // return number of queued transmit bytes
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
const int rd = m_tx.buffer_rd;
const int wr = m_tx.buffer_wr;
const int bytes_available = (wr >= rd) ? wr - rd : (m_tx.buffer.size() - rd) + wr;
return bytes_available;
}
int __fastcall CSerialPort::TxByte(const int b)
{
if (m_device_handle == INVALID_HANDLE_VALUE)
return -1;
const int buf_rd = m_tx.buffer_rd;
int buf_wr = m_tx.buffer_wr;
int space_available = (buf_wr >= buf_rd) ? (int)m_tx.buffer.size() - (buf_wr - buf_rd) : buf_rd - buf_wr;
if (space_available > 0)
space_available--;
if (space_available < 1)
return 0;
m_tx.buffer[buf_wr] = (uint8_t)b;
if (++buf_wr >= (int)m_tx.buffer.size())
buf_wr -= m_tx.buffer.size();