-
Notifications
You must be signed in to change notification settings - Fork 6
/
SerialPort.cpp
931 lines (781 loc) · 25.7 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
/*
** FILENAME CSerialPort.cpp
**
** PURPOSE This class can read, write and watch one serial port.
** It sends messages to its owner when something happends on the port
** The class creates a thread for reading and writing so the main
** program is not blocked.
** NOTE: Writing is now not on thread *** ARW ***
**
** CREATION DATE 15-09-1997
** LAST MODIFICATION 05-04-2020
**
** AUTHOR Remon Spekreijse
** MODIFIED BY Brian Koh Sze Hsian - Andy Whittaker - Tom Herrmann
**
*/
// (c) 1996-99 Andy Whittaker, Chester, England.
#include "SerialPort.h"
#include "EnumSer.h"
#include <assert.h>
#include <afxwin.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Static members must be globally defined
CRITICAL_SECTION CSerialPort::m_csCommunicationSync;
OVERLAPPED CSerialPort::m_ov;
int CSerialPort::m_nActualWriteBufferSize = 0;
////////// Exception handling code
void AfxThrowSerialException(DWORD dwError /* = 0 */)
{
if (dwError == 0)
dwError = ::GetLastError();
CSerialException* pException = new CSerialException(dwError);
TRACE(_T("Warning: throwing CSerialException for error %d\n"), dwError);
THROW(pException);
}
BOOL CSerialException::GetErrorMessage(LPTSTR pstrError, UINT nMaxError, PUINT pnHelpContext)
{
ASSERT(pstrError != NULL && AfxIsValidString(pstrError, nMaxError));
if (pnHelpContext != NULL)
*pnHelpContext = 0;
LPTSTR lpBuffer;
BOOL bRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, m_dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
(LPTSTR) &lpBuffer, 0, NULL);
if (bRet == FALSE)
*pstrError = '\0';
else
{
lstrcpyn(pstrError, lpBuffer, nMaxError);
bRet = TRUE;
LocalFree(lpBuffer);
}
return bRet;
}
CString CSerialException::GetErrorMessage()
{
CString rVal;
LPTSTR pstrError = rVal.GetBuffer(4096);
GetErrorMessage(pstrError, 4096, NULL);
rVal.ReleaseBuffer();
return rVal;
}
CSerialException::CSerialException(DWORD dwError)
{
m_dwError = dwError;
}
CSerialException::~CSerialException()
{
}
IMPLEMENT_DYNAMIC(CSerialException, CException)
#ifdef _DEBUG
void CSerialException::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "m_dwError = " << m_dwError;
}
#endif
//
// Constructor
CSerialPort::CSerialPort()
{
m_hComm = NULL;
m_Thread = NULL; // arw: fix for assertion without creating thread.
// initialize overlapped structure members to zero
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
// create events
m_ov.hEvent = NULL;
m_hShutdownEvent = NULL;
m_bThreadAlive = FALSE;
m_szWriteBuffer = NULL;
m_nWriteBufferSize = 0;
// Recall previous settings from the registry, default Com 1 in case 1st time run.
CWinApp* pApp = AfxGetApp();
m_nPortNr = pApp->GetProfileInt(_T("Communications"), _T("Port"), 1);
m_nWriteDelay = pApp->GetProfileInt(_T("Communications"), _T("WriteDelay"), 100);
m_timeLastWrittenToPort = { 0 };
m_bInitDone = FALSE;
}
// Delete dynamic memory
CSerialPort::~CSerialPort()
{
// Save our settings to the registry
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(_T("Communications"), _T("Port"), m_nPortNr);
pApp->WriteProfileInt(_T("Communications"), _T("WriteDelay"), m_nWriteDelay);
// resume the thread if it was suspended. Needed to process m_hShutdownEvent!
if(m_Thread != NULL)
{
DWORD dwSuspendCount;
do
{
dwSuspendCount = m_Thread->ResumeThread();
}
while ((dwSuspendCount != 0) && (dwSuspendCount != 0xffffffff));
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE(_T("Thread ended\n"));
}
// close handles to avoid memory leaks
CloseHandle(m_ov.hEvent);
CloseHandle(m_hComm);
CloseHandle(m_hShutdownEvent);
m_hComm = NULL;
m_ov.hEvent = NULL;
m_hShutdownEvent = NULL;
if (m_szWriteBuffer != NULL)
delete[] m_szWriteBuffer;
DeleteCriticalSection(&m_csCommunicationSync);
}
IMPLEMENT_DYNAMIC(CSerialPort, CObject)
#ifdef _DEBUG
void CSerialPort::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << _T("m_hComm = ") << m_hComm << _T("\n");
}
#endif
//
// Initialize the port. This can be any Com Port.
BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (e.g. 1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
ASSERT(portnr >= 0 && portnr < 15);
if (portnr == NULL)
{// Use the last port setting.
TRACE(_T("CSerialPort::InitPort - Setting COM Port to last value\n"));
CWinApp* pApp = AfxGetApp();
m_nPortNr = pApp->GetProfileInt(_T("Communications"), _T("Port"), 1);
}
else
m_nPortNr = portnr;
// Call the 2nd override function
return InitPort(pPortOwner, baud, parity, databits, stopbits, dwCommEvents, writebuffersize);
}
// 2nd over-ride - uses the previous Com port number
BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
m_bInitDone = FALSE;
ASSERT(pPortOwner != NULL);
CWinApp* pApp = AfxGetApp();
m_nPortNr = pApp->GetProfileInt(_T("Communications"), _T("Port"), 1);
TRACE(_T("CSerialPort::InitPort - Com Port %d\n"), m_nPortNr);
if (m_nPortNr < 1)
{// This should never happen, however a mistake could have been made in the registry.
TRACE(_T("CSerialPort::InitPort - Com Port cannot be 0\n"));
return FALSE;
}
if (m_nPortNr > 255)
{// This should never happen, however a mistake could have been made in the registry.
TRACE(_T("CSerialPort::InitPort - Com Port cannot be greater than 255\n"));
return FALSE;
}
CEnumerateSerial::CPortsArray m_cuPorts;
CEnumerateSerial::UsingCreateFile(m_cuPorts);
if (m_cuPorts.size() == 0) {
TRACE(_T("CSerialPort::InitPort - No Com Ports found\n"));
return FALSE;
}
else {
BOOL foundPort = FALSE;
for (UINT i = 0; i < m_cuPorts.size(); ++i) {
if (m_nPortNr == m_cuPorts[i]) {
foundPort = TRUE;
break;
}
}
if (foundPort == FALSE) {
TRACE(_T("CSerialPort::InitPort - Selected Com Port not found\n"));
return FALSE;
}
}
// if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE(_T("Thread ended\n"));
}
// create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!m_ov.hEvent)
AfxThrowSerialException();
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!m_hShutdownEvent)
AfxThrowSerialException();
// initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
// initialize critical section
InitializeCriticalSection(&m_csCommunicationSync);
// set buffersize for writing and save the owner
m_pOwner = pPortOwner;
m_nWriteBufferSize = min(writebuffersize, MAX_WRITE_BUFFER);
if (m_szWriteBuffer != NULL) // delete the buffer if it exists
delete [] m_szWriteBuffer;
m_szWriteBuffer = new unsigned char[m_nWriteBufferSize + 1];
memset(m_szWriteBuffer, 0, m_nWriteBufferSize + 1);
m_dwCommEvents = dwCommEvents;
char *szPort = new char[50];
char *szBaud = new char[50];
// now it's critical!
EnterCriticalSection(&m_csCommunicationSync);
// if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// prepare port strings
sprintf_s(szPort, 50, _T("COM%d"), m_nPortNr);
sprintf_s(szBaud, 50, _T("baud=%d parity=%c data=%d stop=%d"), baud, parity, databits, stopbits);
// get a handle to the port
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud;
TRACE(_T("Failed to open up the comms port\n"));
AfxThrowSerialException();
return FALSE;
}
// set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 0;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
m_CommTimeouts.ReadTotalTimeoutConstant = 0;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
m_CommTimeouts.WriteTotalTimeoutConstant = 0;
// configure
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{ // DCB Structure
m_dcb.fRtsControl = RTS_CONTROL_DISABLE; // disable RTS
m_dcb.fOutxCtsFlow = FALSE; // disable CTS
m_dcb.fOutxDsrFlow = FALSE; // disable DSR
m_dcb.fDtrControl = DTR_CONTROL_DISABLE; // disable DTR
m_dcb.fDsrSensitivity = FALSE; // Ignores DSR
m_dcb.fNull = FALSE; // Disable Null Stripping
m_dcb.fTXContinueOnXoff = FALSE; // disable XON/XOFF
m_dcb.fBinary = TRUE; // This is always TRUE in WIN32
m_dcb.fOutX = FALSE; // Disable XOFF checking
m_dcb.fInX = FALSE; // Disable XOFF generation
m_dcb.DCBlength = sizeof(DCB);
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb))
; // normal operation... continue
else
ProcessErrorMessage(_T("SetCommState()"));
}
else
ProcessErrorMessage(_T("BuildCommDCB()"));
}
else
ProcessErrorMessage(_T("GetCommState()"));
}
else
ProcessErrorMessage(_T("SetCommMask()"));
}
else
ProcessErrorMessage(_T("SetCommTimeouts()"));
delete [] szPort;
delete [] szBaud;
// flush the port
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
// release critical section
LeaveCriticalSection(&m_csCommunicationSync);
EscapeCommFunction(m_hComm, SETDTR); // This is needed for self-powered interfaces
EscapeCommFunction(m_hComm, SETRTS); // This is needed for self-powered interfaces
TRACE(_T("Initialisation for Com Port %d completed.\n"),
m_nPortNr);
ftime(&m_timeLastWrittenToPort); // init this to senseble value
m_bInitDone = TRUE;
return TRUE;
}
// The CommThread Function.
UINT CSerialPort::CommThread(LPVOID pParam)
{
// Cast the void pointer passed to the thread, back to
// a pointer of CSerialPort class
CSerialPort *port = (CSerialPort*)pParam;
TRACE(_T("Com Port %d thread is starting.\n"), port->m_nPortNr);
// Set the status variable in the dialog class to
// TRUE to indicate the thread is running.
port->m_bThreadAlive = TRUE;
// Misc. variables
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat = { 0 };
BOOL bResult = TRUE;
// Clear comm buffers at startup
if (port->m_hComm) // check if the port is opened
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
// begin forever loop. This loop will run as long as the thread is alive.
for (;;)
{
// Make a call to WaitCommEvent(). This call will return immediatly
// because our port was created as an async port (FILE_FLAG_OVERLAPPED
// and an m_OverlappedStructerlapped structure specified). This call will cause the
// m_OverlappedStructerlapped element m_OverlappedStruct.hEvent, which is part of
// the m_hEventArray, to be placed in a non-signaled state if there are no bytes
// available to be read, or to a signaled state if there are bytes available.
// If this event handle is set to the non-signaled state, it will be set to
// signaled when a character arrives at the port.
// we do this for each port!
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
if (!bResult)
{
// If WaitCommEvent() returns FALSE, process the last error to determin
// the reason..
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// This is a normal return value if there are no bytes
// to read at the port.
// Do nothing and continue
break;
}
case 87:
{
// Under Windows NT, this value is returned for some reason.
// I have not investigated why, but it is also a valid reply
// Also do nothing and continue.
break;
}
default:
{
// All other error codes indicate a serious error has
// occurred. Process this error.
port->ProcessErrorMessage("WaitCommEvent()");
AfxThrowSerialException();
break;
}
}
}
else
{
// If WaitCommEvent() returns TRUE, check to be sure there are
// actually bytes in the buffer to read.
//
// If you are reading more than one byte at a time from the buffer
// (which this program does not do) you will have the situation occur
// where the first byte to arrive will cause the WaitForMultipleObjects()
// function to stop waiting. The WaitForMultipleObjects() function
// resets the event handle in m_OverlappedStruct.hEvent to the non-signaled state
// as it returns.
//
// If in the time between the reset of this event and the call to
// ReadFile() more bytes arrive, the m_OverlappedStruct.hEvent handle will be set again
// to the signeled state. When the call to ReadFile() occurs, it will
// read all of the bytes from the buffer, and the program will
// loop back around to WaitCommEvent().
//
// At this point you will be in the situation where m_OverlappedStruct.hEvent is set,
// but there are no bytes available to read. If you proceed and call
// ReadFile(), it will return immediatly due to the async port setup, but
// GetOverlappedResults() will not return until the next character arrives.
//
// It is not desirable for the GetOverlappedResults() function to be in
// this state. The thread shutdown event (event 0) will not work if the
// thread is blocked by GetOverlappedResults().
//
// The solution to this is to check the buffer with a call to ClearCommError().
// This call will reset the event handle, and if there are no bytes to read
// we can loop back through WaitCommEvent() again, then proceed.
// If there are really bytes to read, do nothing and proceed.
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
if (!bResult)
AfxThrowSerialException();
if (comstat.cbInQue == 0)
continue;
} // end if bResult
// Main wait function. This function will normally block the thread
// until one of nine events occur that require action.
Event = WaitForMultipleObjects(2, port->m_hEventArray, FALSE, INFINITE);
Event = Event - WAIT_OBJECT_0; // WaitForMultipleObjects help says to do this.
switch (Event)
{
case 0:
{
// Shutdown event. This is event zero so it will be
// the highest priority and be serviced first.
// Tell the rest of the code that the thread is dying.
port->m_bThreadAlive = FALSE;
break;
} // case 0
case 1: // read event
{
bResult = GetCommMask(port->m_hComm, &CommEvent);
if (!bResult)
AfxThrowSerialException();
// if (CommEvent & EV_CTS)
// ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_CTS_DETECTED,
// (WPARAM) 0, (LPARAM) port->m_nPortNr);
// if (CommEvent & EV_RXFLAG)
// ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RXFLAG_DETECTED,
// (WPARAM) 0, (LPARAM) port->m_nPortNr);
// if (CommEvent & EV_BREAK)
// ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_BREAK_DETECTED,
// (WPARAM) 0, (LPARAM) port->m_nPortNr);
// if (CommEvent & EV_ERR)
// ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_ERR_DETECTED,
// (WPARAM) 0, (LPARAM) port->m_nPortNr);
// if (CommEvent & EV_RING)
// ::SendMessage(port->m_pOwner->m_hWnd, WM_COMM_RING_DETECTED,
// (WPARAM) 0, (LPARAM) port->m_nPortNr);
if (CommEvent & EV_RXCHAR)
// Receive character event from port.
ReceiveChar(port, comstat);
break;
} // case 1
} // end switch
// we're exiting, break out of loop and end thread
if (port->m_bThreadAlive == FALSE)
break;
} // close forever loop
// when we get here, the thread is about to shutdown and exit.
TRACE(_T("Com Port %d thread about to die.\n"), port->m_nPortNr);
return 0;
}
// Start comm watching
BOOL CSerialPort::StartMonitoring()
{
if (m_bInitDone == false) {
TRACE(_T("Com Port %d cannot be started. Init did not complete.\n"), m_nPortNr);
return FALSE;
}
TRACE(_T("Com Port %d starting monitoring.\n"), m_nPortNr);
m_Thread = AfxBeginThread(CommThread, this, THREAD_PRIORITY_NORMAL);
if (m_Thread == NULL) {
return FALSE;
}
// Clear buffer
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
TRACE(_T("Thread %ld started\n"), (DWORD)m_Thread->m_nThreadID);
return TRUE;
}
// Restart the comm thread
BOOL CSerialPort::RestartMonitoring()
{
if (m_bInitDone == false) {
TRACE(_T("Com Port %d cannot be restarted. Init did not complete.\n"), m_nPortNr);
return FALSE;
}
TRACE(_T("Com Port %d re-starting monitoring.\n"), m_nPortNr);
// Clear buffer
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
if(m_Thread != NULL)
m_Thread->ResumeThread();
else
StartMonitoring();
return TRUE;
}
// Suspend the comm thread
BOOL CSerialPort::StopMonitoring()
{
if(m_Thread != NULL)
{
TRACE(_T("Com Port %d stopped monitoring.\n"), m_nPortNr);
m_Thread->SuspendThread();
}
// Clear buffer
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
return TRUE;
}
//
// If there is a error, give the right message
void CSerialPort::ProcessErrorMessage(char* ErrorText)
{
char *Temp = new char[200];
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
sprintf_s(Temp, 200, _T("WARNING: %s Failed with the following error: \n%s\nPort: %d\n"),
(char*)ErrorText, (char*)lpMsgBuf, m_nPortNr);
MessageBox(NULL, Temp, _T("Application Error"), MB_ICONSTOP);
LocalFree(lpMsgBuf);
delete[] Temp;
}
// Write a character.
void CSerialPort::WriteChar(CSerialPort* port)
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;
DWORD BytesSent = 0;
// Gain ownership of the critical section
EnterCriticalSection(&port->m_csCommunicationSync);
if (bWrite)
{
// Initialise variables
port->m_ov.Offset = 0;
port->m_ov.OffsetHigh = 0;
// Clear buffer
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
bResult = WriteFile(port->m_hComm, // Handle to COM Port
port->m_szWriteBuffer, // Pointer to message buffer in calling function
port->m_nActualWriteBufferSize,
&BytesSent, // Where to store the number of bytes sent
&port->m_ov); // Overlapped structure
// deal with any error codes
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
port->ProcessErrorMessage(_T("WriteFile()"));
AfxThrowSerialException();
}
}
}
else
{
LeaveCriticalSection(&port->m_csCommunicationSync);
}
} // end if(bWrite)
// If we have not written all the bytes out, we call GetOverlappedResult(..) to
// wait for the pending writes to happen.
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, //Overlapped structure
&BytesSent, //Stores number of bytes sent
TRUE); //Wait flag
LeaveCriticalSection(&port->m_csCommunicationSync);
// deal with the error code
if (!bResult)
{
port->ProcessErrorMessage(_T("GetOverlappedResults() in WriteFile()"));
AfxThrowSerialException();
}
} // end if (!bWrite)
// Verify that the data size sent equals what we tried to send
if (BytesSent != (DWORD)port->m_nActualWriteBufferSize)
{
TRACE(_T("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n"),
BytesSent, port->m_nActualWriteBufferSize);
}
}
// Character received. Inform the owner
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
DWORD BytesToRead = 1; //modified
unsigned char RXBuff[MAXBYTESTOREAD];//modified
for (;;)
{
// Gain ownership of the comm port critical section.
// This process guarantees no other part of this program
// is using the port object.
EnterCriticalSection(&port->m_csCommunicationSync);
// ClearCommError() will update the COMSTAT structure and
// clear any other errors.
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
BytesToRead = min(MAXBYTESTOREAD, comstat.cbInQue);
LeaveCriticalSection(&port->m_csCommunicationSync);
if (!bResult)
AfxThrowSerialException();
// start forever loop. I use this type of loop because I
// do not know at runtime how many loops this will have to
// run. My solution is to start a forever loop and to
// break out of it when I have processed all of the
// data available. Be careful with this approach and
// be sure your loop will exit.
// My reasons for this are not as clear in this sample
// as it is in my production code, but I have found this
// solution to be the most efficient way to do this.
if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}
EnterCriticalSection(&port->m_csCommunicationSync);
if (bRead)
{
bResult = ReadFile(port->m_hComm, // Handle to COMM port
&RXBuff, // RX Buffer Pointer
BytesToRead, // byte to read depends on que
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
bRead = FALSE;
break;
}
default:
{
// Another error has occured. Process this error.
port->ProcessErrorMessage("ReadFile()");
AfxThrowSerialException();
break;
}
}
}
else
{
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead)
if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag
// deal with the error code
if (!bResult)
{
port->ProcessErrorMessage(_T("GetOverlappedResults() in ReadFile()"));
AfxThrowSerialException();
}
} // close if (!bRead)
LeaveCriticalSection(&port->m_csCommunicationSync);
// notify parent that a byte was received
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) BytesRead);
} // end forever loop
}
// *************************************************************************************************
// Public Functions
//
// Write a string to the port - This can write even NULL characters
void CSerialPort::WriteToPort(unsigned char* string, int stringlength, BOOL bDelay)
{
DWORD messageLength = max(stringlength, 0);
DWORD halfDelay = max(5, m_nWriteDelay / 2);
assert(m_hComm != 0);
if (messageLength > m_nWriteBufferSize) {
messageLength = m_nWriteBufferSize;
}
::EnterCriticalSection(&m_csCommunicationSync);
memcpy(m_szWriteBuffer, string, messageLength);
if (messageLength < m_nWriteBufferSize) {
memset(m_szWriteBuffer + messageLength, 0, m_nWriteBufferSize - messageLength);
}
m_nActualWriteBufferSize=messageLength;
::LeaveCriticalSection(&m_csCommunicationSync);
if (bDelay) { // sleep to reduce stress on ECU serial port.
struct timeb currentTime = { 0 };
ftime(¤tTime);
time_t timeSinceLastWrite = ((currentTime.time - m_timeLastWrittenToPort.time) * 1000) + (currentTime.millitm - m_timeLastWrittenToPort.millitm);
if (timeSinceLastWrite < 0) {
// overflow...
Sleep(halfDelay);
}
else if (timeSinceLastWrite < halfDelay) {
Sleep((DWORD) (halfDelay - timeSinceLastWrite));
}
}
WriteChar(this); // Write to port immediately
if (bDelay) { // sleep to reduce stress on ECU serial port.
Sleep(halfDelay);
}
ftime(&m_timeLastWrittenToPort);
}
// Returns the device control block
DCB CSerialPort::GetDCB()
{
return m_dcb;
}
// Returns the communication event masks
DWORD CSerialPort::GetCommEvents()
{
return m_dwCommEvents;
}
// Returns the output buffer size
DWORD CSerialPort::GetWriteBufferSize()
{
return m_nWriteBufferSize;
}
// Returns the current port number
UINT CSerialPort::GetPort(void)
{
return m_nPortNr;
}
// Sets the internal com port number
void CSerialPort::SetPort(UINT nPort)
{
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(_T("Communications"), _T("Port"), nPort);
TRACE(_T(_T("Comms port number changed\n")));
}
// Returns the minimum number of milliseconds the TX routine will wait before returning
DWORD CSerialPort::GetWriteDelay(void)
{
return m_nWriteDelay;
}
// Sets the minimum number of milliseconds the TX routine will wait before returning
void CSerialPort::SetWriteDelay(DWORD nDelay)
{
m_nWriteDelay = nDelay;
// Write this to the registry
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(_T("Communications"), _T("WriteDelay"), m_nWriteDelay);
TRACE(_T("Comms port write delay changed\n"));
}