📄 comm.cpp
字号:
// Comm.cpp: implementation of the CComm class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Comm.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CComm::CComm()
{
HKEY hKey;
if(RegOpenKeyEx(HKEY_CURRENT_USER, "software\\doggle\\AirGuard", 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
MessageBox(NULL, "Calling RegOpenKeyEx error", "error", MB_OK | MB_ICONERROR);
char lpstrValue[100], lpszMessage[100];
DWORD cbData = 100, dwType;
unsigned long dwRtValue;
dwRtValue = RegQueryValueEx(hKey, "SerialPort", NULL, &dwType, (LPBYTE)lpstrValue, &cbData);
if (dwRtValue != ERROR_SUCCESS){
// FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwRtValue, 0, lpszMessage, 100, NULL);
sprintf(lpszMessage, "找不到指定文件, 请与供应商联系");
MessageBox(NULL, lpszMessage, "error", MB_OK | MB_ICONERROR);
exit(0);
}
if (strlen(lpstrValue) <= 20)
nSerialPort = (int)atoi(lpstrValue);
else MessageBox(NULL, "Regester error", "error", MB_OK | MB_ICONSTOP);
}
BOOL CComm::InitComm (HWND hwnd)
{
DCB dcb;
int errdcb, err;
int port = nSerialPort;
DWORD threadComm;
char sComPort[80];
char sDCB[] = " :baud=9600 parity=N data=8 stop=1";
COMMTIMEOUTS cto;
hwndComm = hwnd;
sprintf (sComPort, "COM%d", port);
if ((idComDev = CreateFile (sComPort, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
== INVALID_HANDLE_VALUE) {
#ifdef _DEBUG
DWORD e = GetLastError();
sprintf (sComPort, "Error code: %x", e);
MessageBox (NULL, sComPort, "COMM Error", MB_OK);
#endif
return FALSE;
}
lstrcat (sComPort, sDCB);
SetupComm (idComDev, 1024, 1024);
errdcb = BuildCommDCB (sComPort, &dcb);
dcb.fParity = 1;
dcb.Parity = ODDPARITY; //EVENPARITY;
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
err = SetCommState (idComDev, &dcb);
if (!errdcb || !err){
#ifdef _DEBUG
MessageBox(NULL, "Error open comm!", "Error", MB_OK | MB_ICONSTOP);
#endif
CloseHandle(idComDev);
return FALSE;
}
PurgeComm (idComDev, PURGE_RXCLEAR);
PurgeComm (idComDev, PURGE_TXCLEAR);
cto.ReadIntervalTimeout = 1000;
cto.ReadTotalTimeoutMultiplier = 1000;
cto.ReadTotalTimeoutConstant = 1000;
cto.WriteTotalTimeoutMultiplier = 1000;
cto.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(idComDev, &cto);
ToStop = 0;
if((hthreadComm = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE) CheckComm,
this, 0, &threadComm)) == NULL){
CloseHandle(idComDev);
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
//
BOOL CComm::CloseComm(void)
{
char msg0[50] = "Terminate thread error!\nError code: ";
char msg1[50] = "Close serial port error!\nError code: ";
char tmp[50];
#ifdef _DEBUG
DWORD e;
#endif
ToStop = 1;
SetCommMask (idComDev, 0); // To make WaitCommEvent return
if (WaitForSingleObject (hthreadComm, 3000) == WAIT_FAILED)
if (!TerminateThread (hthreadComm, 0)){
#ifdef _DEBUG
e = GetLastError ();
sprintf (tmp, "0x%x", e);
strcat (msg0, tmp);
MessageBox (NULL, msg0, "INFO", MB_OK|MB_ICONSTOP);
#endif
return FALSE;
}
if (!CloseHandle(idComDev)){
#ifdef _DEBUG
e = GetLastError();
sprintf (tmp, "0x%x", e);
strcat (msg1, tmp);
MessageBox (NULL, msg1, "INFO", MB_OK|MB_ICONSTOP);
#endif
return FALSE;
}
return TRUE;
}
//---------------------------------------------------------------------------
//
void CComm::CheckComm(CComm *pSerialPort)
{
DWORD dwEvtMask;
char stmp[50] = "";
static int nRead;
DWORD error;
#ifdef _DEBUG
DWORD e;
#endif
COMSTAT stat;
if (!SetCommMask (pSerialPort->idComDev, EV_RXCHAR /*(0x0001)*/ )){
#ifdef _DEBUG
e = GetLastError();
sprintf (stmp, "SetCommMask Failed.\nError Code: 0x%x\nHandle: 0x%x",
e, pSerialPort->idComDev);
MessageBox(NULL, stmp, "Error", MB_OK | MB_ICONSTOP);
#endif
CloseHandle(pSerialPort->idComDev);
PostQuitMessage(0);
ExitThread (0);
}
for (nRead = 0; pSerialPort->ToStop != 1; ){
WaitCommEvent (pSerialPort->idComDev, &dwEvtMask, NULL);
if (pSerialPort->ToStop) break;
if (dwEvtMask == EV_RXCHAR){
ClearCommError (pSerialPort->idComDev, &error, &stat);
nRead = (int)stat.cbInQue;
PostMessage (pSerialPort->hwndComm, WM_COMMNOTIFY32, 0, nRead);
}
}
ExitThread (0);
}
//---------------------------------------------------------------------------
//
int CComm::ReadComm(unsigned char* cBuf, int toRead)
{
DWORD nRead;
if (!ReadFile (idComDev, cBuf, toRead, &nRead, NULL))
return 0;
return nRead;
}
//---------------------------------------------------------------------------
//
int CComm::WriteComm(unsigned char* cBuf, int toWrite)
{
DWORD nWritten;
int i;
PurgeComm (idComDev, PURGE_RXCLEAR);
PurgeComm (idComDev, PURGE_TXCLEAR);
for (i=0; i< toWrite; i++){
if (!WriteFile (idComDev, cBuf+i, 1, &nWritten, NULL))
return i;
//Sleep(50);
}
return toWrite;
}
CComm::~CComm()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -