提交 | 用户 | age
|
0ed438
|
1 |
#include "pch.h"
|
Q |
2 |
#include "HvSerialPort.h"
|
|
3 |
|
|
4 |
|
|
5 |
int HvSerialPort::Init()
|
|
6 |
{
|
|
7 |
return R_OK;
|
|
8 |
}
|
|
9 |
|
|
10 |
int HvSerialPort::SetParams(int nPortNum, int BaudRate, CString Settings)
|
|
11 |
{
|
|
12 |
m_nPort = nPortNum;
|
|
13 |
m_nBaudRate = BaudRate;
|
|
14 |
m_Settings = Settings;
|
|
15 |
return R_OK;
|
|
16 |
}
|
|
17 |
|
|
18 |
int HvSerialPort::Open()
|
|
19 |
{
|
|
20 |
// TODO: 在此处添加实现代码.
|
|
21 |
//Open Port
|
|
22 |
int j;
|
|
23 |
CString s1, s2;
|
|
24 |
CString ComPortName;
|
|
25 |
if (this->m_nPort == 0)
|
|
26 |
{
|
|
27 |
m_strResult.Format(_T("COM not specified %d"),m_nPort);
|
|
28 |
return R_ERR;
|
|
29 |
}
|
|
30 |
if (this->m_bOpened)
|
|
31 |
{
|
|
32 |
m_strResult.Format(_T("COM%d already Opened"),m_nPort);
|
|
33 |
return R_ERR;
|
|
34 |
}
|
|
35 |
ComPortName.Format(_T("\\\\.\\COM%d"), this->m_nPort);
|
|
36 |
hCom1 = CreateFile(ComPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED
|
|
37 |
if (hCom1 == INVALID_HANDLE_VALUE)
|
|
38 |
{
|
|
39 |
m_dwError = GetLastError();
|
|
40 |
m_bOpened = false;
|
|
41 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, m_dwError, 0, s1.GetBuffer(2048), 2048, NULL);
|
|
42 |
s1.ReleaseBuffer();
|
|
43 |
|
|
44 |
m_strResult.Format(_T("Open COM%d Err %d ,%s "), this->m_nPort, this->m_dwError, s1);
|
|
45 |
return R_ERR;
|
|
46 |
}
|
|
47 |
else
|
|
48 |
{
|
|
49 |
m_dwError = GetLastError();
|
|
50 |
|
|
51 |
m_strResult.Format(_T("COM%d OK."), this->m_nPort);
|
|
52 |
LastTime = GetTimeMs();
|
|
53 |
TotalSendBytes = 0; TotalRecvBytes = 0;
|
|
54 |
LastSendBytes = 0; SendBytes = 0; RecvBytes = 0;
|
|
55 |
LastRecvBytes = 0; SendSpeed = 0; RecvSpeed = 0;
|
|
56 |
}
|
|
57 |
|
|
58 |
j = SetupComm(hCom1, 256, 256);
|
|
59 |
|
|
60 |
DCB dcb1;
|
|
61 |
j = GetCommState(hCom1, &dcb1);
|
|
62 |
BuildCommDCB(m_Settings, &dcb1);
|
|
63 |
|
|
64 |
dcb1.BaudRate = m_nBaudRate;
|
|
65 |
|
|
66 |
if (m_Settings.Find(_T("8")) != -1) { dcb1.ByteSize = 8; }
|
|
67 |
else if (m_Settings.Find(_T("7")) != -1) { dcb1.ByteSize = 8; }
|
|
68 |
else if (m_Settings.Find(_T("6")) != -1) { dcb1.ByteSize = 8; }
|
|
69 |
else if (m_Settings.Find(_T("5")) != -1) { dcb1.ByteSize = 8; }
|
|
70 |
|
|
71 |
if (m_Settings.Find(_T("N")) != -1) { dcb1.fParity = FALSE; dcb1.Parity = NOPARITY; }
|
|
72 |
else if (m_Settings.Find(_T("O")) != -1) { dcb1.fParity = TRUE; dcb1.Parity = ODDPARITY; }
|
|
73 |
else if (m_Settings.Find(_T("E")) != -1) { dcb1.fParity = TRUE; dcb1.Parity = EVENPARITY; }
|
|
74 |
else if (m_Settings.Find(_T("M")) != -1) { dcb1.fParity = TRUE; dcb1.Parity = MARKPARITY; }
|
|
75 |
else if (m_Settings.Find(_T("S")) != -1) { dcb1.fParity = TRUE; dcb1.Parity = SPACEPARITY; }
|
|
76 |
|
|
77 |
if (m_Settings.Find(_T("1.5")) != -1) { dcb1.StopBits = ONE5STOPBITS; }
|
|
78 |
else if (m_Settings.Find(_T("1")) != -1) { dcb1.StopBits = ONESTOPBIT; }
|
|
79 |
else if (m_Settings.Find(_T("2")) != -1) { dcb1.StopBits = TWOSTOPBITS; }
|
|
80 |
|
|
81 |
dcb1.fDtrControl = 0;
|
|
82 |
dcb1.fDsrSensitivity = false;
|
|
83 |
dcb1.fBinary = 1;
|
|
84 |
dcb1.fOutxCtsFlow = false;
|
|
85 |
dcb1.fOutxDsrFlow = false;
|
|
86 |
dcb1.fRtsControl = RTS_CONTROL_DISABLE;
|
|
87 |
|
|
88 |
j = SetCommState(hCom1, &dcb1);
|
|
89 |
if (j == 0)
|
|
90 |
{
|
|
91 |
m_strResult.Format(_T("SetCommState Fail %d %s \r\n"), m_nBaudRate, m_Settings);
|
|
92 |
CloseHandle(hCom1);
|
|
93 |
m_bOpened = false;
|
|
94 |
return R_ERR;
|
|
95 |
}
|
|
96 |
j = GetCommState(hCom1, &dcb1);
|
|
97 |
|
|
98 |
CString strParity;
|
|
99 |
CString strStopBit;
|
|
100 |
|
|
101 |
if (dcb1.Parity == NOPARITY) { strParity = _T("N"); }
|
|
102 |
else if (dcb1.Parity == EVENPARITY) { strParity = _T("E"); }
|
|
103 |
else if (dcb1.Parity == ODDPARITY) { strParity = _T("O"); }
|
|
104 |
else if (dcb1.Parity == MARKPARITY) { strParity = _T("M"); }
|
|
105 |
else if (dcb1.Parity == SPACEPARITY) { strParity = _T("S"); }
|
|
106 |
else { strParity = _T("X"); }
|
|
107 |
|
|
108 |
if (dcb1.StopBits == ONESTOPBIT) { strStopBit = _T("1"); }
|
|
109 |
else if (dcb1.StopBits == ONE5STOPBITS) { strStopBit = _T("1.5"); }
|
|
110 |
else if (dcb1.StopBits == TWOSTOPBITS) { strStopBit = _T("2"); }
|
|
111 |
else { strStopBit = _T("3"); }
|
|
112 |
|
|
113 |
s1.Format(_T("%d %d-%s-%s"), dcb1.BaudRate, dcb1.ByteSize, strParity, strStopBit);
|
|
114 |
m_strResult += s1;
|
|
115 |
|
|
116 |
COMMTIMEOUTS comtimeout1;
|
|
117 |
COMSTAT comstat1;
|
|
118 |
GetCommTimeouts(hCom1, &comtimeout1);
|
|
119 |
|
|
120 |
comtimeout1.ReadIntervalTimeout = 1; //两个字符之间的时间
|
|
121 |
comtimeout1.ReadTotalTimeoutMultiplier = 0; //每个字符等待的时间,读多个字符则等待时间为单个时间乘以字符个数
|
|
122 |
comtimeout1.ReadTotalTimeoutConstant = 1; //读取时,再另外多等待的时间。
|
|
123 |
comtimeout1.WriteTotalTimeoutMultiplier = 0; //写入时,每个字符等待的时间,写入多个字符则等待时间为单个时间乘以字符个数。
|
|
124 |
comtimeout1.WriteTotalTimeoutConstant = 0; //写入时,再另外多等待的时间。
|
|
125 |
|
|
126 |
SetCommTimeouts(hCom1, &comtimeout1);
|
|
127 |
j = ClearCommError(hCom1, &m_dwError, &comstat1);
|
|
128 |
s1.Format(_T("ClearCommError j %d errors %d "), j, m_dwError);
|
|
129 |
s2 = s1;
|
|
130 |
s1.Format(_T("cbInQue %d "), comstat1.cbInQue);
|
|
131 |
s2 += s1;
|
|
132 |
s1.Format(_T("cbOutQue %d "), comstat1.cbOutQue);
|
|
133 |
s2 += s1;
|
|
134 |
PurgeComBuf();
|
|
135 |
m_bOpened = true;
|
|
136 |
|
|
137 |
//Start Read Thread
|
|
138 |
|
|
139 |
MyThreadProc1ToRun = 1;
|
|
140 |
AfxBeginThread(MyJumper1, (LPVOID)this);
|
|
141 |
|
|
142 |
return R_OK;
|
|
143 |
}
|
|
144 |
|
|
145 |
int HvSerialPort::ClearStatData()
|
|
146 |
{
|
|
147 |
LastSendBytes = 0;
|
|
148 |
LastRecvBytes = 0;
|
|
149 |
SendBytes = 0;
|
|
150 |
RecvBytes = 0;
|
|
151 |
SendSpeed = 0;
|
|
152 |
RecvSpeed = 0;
|
|
153 |
return R_OK;
|
|
154 |
}
|
|
155 |
|
|
156 |
int HvSerialPort::CalSpeed()
|
|
157 |
{
|
|
158 |
double thistime = GetTimeMs();
|
|
159 |
double diftime = thistime - LastTime;
|
|
160 |
if (diftime >= 500)
|
|
161 |
{
|
|
162 |
SendSpeed = (DWORD)(SendBytes * 1000 / diftime);
|
|
163 |
RecvSpeed = static_cast<DWORD> (RecvBytes * 1000 / diftime);
|
|
164 |
LastSendBytes = SendBytes; SendBytes = 0;
|
|
165 |
LastRecvBytes = RecvBytes; RecvBytes = 0;
|
|
166 |
LastTime = thistime;
|
|
167 |
}
|
|
168 |
return 0;
|
|
169 |
}
|
|
170 |
|
|
171 |
int HvSerialPort::Close()
|
|
172 |
{
|
|
173 |
// TODO: 在此处添加实现代码.
|
|
174 |
if (m_bOpened)
|
|
175 |
{
|
|
176 |
// SendSign(STOP);
|
|
177 |
MyThreadProc1ToRun = 0;
|
|
178 |
// wait for Thread stop
|
|
179 |
//WaitForMultipleObjects(nCount);
|
|
180 |
// close port
|
|
181 |
CloseHandle(hCom1);
|
|
182 |
m_bOpened = 0;
|
|
183 |
hCom1 = INVALID_HANDLE_VALUE;
|
|
184 |
m_strResult.Format(_T("COM%d Closed"), m_nPort);
|
|
185 |
return R_OK;
|
|
186 |
}
|
|
187 |
else
|
|
188 |
{
|
|
189 |
m_strResult.Format(_T("COM%d not Opened"),m_nPort);
|
|
190 |
return R_ERR;
|
|
191 |
}
|
|
192 |
}
|
|
193 |
|
|
194 |
int HvSerialPort::SetRecvDoneCallBack(pRecvDone pRecvDoneFunc)
|
|
195 |
{
|
|
196 |
// TODO: 在此处添加实现代码.
|
|
197 |
if (pRecvDoneFunc != NULL)
|
|
198 |
{
|
|
199 |
RecvDoneCB = pRecvDoneFunc;
|
|
200 |
m_bRecvDoneCBSet = 1;
|
|
201 |
}
|
|
202 |
else
|
|
203 |
{
|
|
204 |
RecvDoneCB = NULL;
|
|
205 |
m_bRecvDoneCBSet = 0;
|
|
206 |
}
|
|
207 |
return R_OK;
|
|
208 |
}
|
|
209 |
|
|
210 |
UINT HvSerialPort::MyJumper1(LPVOID pParam) //线程跳板
|
|
211 |
{
|
|
212 |
HvSerialPort *pInput = (HvSerialPort *)pParam; //反跳到真正的执行程序
|
|
213 |
return pInput->MyThreadProc1(0);
|
|
214 |
}
|
|
215 |
|
|
216 |
DWORD WINAPI HvSerialPort::MyThreadProc1(LPVOID pParam)
|
|
217 |
{
|
|
218 |
//串口接收数据函数
|
|
219 |
|
|
220 |
//接收到的数据 通知方式
|
|
221 |
// 1. windows消息队列
|
|
222 |
// 2. callback 函数 // 不同线程
|
|
223 |
// 3. Event
|
|
224 |
// 4. Buffer
|
|
225 |
|
|
226 |
OVERLAPPED osRead;
|
|
227 |
HANDLE hEventArr[2];
|
|
228 |
memset(&osRead, 0, sizeof(OVERLAPPED));
|
|
229 |
osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
230 |
SetCommMask(hCom1, EV_RXCHAR| EV_TXEMPTY);
|
|
231 |
|
|
232 |
hEventArr[0] = osRead.hEvent;
|
|
233 |
// hEventArr[1] = *g_OutPutList.GetEvent();
|
|
234 |
|
|
235 |
CString s1, s2;
|
|
236 |
// SysLog(_T("线程开始运行\r\n"));
|
|
237 |
MyThreadProc1Running = 1;
|
|
238 |
|
|
239 |
/*
|
|
240 |
static OVERLAPPED ovlap1, ovlap2;
|
|
241 |
*/
|
|
242 |
|
|
243 |
//SysLog(_T("准备接收\r\n"));
|
|
244 |
DWORD dNumtoRead = 1000;
|
|
245 |
dNumtoRead = 64;
|
|
246 |
//nDataToSend = 0;
|
|
247 |
DWORD wCount2 = 0;
|
|
248 |
DWORD dwSent = 0;
|
|
249 |
int nRunCount = 0;
|
|
250 |
//计算接收速度;
|
|
251 |
|
|
252 |
int LastSpeedTime = (int)GetTimeMs();
|
|
253 |
int nThisRecv = 0;
|
|
254 |
|
|
255 |
COMSTAT cs;
|
|
256 |
OVERLAPPED os;
|
|
257 |
DWORD dwTrans;
|
|
258 |
DWORD dwReaded;
|
|
259 |
for (; MyThreadProc1ToRun == 1;)
|
|
260 |
{
|
|
261 |
|
|
262 |
DWORD dwEvtMask = 0;
|
|
263 |
/*
|
|
264 |
BOOL result = WaitCommEvent(hCom1, &dwEvtMask, &os);
|
|
265 |
if (!result)
|
|
266 |
{
|
|
267 |
if (GetLastError() == ERROR_IO_PENDING)
|
|
268 |
{
|
|
269 |
GetOverlappedResult(hCom1, &os, &dwTrans, true);
|
|
270 |
if (dwEvtMask & EV_RXCHAR)
|
|
271 |
{
|
|
272 |
ClearCommError(hCom1, &m_dwError, &cs);
|
|
273 |
int bytes = cs.cbInQue;
|
|
274 |
dwReaded = 0;
|
|
275 |
ReadFile(hCom1,RecvBuf,bytes,&dwReaded,NULL);
|
|
276 |
}
|
|
277 |
}
|
|
278 |
}
|
|
279 |
//EscapeCommFunction(hCom1, SETDTR);
|
|
280 |
//*/
|
|
281 |
Sleep(1);
|
|
282 |
}
|
|
283 |
//SysLog(_T("跳出接收循环\r\n"));
|
|
284 |
// hcurDC.SelectObject(def_font);
|
|
285 |
// CloseHandle(hCom);
|
|
286 |
//SysLog(_T("线程结束运行\r\n\r\n"));
|
|
287 |
MyThreadProc1Running = 0;
|
|
288 |
return 0;
|
|
289 |
}
|
|
290 |
|
|
291 |
|
|
292 |
int HvSerialPort::Send(void * pBuf, int len1)
|
|
293 |
{
|
|
294 |
// TODO: 在此处添加实现代码.
|
|
295 |
CString s1;
|
|
296 |
int i, j;
|
|
297 |
DWORD numsent;
|
|
298 |
|
|
299 |
if (!m_bOpened) { return R_ERR; }
|
|
300 |
|
|
301 |
j = WriteFile(hCom1, pBuf, len1, &numsent, NULL);
|
|
302 |
|
|
303 |
if (j != 0)
|
|
304 |
{
|
|
305 |
m_strResult.Format(_T("Send To COM%d OK %dB : "), this->m_nPort, numsent);
|
|
306 |
for (i = 0; (unsigned)i < numsent; i++)
|
|
307 |
{
|
|
308 |
m_strResult.AppendFormat(_T("%c"), ((char *)pBuf)[i]);
|
|
309 |
}
|
|
310 |
m_strResult.AppendFormat(_T("\r\n"));
|
|
311 |
}
|
|
312 |
else
|
|
313 |
{
|
|
314 |
m_dwError = GetLastError();
|
|
315 |
m_strResult.Format(_T("Send To COM%d fail. Error:%d SentBytes:%d "), this->m_nPort, m_dwError, numsent);
|
|
316 |
}
|
|
317 |
SendBytes += numsent;
|
|
318 |
TotalSendBytes += numsent;
|
|
319 |
return numsent;
|
|
320 |
|
|
321 |
return R_OK;
|
|
322 |
}
|
|
323 |
|
|
324 |
|
|
325 |
int HvSerialPort::Recv(void * pBuf, int len1)
|
|
326 |
{
|
|
327 |
// TODO: 在此处添加实现代码.
|
|
328 |
CString s1;
|
|
329 |
int i, j;
|
|
330 |
DWORD numreaded;
|
|
331 |
if (!m_bOpened) { return R_ERR; }
|
|
332 |
int len2 = 0;
|
|
333 |
//Sleep(n/10);
|
|
334 |
int nTryCount2 = 0;
|
|
335 |
m_strResult.Format(_T("ToRead %d "), len1);
|
|
336 |
for (int i = 0; i < m_nCountToTry; i++)
|
|
337 |
{
|
|
338 |
numreaded = 0;
|
|
339 |
|
|
340 |
j = ReadFile(hCom1, (char *)pBuf + len2, len1 - len2, &numreaded, NULL);
|
|
341 |
if (j != 0) {
|
|
342 |
len2 += numreaded;
|
|
343 |
((char *)pBuf)[len2] = 0;
|
|
344 |
if (len2 >= len1) break;
|
|
345 |
nTryCount2 = 0;
|
|
346 |
}
|
|
347 |
else {
|
|
348 |
if (len2 > 0) {
|
|
349 |
nTryCount2++;
|
|
350 |
if (nTryCount2 >= m_nCountToWait) break;
|
|
351 |
}
|
|
352 |
}
|
|
353 |
if (len2 > 0 && nTryCount2 >= m_nCountToWait) break;
|
|
354 |
}
|
|
355 |
numreaded = len2;
|
|
356 |
if (numreaded > 0) {
|
|
357 |
m_strResult.AppendFormat(_T("Read From COM%d OK %dB "), this->m_nPort, numreaded);
|
|
358 |
for (i = 0; (unsigned)i < numreaded; i++)
|
|
359 |
{
|
|
360 |
m_strResult.AppendFormat(_T("%c"), ((char *)pBuf)[i]);
|
|
361 |
}
|
|
362 |
m_strResult.AppendFormat(_T("\r\n"));
|
|
363 |
}
|
|
364 |
else {
|
|
365 |
m_strResult.Format(_T("Read From COM failed \r\n"));
|
|
366 |
}
|
|
367 |
|
|
368 |
RecvBytes += numreaded;
|
|
369 |
TotalRecvBytes += numreaded;
|
|
370 |
|
|
371 |
return numreaded;
|
|
372 |
return R_OK;
|
|
373 |
}
|