?? commcode.c
字號:
}
default: // This case should never occur.
{
OutputDebugPrintf("Unexpected Wait return value '%lx'",
dwHandleSignaled);
PostHangupCall();
goto EndReadThread;
}
} // End of switch(dwHandleSignaled).
} // End of while(TRUE) loop.
// Time to clean up Read Thread.
EndReadThread:
OutputDebugString("Read thread shutting down\n");
PurgeComm(g_hCommFile, PURGE_RXABORT | PURGE_RXCLEAR);
CloseHandle(overlappedRead.hEvent);
CloseHandle(overlappedCommEvent.hEvent);
g_dwReadThreadID = 0;
CloseHandle(g_hReadThread);
g_hReadThread = 0;
return 0;
}
//
// FUNCTION: SetupReadEvent(LPOVERLAPPED, LPSTR, DWORD, LPDWORD)
//
// PURPOSE: Sets up an overlapped ReadFile
//
// PARAMETERS:
// lpOverlappedRead - address of overlapped structure to use.
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
// lpnNumberOfBytesRead - address of DWORD to place the number of read bytes.
//
// RETURN VALUE:
// TRUE if able to successfully setup the ReadFile. FALSE if there
// was a failure setting up or if the CloseEvent object was signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread. This
// function sets up the overlapped ReadFile so that it can later
// be waited on (or more appropriatly, so the event in the overlapped
// structure can be waited upon). If there is data waiting, it is
// handled and the next ReadFile is initiated.
// Another possible reason for returning FALSE is if the comm port
// is closed by the service provider.
//
//
//
BOOL SetupReadEvent(LPOVERLAPPED lpOverlappedRead,
LPSTR lpszInputBuffer, DWORD dwSizeofBuffer,
LPDWORD lpnNumberOfBytesRead)
{
DWORD dwLastError;
StartSetupReadEvent:
// Make sure the CloseEvent hasn't been signaled yet.
// Check is needed because this function is potentially recursive.
if (WAIT_TIMEOUT != WaitForSingleObject(g_hCloseEvent,0))
return FALSE;
// Start the overlapped ReadFile.
if (ReadFile(g_hCommFile,
lpszInputBuffer, dwSizeofBuffer,
lpnNumberOfBytesRead, lpOverlappedRead))
{
// This would only happen if there was data waiting to be read.
OutputDebugString("Data waiting for ReadFile.\n");
// Handle the data.
if (!HandleReadData(lpszInputBuffer, *lpnNumberOfBytesRead))
{
return FALSE;
}
// Start waiting for more data.
goto StartSetupReadEvent;
}
// ReadFile failed. Expected because of overlapped I/O.
dwLastError = GetLastError();
// LastError was ERROR_IO_PENDING, as expected.
if (dwLastError == ERROR_IO_PENDING)
{
OutputDebugString("Waiting for data from comm connection.\n");
return TRUE;
}
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if (dwLastError == ERROR_INVALID_HANDLE)
{
OutputDebugString("ERROR_INVALID_HANDLE, "
"Likely that the Service Provider has closed the port.\n");
return FALSE;
}
// Unexpected error. No idea what could cause this to happen.
OutputDebugLastError(dwLastError,"Unexpected ReadFile error: ");
PostHangupCall();
return FALSE;
}
//
// FUNCTION: HandleReadEvent(LPOVERLAPPED, LPSTR, DWORD, LPDWORD)
//
// PURPOSE: Retrieves and handles data when there is data ready.
//
// PARAMETERS:
// lpOverlappedRead - address of overlapped structure to use.
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
// lpnNumberOfBytesRead - address of DWORD to place the number of read bytes.
//
// RETURN VALUE:
// TRUE if able to successfully retrieve and handle the available data.
// FALSE if unable to retrieve or handle the data.
//
// COMMENTS:
//
// This function is another helper function for the Read Thread. This
// is the function that is called when there is data available after
// an overlapped ReadFile has been setup. It retrieves the data and
// handles it.
//
//
BOOL HandleReadEvent(LPOVERLAPPED lpOverlappedRead,
LPSTR lpszInputBuffer, DWORD dwSizeofBuffer,
LPDWORD lpnNumberOfBytesRead)
{
DWORD dwLastError;
if (GetOverlappedResult(g_hCommFile,
lpOverlappedRead, lpnNumberOfBytesRead, FALSE))
{
return HandleReadData(lpszInputBuffer, *lpnNumberOfBytesRead);
}
// Error in GetOverlappedResult; handle it.
dwLastError = GetLastError();
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if (dwLastError == ERROR_INVALID_HANDLE)
{
OutputDebugString("ERROR_INVALID_HANDLE, "
"Likely that the Service Provider has closed the port.\n");
return FALSE;
}
OutputDebugLastError(dwLastError,
"Unexpected GetOverlappedResult Read Error: ");
PostHangupCall();
return FALSE;
}
//
// FUNCTION: HandleReadData(LPCSTR, DWORD)
//
// PURPOSE: Deals with data after its been read from the comm file.
//
// PARAMETERS:
// lpszInputBuffer - Buffer to place incoming bytes.
// dwSizeofBuffer - size of lpszInputBuffer.
//
// RETURN VALUE:
// TRUE if able to successfully handle the data.
// FALSE if unable to allocate memory or handle the data.
//
// COMMENTS:
//
// This function is yet another helper function for the Read Thread.
// It LocalAlloc()s a buffer, copies the new data to this buffer and
// calls PostWriteToDisplayCtl to let the EditCtls module deal with
// the data. Its assumed that PostWriteToDisplayCtl posts the message
// rather than dealing with it right away so that the Read Thread
// is free to get right back to waiting for data. Its also assumed
// that the EditCtls module is responsible for LocalFree()ing the
// pointer that is passed on.
//
//
BOOL HandleReadData(LPCSTR lpszInputBuffer, DWORD dwSizeofBuffer)
{
// If we got data and didn't just time out empty...
if (dwSizeofBuffer)
{
LPSTR lpszPostedBytes;
// Do something with the bytes read.
OutputDebugString("Got something from Comm port!!!\n");
lpszPostedBytes = LocalAlloc(LPTR,dwSizeofBuffer+1);
if (lpszPostedBytes == NULL)
{
OutputDebugLastError(GetLastError(), "LocalAlloc: ");
return FALSE;
}
memcpy(lpszPostedBytes, lpszInputBuffer, dwSizeofBuffer);
lpszPostedBytes[dwSizeofBuffer] = '\0';
return PostWriteToDisplayCtl(lpszPostedBytes, dwSizeofBuffer);
}
}
//
// FUNCTION: SetupCommEvent(LPOVERLAPPED, LPDWORD)
//
// PURPOSE: Sets up the overlapped WaitCommEvent call.
//
// PARAMETERS:
// lpOverlappedCommEvent - Pointer to the overlapped structure to use.
// lpfdwEvtMask - Pointer to DWORD to received Event data.
//
// RETURN VALUE:
// TRUE if able to successfully setup the WaitCommEvent.
// FALSE if unable to setup WaitCommEvent, unable to handle
// an existing outstanding event or if the CloseEvent has been signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread that sets up
// the WaitCommEvent so we can deal with comm events (like Comm errors)
// if they occur.
//
//
BOOL SetupCommEvent(LPOVERLAPPED lpOverlappedCommEvent,
LPDWORD lpfdwEvtMask)
{
DWORD dwLastError;
StartSetupCommEvent:
// Make sure the CloseEvent hasn't been signaled yet.
// Check is needed because this function is potentially recursive.
if (WAIT_TIMEOUT != WaitForSingleObject(g_hCloseEvent,0))
return FALSE;
// Start waiting for Comm Errors.
if (WaitCommEvent(g_hCommFile, lpfdwEvtMask, lpOverlappedCommEvent))
{
// This could happen if there was an error waiting on the
// comm port. Lets try and handle it.
OutputDebugString("Event (Error) waiting before WaitCommEvent.\n");
if (!HandleCommEvent(NULL, lpfdwEvtMask, FALSE))
return FALSE;
// What could cause infinite recursion at this point?
goto StartSetupCommEvent;
}
// We expect ERROR_IO_PENDING returned from WaitCommEvent
// because we are waiting with an overlapped structure.
dwLastError = GetLastError();
// LastError was ERROR_IO_PENDING, as expected.
if (dwLastError == ERROR_IO_PENDING)
{
OutputDebugString("Waiting for a CommEvent (Error) to occur.\n");
return TRUE;
}
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if (dwLastError == ERROR_INVALID_HANDLE)
{
OutputDebugString("ERROR_INVALID_HANDLE, "
"Likely that the Service Provider has closed the port.\n");
return FALSE;
}
// Unexpected error. No idea what could cause this to happen.
OutputDebugLastError(dwLastError, "Unexpected WaitCommEvent error: ");
return FALSE;
}
//
// FUNCTION: HandleCommEvent(LPOVERLAPPED, LPDWORD, BOOL)
//
// PURPOSE: Handle an outstanding Comm Event.
//
// PARAMETERS:
// lpOverlappedCommEvent - Pointer to the overlapped structure to use.
// lpfdwEvtMask - Pointer to DWORD to received Event data.
// fRetrieveEvent - Flag to signal if the event needs to be
// retrieved, or has already been retrieved.
//
// RETURN VALUE:
// TRUE if able to handle a Comm Event.
// FALSE if unable to setup WaitCommEvent, unable to handle
// an existing outstanding event or if the CloseEvent has been signaled.
//
// COMMENTS:
//
// This function is a helper function for the Read Thread that (if
// fRetrieveEvent == TRUE) retrieves an outstanding CommEvent and
// deals with it. The only event that should occur is an EV_ERR event,
// signalling that there has been an error on the comm port.
//
// Normally, comm errors would not be put into the normal data stream
// as this sample is demonstrating. Putting it in a status bar would
// be more appropriate for a real application.
//
//
BOOL HandleCommEvent(LPOVERLAPPED lpOverlappedCommEvent,
LPDWORD lpfdwEvtMask, BOOL fRetrieveEvent)
{
DWORD dwDummy;
LPSTR lpszOutput;
char szError[128] = "";
DWORD dwErrors;
DWORD nOutput;
DWORD dwLastError;
lpszOutput = LocalAlloc(LPTR,256);
if (lpszOutput == NULL)
{
OutputDebugLastError(GetLastError(), "LocalAlloc: ");
return FALSE;
}
// If this fails, it could be because the file was closed (and I/O is
// finished) or because the overlapped I/O is still in progress. In
// either case (or any others) its a bug and return FALSE.
if (fRetrieveEvent)
if (!GetOverlappedResult(g_hCommFile,
lpOverlappedCommEvent, &dwDummy, FALSE))
{
dwLastError = GetLastError();
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if (dwLastError == ERROR_INVALID_HANDLE)
{
OutputDebugString("ERROR_INVALID_HANDLE, "
"Likely that the Service Provider has closed the port.\n");
return FALSE;
}
OutputDebugLastError(dwLastError,
"Unexpected GetOverlappedResult for WaitCommEvent: ");
return FALSE;
}
// Was the event an error?
if (*lpfdwEvtMask & EV_ERR)
{
// Which error was it?
if (!ClearCommError(g_hCommFile, &dwErrors, NULL))
{
dwLastError = GetLastError();
// Its possible for this error to occur if the
// service provider has closed the port. Time to end.
if (dwLastError == ERROR_INVALID_HANDLE)
{
OutputDebugString("ERROR_INVALID_HANDLE, "
"Likely that the Service Provider has closed the port.\n");
return FALSE;
}
OutputDebugLastError(GetLastError(),"ClearCommError: ");
return FALSE;
}
// Its possible that multiple errors occured and were handled
// in the last ClearCommError. Because all errors were signaled
// individually, but cleared all at once, pending comm events
// can yield EV_ERR while dwErrors equals 0. Ignore this event.
if (dwErrors == 0)
{
strcat(szError, "NULL Error");
}
if (dwErrors & CE_FRAME)
{
if (szError[0])
strcat(szError," and ");
strcat(szError,"CE_FRAME");
}
if (dwErrors & CE_OVERRUN)
{
if (szError[0])
strcat(szError," and ");
strcat(szError,"CE_OVERRUN");
}
if (dwErrors & CE_RXPARITY)
{
if (szError[0])
strcat(szError," and ");
strcat(szError,"CE_RXPARITY");
}
if (dwErrors & ~ (CE_FRAME | CE_OVERRUN | CE_RXPARITY))
{
if (szError[0])
strcat(szError," and ");
strcat(szError,"EV_ERR Unknown EvtMask");
}
nOutput = wsprintf(lpszOutput,
"Comm Event: '%s', EvtMask = '%lx'\n",
szError, dwErrors);
PostWriteToDisplayCtl(lpszOutput, nOutput);
return TRUE;
}
// Should not have gotten here. Only interested in ERR conditions.
OutputDebugPrintf("Unexpected comm event %lx",*lpfdwEvtMask);
return FALSE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -