forked from sanwer/CSerialPort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.cpp
116 lines (104 loc) · 2.77 KB
/
Example.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
#include <windows.h>
#include "SerialPort.h"
class CMainDlg : public WindowImplBase, public ISerialPortSink
{
public:
void OpenSP1();
protected:
void OnCommEvent(UINT nPort,UINT nMsg,PVOID pParam,UINT nLen);
}
void CMainDlg::OpenSP1()
{
CDuiString sBuffer = ctrlSP1ComId->GetText();
sBuffer.Replace(_T("COM"),_T(""));
m_nSP1ComId = _tstoi(sBuffer.GetData());
if(m_nSP1ComId< 0 || m_nSP1ComId > 20)
m_nSP1ComId = 1;
sBuffer = ctrlSP1Speed->GetText();
m_nSP1Baud = _tstoi(sBuffer.GetData());
if(m_nSP1Baud< 75 || m_nSP1Baud > 128000)
m_nSP1Baud = 9600;
sBuffer = ctrlSP1Parity->GetText();
if(sBuffer.CompareNoCase(_T("ODD"))==0)
m_cSP1Parity = 'O';
else if(sBuffer.CompareNoCase(_T("EVEN"))==0)
m_cSP1Parity = 'E';
else if(sBuffer.CompareNoCase(_T("MARK"))==0)
m_cSP1Parity = 'M';
else if(sBuffer.CompareNoCase(_T("SPACE"))==0)
m_cSP1Parity = 'S';
else
m_cSP1Parity='N';
sBuffer = ctrlSP1Databits->GetText();
m_nSP1Databits = _tstoi(sBuffer.GetData());
if(m_nSP1Databits< 4 || m_nSP1Databits > 8)
m_nSP1Databits = 8;
sBuffer = ctrlSP1Stopbits->GetText();
if(sBuffer.CompareNoCase(_T("1"))==0)
m_nSP1Stopbits = ONESTOPBIT;
else if(sBuffer.CompareNoCase(_T("2"))==0)
m_nSP1Stopbits = TWOSTOPBITS;
else
m_nSP1Stopbits = ONE5STOPBITS;
sBuffer = ctrlSP1Command->GetText();
m_nSP1CommandLen = _tstoi(sBuffer.GetData());
if(m_nSP1CommandLen< 1 || m_nSP1CommandLen > 16)
m_nSP1CommandLen = 16;
if (m_SP1Port.OpenPort(this,m_nSP1ComId,
m_nSP1Baud,m_cSP1Parity,m_nSP1Databits,
m_nSP1Stopbits,m_nSP1Events,512))
{
m_bSP1Open=TRUE;
m_bSP1HasSent = true;
m_nSP1Send = 0;
m_nSP1Received = 0;
btnSP1Open->SetText(_T("Close"));
SP1Log(_T("COM%d Opened"),m_nSP1ComId);
}
else
{
SP1Log(_T("COM%d Open Failed"),m_nSP1ComId);
}
}
void CMainDlg::OnCommEvent(UINT nPort,UINT nMsg,PVOID pParam,UINT nLen)
{
switch (nMsg)
{
case COMM_RXCHAR:
{
CDuiString sBuffer,sItem;
PBYTE lpBuf =(PBYTE)pParam;
if(nPort == m_nSP1ComId){
m_SP1RecvLock.Lock();
UINT nReceivedLen=0,nRecvLen=0;
while(nReceivedLen<nLen)
{
nRecvLen = nLen-nReceivedLen;
if(nRecvLen >= m_nSP1CommandLen-m_nSP1Received)
nRecvLen = m_nSP1CommandLen-m_nSP1Received;
memcpy(m_szSP1Recv+m_nSP1Received,lpBuf+nReceivedLen,nRecvLen);
nReceivedLen += nRecvLen;
m_nSP1Received += nRecvLen;
if(m_nSP1Received == m_nSP1CommandLen)
{
SP1Recv(m_szSP1Recv);
m_nSP1Received=0;
}
}
m_SP1RecvLock.Unlock();
}
break;
}
case COMM_TXEMPTY_DETECTED:
{
if(nPort == m_nSP1ComId){
m_SP1SendLock.Lock();
m_bSP1HasSent = true;
m_SP1SendLock.Unlock();
}
break;
}
default:
break;
}
}