提交 | 用户 | age
|
418cb3
|
1 |
#pragma once
|
Q |
2 |
class HvSerialPort
|
|
3 |
{
|
|
4 |
public:
|
|
5 |
double GetTimemS()
|
|
6 |
{
|
|
7 |
LARGE_INTEGER perfreq;
|
|
8 |
LARGE_INTEGER percounter1;
|
|
9 |
QueryPerformanceFrequency(&perfreq);
|
|
10 |
QueryPerformanceCounter(&percounter1);
|
|
11 |
|
|
12 |
double time1 = (double)percounter1.QuadPart / perfreq.QuadPart;
|
|
13 |
return (time1 * 1000);
|
|
14 |
};
|
|
15 |
public:
|
|
16 |
enum Results {
|
|
17 |
R_ERR = -1,
|
|
18 |
R_OK = 0 ,
|
|
19 |
R_ERR2 =1,
|
|
20 |
};
|
|
21 |
HANDLE hCom1;
|
|
22 |
bool m_bOpened;
|
|
23 |
int m_nPort;
|
|
24 |
int m_nBaudRate;
|
|
25 |
CString m_Settings;
|
|
26 |
CCriticalSection m_CS1;
|
|
27 |
CCriticalSection m_CS2;
|
|
28 |
|
|
29 |
volatile bool MyThreadProc1ToRun;
|
|
30 |
volatile bool MyThreadProc1Running;
|
|
31 |
|
|
32 |
volatile int RecvBufDataLen = 0;
|
|
33 |
unsigned char * RecvBuf[4096];
|
|
34 |
volatile int RecvBufPos = 0;
|
|
35 |
|
|
36 |
unsigned char * RecvBuf2[4096];
|
|
37 |
DWORD m_dwError;
|
|
38 |
CString m_strResult;
|
|
39 |
int m_nCountToTry = 5;
|
|
40 |
int m_nCountToWait = 1;
|
|
41 |
|
|
42 |
volatile DWORD TotalSendBytes, TotalRecvBytes;
|
|
43 |
volatile DWORD SendBytes, RecvBytes;
|
|
44 |
volatile DWORD LastSendBytes, LastRecvBytes;
|
|
45 |
volatile DWORD SendSpeed, RecvSpeed;
|
|
46 |
volatile double LastTime;
|
|
47 |
|
|
48 |
typedef int(*pRecvDone)(void *, int); //RecvDone CallBack func
|
|
49 |
int m_bRecvDoneCBSet = 0;
|
|
50 |
typedef int(*pEventCB)(int ,void *); //Event CallBack func
|
|
51 |
int m_bEventCBSet = 0;
|
|
52 |
|
|
53 |
pRecvDone RecvDoneCB =NULL;
|
|
54 |
pEventCB EventCB=NULL;
|
|
55 |
|
|
56 |
HvSerialPort(void)
|
|
57 |
{
|
|
58 |
this->hCom1 = INVALID_HANDLE_VALUE;
|
|
59 |
this->m_bOpened = 0;
|
|
60 |
|
|
61 |
this->m_nPort = 0;
|
|
62 |
this->m_nBaudRate = 0;
|
|
63 |
this->m_Settings.Empty();
|
|
64 |
|
|
65 |
this->m_dwError = 0;
|
|
66 |
};
|
|
67 |
~HvSerialPort()
|
|
68 |
{
|
|
69 |
MyThreadProc1ToRun = 0;
|
|
70 |
if (this->m_bOpened) { this->Close(); }
|
|
71 |
this->hCom1 = INVALID_HANDLE_VALUE;
|
|
72 |
|
|
73 |
};
|
|
74 |
public:
|
|
75 |
int Init();
|
|
76 |
|
|
77 |
public:
|
|
78 |
int SetParams(int nPortNum, int BaudRate, CString Settings);
|
|
79 |
|
|
80 |
int Open();
|
|
81 |
int ClearStatData();
|
|
82 |
int CalSpeed();
|
|
83 |
|
|
84 |
int Close();
|
|
85 |
int SetRecvDoneCallBack(pRecvDone);
|
|
86 |
|
|
87 |
int ClearBuf();
|
|
88 |
|
|
89 |
int Send(void * pBuf, int len1);
|
|
90 |
int Recv(void * pBuf, int len1);
|
|
91 |
int RecvFromCom(void * pBuf, int len1);
|
|
92 |
int RecvFromBuf(void * pBuf, int len1);
|
|
93 |
|
|
94 |
protected:
|
|
95 |
int PurgeComBuf()
|
|
96 |
{
|
|
97 |
return PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXCLEAR);
|
|
98 |
}
|
|
99 |
|
|
100 |
|
|
101 |
static UINT MyJumper1(LPVOID pParam);
|
|
102 |
DWORD WINAPI MyThreadProc1(LPVOID pParam);
|
|
103 |
|
|
104 |
};
|
|
105 |
|