|
The function of sending messages is defined in a 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;
Ranch
pCommInfo = (COMMINFO *) lpParameter;
// Clear the serial buffer
PurgeComm (pCommInfo-> hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
Ranch
// Which serial events need to be monitored
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) {
// Increase critical section control
memset (RetInfo, 0, MAX_BUF_SIZE);
strcpy (RetInfo, RecvBuf);
SetEvent (hRecvEvent);
}
Ranch
memset (RecvBuf, 0, MAX_BUF_SIZE);
}
}
ResetEvent (Overlapped.hEvent);
}
else if (dwSignal == WAIT_OBJECT_0 + 1) {
SendChar (pCommInfo-> hComm);
}
}
return 0;
}
Calling StartMonitor in the c # program can enable monitoring of the com port, but the program needs to monitor multiple serial devices on a computer, so the winproc function is overloaded in c #. m.WParam, phoneNum, msg); the above message is not received well, I wonder if everyone has a good way |
|