|
在一个dll中定义了消息发送的函数,
bool StartMonitor(HANDLE hComm, HWND hOwner)
{
DWORD dwThreadId;
CommInfo.hComm = hComm;
CommInfo.hOwner = hOwner;
hThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0, ThreadProc, &CommInfo, 0, &dwThreadId);
if (hThread == NULL)
return false;
return true;
}
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
COMMINFO *pCommInfo;
DWORD Bytes = 0;
DWORD BytesToRead = 0;
DWORD ErrorFlags = 0;
char CnfMsg[MAX_BUF_SIZE] = "\0";
char szReadBuf[MAX_BUF_SIZE] = "\0";
DWORD dwEvtMask = 0;
DWORD dwSignal = 0;
pCommInfo = (COMMINFO *)lpParameter;
//清除串口缓冲区
PurgeComm(pCommInfo->hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
//哪些串口事件需要监视
SetCommMask(pCommInfo->hComm, EV_ERR | EV_RLSD | EV_RING | EV_RXCHAR | EV_TXEMPTY);
while (1) {
WaitCommEvent(pCommInfo->hComm, &dwEvtMask, &Overlapped);
dwSignal = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
if (dwSignal == WAIT_OBJECT_0) {
if((dwEvtMask & EV_RXCHAR) == EV_RXCHAR) {
Sleep(100);
RecvChar(pCommInfo->hComm);
if (strlen(RecvBuf) != 0) {
strupr(RecvBuf);
#ifdef SMS_DLL_DEBUG
sprintf(RecvDebugInfo, "<-- %s", RecvBuf);
if (strncmp(RecvBuf, "ATWITS", 6))
PostMessage(pCommInfo->hOwner, WM_RECVDATA, 1, (LPARAM)&RecvDebugInfo);
else
if (DispFlag)
PostMessage(pCommInfo->hOwner, WM_RECVDATA, 1, (LPARAM)&RecvDebugInfo);
#endif
if (!strncmp(RecvBuf, "ATWITS+CMTI:1", 13)) {
if (pCommInfo->hOwner != NULL)
PostMessage(pCommInfo->hOwner, WM_RECVSMS, (LPARAM)pCommInfo->hComm, 0);
}
// else if (!strncmp(RecvBuf, "ATWITS", 6)) {
// if (pCommInfo->hOwner != NULL)
// PostMessage(pCommInfo->hOwner, WM_POWERON, 0, 0);
// }
else if (strlen(RecvBuf) != 0) {
//增加临界区控制
memset(RetInfo, 0, MAX_BUF_SIZE);
strcpy(RetInfo, RecvBuf);
SetEvent(hRecvEvent);
}
memset(RecvBuf, 0, MAX_BUF_SIZE);
}
}
ResetEvent(Overlapped.hEvent);
}
else if(dwSignal == WAIT_OBJECT_0 + 1) {
SendChar(pCommInfo->hComm);
}
}
return 0;
}
在c#程序中调用StartMonitor,就可开启对com口的监视,但是程序中需要对一台电脑上的多个串口设备进行监视,因此在c#中重载了winproc函数,RecvSMS((IntPtr)m.WParam, phoneNum, msg);对上面消息进行接收,不能够很好的接收不知大家有没好办法
|
|