#pragma once #ifdef KLINK_EXPORTS #define KLINK1_API __declspec(dllexport) #else #define KLINK1_API __declspec(dllimport) #endif #include "pch.h" class KLINK1_API HvSerialPort1 { 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; CRITICAL_SECTION g_cs; bool m_bAutoReConnect = 1; bool m_bUseWorkThread = 0; // CCriticalSection m_CS1; // CCriticalSection m_CS2; volatile bool MyThreadProc1ToRun; volatile bool MyThreadProc1Running; volatile int RecvBufDataLen = 0; unsigned char RecvBuf[4096]; volatile int RecvBufPos = 0; unsigned char RecvBuf2[4096]; DWORD m_dwError; CString m_strResult; int m_nCountToTry = 3; int m_nCountToWait = 1; 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; HvSerialPort1(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; }; ~HvSerialPort1() { 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 Open(); int ClearStatData(); int CalSpeed(); int Close(); int SetRecvDoneCallBack(pRecvDone); int ClearBuf(); int Send(void * pBuf, int len1); int Recv(void * pBuf, int len1); int RecvFromCom(void * pBuf, int len1); int RecvFromBuf(void * pBuf, int len1); protected: // int PurgeComBuf() // { //ClearCommError() // return PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); // } static UINT MyJumper1(LPVOID pParam); DWORD WINAPI MyThreadProc1(LPVOID pParam); };