#pragma once
|
class HvSerialPort
|
{
|
public:
|
double GetTimemS()
|
{
|
LARGE_INTEGER perfreq;
|
LARGE_INTEGER percounter1;
|
QueryPerformanceFrequency(&perfreq);
|
QueryPerformanceCounter(&percounter1);
|
|
double time1 = (double)percounter1.QuadPart / perfreq.QuadPart;
|
return (time1 * 1000);
|
};
|
public:
|
enum Results {
|
R_ERR = -1,
|
R_OK = 0 ,
|
R_ERR2 =1,
|
};
|
HANDLE hCom1;
|
bool m_bOpened;
|
int m_nPort;
|
int m_nBaudRate;
|
CString m_Settings;
|
CCriticalSection m_CS1;
|
CCriticalSection m_CS2;
|
|
volatile bool MyThreadProc1ToRun;
|
volatile bool MyThreadProc1Running;
|
|
volatile int RecvBufDataLen = 0;
|
unsigned char * RecvBuf[2048];
|
|
DWORD m_dwError;
|
CString m_strResult;
|
int m_nCountToTry = 5;
|
int m_nCountToWait = 0;
|
|
volatile DWORD TotalSendBytes, TotalRecvBytes;
|
volatile DWORD SendBytes, RecvBytes;
|
volatile DWORD LastSendBytes, LastRecvBytes;
|
volatile DWORD SendSpeed, RecvSpeed;
|
volatile double LastTime;
|
|
typedef int(*pRecvDone)(void *, int); //RecvDone CallBack func
|
int m_bRecvDoneCBSet = 0;
|
typedef int(*pEventCB)(int ,void *); //Event CallBack func
|
int m_bEventCBSet = 0;
|
|
pRecvDone RecvDoneCB =NULL;
|
pEventCB EventCB=NULL;
|
|
HvSerialPort(void)
|
{
|
this->hCom1 = INVALID_HANDLE_VALUE;
|
this->m_bOpened = 0;
|
|
this->m_nPort = 0;
|
this->m_nBaudRate = 0;
|
this->m_Settings.Empty();
|
|
this->m_dwError = 0;
|
};
|
~HvSerialPort()
|
{
|
MyThreadProc1ToRun = 0;
|
if (this->m_bOpened) { this->Close(); }
|
this->hCom1 = INVALID_HANDLE_VALUE;
|
|
};
|
public:
|
int Init();
|
|
public:
|
int SetParams(int nPortNum, int BaudRate, CString Settings);
|
int PurgeComBuf()
|
{
|
return PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXCLEAR);
|
}
|
|
int Open();
|
int ClearStatData();
|
int CalSpeed();
|
|
int Close();
|
int SetRecvDoneCallBack(pRecvDone);
|
|
int Send(void * pBuf, int len1);
|
int Recv(void * pBuf, int len1);
|
|
static UINT MyJumper1(LPVOID pParam);
|
DWORD WINAPI MyThreadProc1(LPVOID pParam);
|
|
};
|
|