📄 serialport.cpp
字号:
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!EscapeCommFunction(hComm, dwFunc))
{
TRACE(_T("Failed in call to EscapeCommFunction\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_ClearDTR(HANDLE hComm)
{
return RS_Escape(hComm, CLRDTR);
}
extern "C" BOOL FAR PASCAL RS_ClearRTS(HANDLE hComm)
{
return RS_Escape(hComm, CLRRTS);
}
extern "C" BOOL FAR PASCAL RS_SetDTR(HANDLE hComm)
{
return RS_Escape(hComm, SETDTR);
}
extern "C" BOOL FAR PASCAL RS_SetRTS(HANDLE hComm)
{
return RS_Escape(hComm, SETRTS);
}
extern "C" BOOL FAR PASCAL RS_SetXOFF(HANDLE hComm)
{
return RS_Escape(hComm, SETXOFF);
}
extern "C" BOOL FAR PASCAL RS_SetXON(HANDLE hComm)
{
return RS_Escape(hComm, SETXON);
}
extern "C" BOOL FAR PASCAL RS_GetProperties(HANDLE hComm, COMMPROP& properties)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!GetCommProperties(hComm, &properties))
{
TRACE(_T("Failed in call to GetCommProperties\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_GetModemStatus(HANDLE hComm, DWORD& dwModemStatus)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!GetCommModemStatus(hComm, &dwModemStatus))
{
TRACE(_T("Failed in call to GetCommModemStatus\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_SetMask(HANDLE hComm, DWORD dwMask)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!SetCommMask(hComm, dwMask))
{
TRACE(_T("Failed in call to SetCommMask\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_GetMask(HANDLE hComm, DWORD& dwMask)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!GetCommMask(hComm, &dwMask))
{
TRACE(_T("Failed in call to GetCommMask\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_Flush(HANDLE hComm)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!FlushFileBuffers(hComm))
{
TRACE(_T("Failed in call to FlushFileBuffers\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_Purge(HANDLE hComm, DWORD dwFlags)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!PurgeComm(hComm, dwFlags))
{
TRACE(_T("Failed in call to PurgeComm\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_TerminateOutstandingWrites(HANDLE hComm)
{
return RS_Purge(hComm, PURGE_TXABORT);
}
extern "C" BOOL FAR PASCAL RS_TerminateOutstandingReads(HANDLE hComm)
{
return RS_Purge(hComm, PURGE_RXABORT);
}
extern "C" BOOL FAR PASCAL RS_ClearWriteBuffer(HANDLE hComm)
{
return RS_Purge(hComm, PURGE_TXCLEAR);
}
extern "C" BOOL FAR PASCAL RS_ClearReadBuffer(HANDLE hComm)
{
return RS_Purge(hComm, PURGE_RXCLEAR);
}
extern "C" BOOL FAR PASCAL RS_Setup(HANDLE hComm, DWORD dwInQueue, DWORD dwOutQueue)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!SetupComm(hComm, dwInQueue, dwOutQueue))
{
TRACE(_T("Failed in call to SetupComm\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_SetTimeouts(HANDLE hComm, COMMTIMEOUTS& timeouts)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!SetCommTimeouts(hComm, &timeouts))
{
TRACE(_T("Failed in call to SetCommTimeouts\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_GetTimeouts(HANDLE hComm, COMMTIMEOUTS& timeouts)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
if (!GetCommTimeouts(hComm, &timeouts))
{
TRACE(_T("Failed in call to GetCommTimeouts\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_Set0Timeout(HANDLE hComm)
{
COMMTIMEOUTS Timeouts;
ZeroMemory(&Timeouts, sizeof(COMMTIMEOUTS));
Timeouts.ReadIntervalTimeout = MAXDWORD;
return RS_SetTimeouts(hComm, Timeouts);
}
extern "C" BOOL FAR PASCAL RS_Set0WriteTimeout(HANDLE hComm)
{
COMMTIMEOUTS Timeouts;
RS_GetTimeouts(hComm, Timeouts);
Timeouts.WriteTotalTimeoutMultiplier = 0;
Timeouts.WriteTotalTimeoutConstant = 0;
return RS_SetTimeouts(hComm, Timeouts);
}
extern "C" BOOL FAR PASCAL RS_Set0ReadTimeout(HANDLE hComm)
{
COMMTIMEOUTS Timeouts;
RS_GetTimeouts(hComm, Timeouts);
Timeouts.ReadIntervalTimeout = MAXDWORD;
Timeouts.ReadTotalTimeoutMultiplier = 0;
Timeouts.ReadTotalTimeoutConstant = 0;
return RS_SetTimeouts(hComm, Timeouts);
}
extern "C" BOOL FAR PASCAL RS_WaitEvent(HANDLE hComm, DWORD& dwMask)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
ASSERT(!g_bOverlapped);
if (!WaitCommEvent(hComm, &dwMask, NULL))
{
TRACE(_T("Failed in call to WaitCommEvent\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_WaitEventOv(HANDLE hComm, DWORD& dwMask, OVERLAPPED& overlapped)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
ASSERT(g_bOverlapped);
ASSERT(overlapped.hEvent);
if (!WaitCommEvent(hComm, &dwMask, &overlapped))
{
TRACE(_T("Failed in call to WaitCommEvent\n"));
return FALSE;
}
return TRUE;
}
extern "C" BOOL FAR PASCAL RS_WriteBufferOv(HANDLE hComm, const void* lpBuf, DWORD dwCount, DWORD* pBytesWrite)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
ASSERT(g_bOverlapped);
OVERLAPPED osWrite = {0};
DWORD dwRes;
BOOL fRes;
// Create this write operation's OVERLAPPED structure hEvent.
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL)
{
// Error creating overlapped event handle.
return FALSE;
}
DWORD dwError;
RS_ClearError(hComm, dwError);
// Issue write
if (!WriteFile(hComm, lpBuf, dwCount, pBytesWrite, &osWrite))
{
if (GetLastError() == ERROR_IO_PENDING)
{
// Write is pending.
dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
switch(dwRes)
{
// Overlapped event has been signaled.
case WAIT_OBJECT_0:
if (!::GetOverlappedResult(hComm, &osWrite, pBytesWrite, FALSE))
fRes = FALSE;
else
{
if (dwCount != *pBytesWrite) fRes = FALSE;
else fRes = TRUE;
}
break;
case WAIT_TIMEOUT:
//::CancelIo(hComm);
fRes = FALSE;
break;
default:
// An error has occurred in WaitForSingleObject. This usually
// indicates a problem with the overlapped event handle.
fRes = FALSE;
break;
}
}
else
{
// WriteFile failed, but it isn't delayed. Report error.
fRes = FALSE;
}
}
else
{
// WriteFile completed immediately.
if (dwCount != *pBytesWrite)
{
// The write operation timed out. I now need to
// decide if I want to abort or retry. If I retry,
// I need to send only the bytes that weren't sent.
// If I want to abort, then I would just set fRes to
// FALSE and return.
fRes = FALSE;
}
else
fRes = TRUE;
}
if(!fRes)
RS_Purge(hComm, PURGE_TXCLEAR | PURGE_TXABORT);
CloseHandle(osWrite.hEvent);
return fRes;
}
extern "C" BOOL FAR FAR PASCAL RS_ReadBufferOv(HANDLE hComm, void* lpBuf, DWORD dwCount, DWORD* pBytesRead)
{
ASSERT(hComm != INVALID_HANDLE_VALUE);
ASSERT(g_bOverlapped);
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};
BOOL fRes=FALSE;
// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
{
// Error creating overlapped event; abort.
return FALSE;
}
DWORD dwError;
RS_ClearError(hComm, dwError);
// Issue read operation.
if (!ReadFile(hComm, lpBuf, dwCount, pBytesRead, &osReader))
{
if (GetLastError() == ERROR_IO_PENDING)
{
DWORD dwRes;
dwRes = WaitForSingleObject(osReader.hEvent, 200);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!::GetOverlappedResult(hComm, &osReader, pBytesRead, FALSE))
{
TRACE("Library:SERIAL Function:RS_ReadBuffOv GetOverlappedResult failed!\n");
fRes = FALSE;
}
else
{
if (*pBytesRead != dwCount)
{
fRes = FALSE;
TRACE("Library:SERIAL Function:RS_ReadBuffOv GetOverlappedResult BytesRead=%d dwCount=%d!\n",*pBytesRead,dwCount);
}
else
fRes = TRUE;
}
// Reset flag so that another opertion can be issued.
break;
case WAIT_TIMEOUT:
// Operation isn't complete yet. fWaitingOnRead flag isn't
// changed since I'll loop back around, and I don't want
// to issue another read until the first one finishes.
//
// This is a good time to do some background work.
//::CancelIo(hComm);
TRACE("Library:SERIAL Function:RS_ReadBuffOv timeout !\n");
fRes = FALSE;
break;
default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
TRACE("Library:SERIAL Function:RS_ReadBuffOv default error !\n");
fRes = FALSE;
break;
}
}
else
{
// Error in communications; report it.
TRACE("Library:SERIAL Function:RS_ReadBuffOv error in communications !\n");
fRes = FALSE;
}
}
else
{
// ReadFile completed immediately.
if (*pBytesRead != dwCount)
{
TRACE("Library:SERIAL Function:RS_ReadBuffOv ReadFile completed immediately BytesRead=%d dwCount=%d!\n",*pBytesRead,dwCount);
fRes = FALSE;
}
else
fRes = TRUE;
}
if(!fRes)
RS_Purge(hComm,PURGE_RXCLEAR | PURGE_RXABORT);
CloseHandle(osReader.hEvent);
return fRes;
}
/////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -