📄 commport.cpp
字号:
}
}
if( i == 100 )
{
OutputDebugString("DoRead loop executed 100 times.\n");
}
}
//////////////////////////////////////////////////////////////////////
// DoWaitCommEventLoop
//////////////////////////////////////////////////////////////////////
void CCommPort::DoWaitCommEventLoop( void )
{
int i;
for( i = 0; i < 100; i++ )
{
if( m_hPort == NULL )
break;
if( DoWaitCommEvent() )
{
OnCommEvent();
}
else
{
break;
}
}
if( i == 100 )
{
OutputDebugString("WaitCommEvent loop executed 100 times.\n");
}
}
//////////////////////////////////////////////////////////////////////
// DoWriteLoop
//////////////////////////////////////////////////////////////////////
void CCommPort::DoWriteLoop( void )
{
// char szMsg[80];
int i;
int nBytesToWrite;
for( i = 0; i < 100; i++ )
{
if( m_hPort == NULL )
break;
if( m_WriteQueue.size() > 0 )
{
//wsprintf( szMsg, "DoWriteLoop(%d)\n", i );
//OutputDebugString( szMsg );
deque<CWriteBuffer*>::iterator iter;
iter = m_WriteQueue.begin();
// CWriteBuffer* p = (*iter);
m_WriteQueue.pop_front();
memcpy( m_szWriteBuff, (*iter)->Buffer, (*iter)->Bytes );
nBytesToWrite = (*iter)->Bytes;
delete (*iter);
if( WritePacket( m_szWriteBuff, nBytesToWrite ) )
{
//wsprintf( szMsg, "Sent %d bytes 2\n", (*iter)->Bytes );
//OutputDebugString( szMsg );
}
else
{
break;
}
if( i == 100 )
{
OutputDebugString("WriteEvent loop executed 100 times.\n");
}
}
else
{
break;
}
}
}
//////////////////////////////////////////////////////////////////////
// ReadEventSignalled
//////////////////////////////////////////////////////////////////////
void CCommPort::ReadEventSignalled(void)
{
BOOL bRet;
if( m_hPort == NULL )
return;
bRet = GetOverlappedResult( m_hPort, &m_ReadOverlapped, &m_BytesRead, FALSE );
ResetEvent( m_ReadOverlapped.hEvent );
if( bRet )
{
if( m_BytesRead > 0 )
{
m_szReadBuff[m_BytesRead] = '\0';
if( m_bDebugLog )
{
WriteDebugLog( true );
}
OnRead();
}
DoReadLoop();
}
else
{
DWORD 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("Likely that the Service Provider has closed the port.\n");
}
OutputDebugString( "Unexpected GetOverlappedResult Read Error: " );
}
}
//////////////////////////////////////////////////////////////////////
// WaitCommEventSignalled
//////////////////////////////////////////////////////////////////////
void CCommPort::WaitCommEventSignalled(void)
{
BOOL bRet;
DWORD dwBytes = 0;
if( m_hPort == NULL )
return;
bRet = GetOverlappedResult( m_hPort, &m_CommEventOverlapped, &dwBytes, FALSE );
ResetEvent( m_CommEventOverlapped.hEvent );
if( bRet )
{
OnCommEvent();
DoWaitCommEventLoop();
}
else
{
DWORD 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("Likely that the Service Provider has closed the port.\n");
}
OutputDebugString( "Unexpected GetOverlappedResult Read Error: ");
}
}
//////////////////////////////////////////////////////////////////////
// OnWriteEvent
//////////////////////////////////////////////////////////////////////
void CCommPort::WriteEventSignalled(void)
{
BOOL bRet;
if( m_hPort == NULL )
return;
bRet = GetOverlappedResult( m_hPort, &m_WriteOverlapped, &m_BytesWritten, FALSE );
ResetEvent( m_WriteOverlapped.hEvent );
if( bRet )
{
//DWORD dwErrs = 0;
//COMSTAT stat;
//ClearCommError( m_hPort, &dwErrs, &stat );
//char szMsg[80];
//wsprintf( szMsg, "Wrote %d bytes,\n", m_BytesWritten);
//OutputDebugString( szMsg );
m_bWriteInProgress = false;
OnWrite();
if( m_bDebugLog )
{
WriteDebugLog( false );
}
DoWriteLoop();
}
else
{
DWORD 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("Likely that the Service Provider has closed the port.\n");
}
OutputDebugString( "Unexpected GetOverlappedResult Read Error: " );
}
}
//////////////////////////////////////////////////////////////////////
// OnCommEvent
//////////////////////////////////////////////////////////////////////
void CCommPort::OnCommEvent(void)
{
if( m_CommEvent & EV_CTS )
{
OutputDebugString( "CCommPort::OnCommEvent: CTS changed state\n" );
}
if( m_CommEvent & EV_ERR )
{
OutputDebugString( "CCommPort::OnCommEvent: Error\n" );
}
if( m_CommEvent & EV_DSR )
{
OutputDebugString( "CCommPort::OnCommEvent: DSR changed state\n" );
}
if( m_CommEvent & EV_RLSD )
{
OutputDebugString( "CCommPort::OnCommEvent: RLSD changed state\n" );
}
if( m_CommEvent & EV_BREAK )
{
OutputDebugString( "CCommPort::OnCommEvent: Break detected\n" );
}
}
//////////////////////////////////////////////////////////////////////
// OnConnect
//////////////////////////////////////////////////////////////////////
void CCommPort::OnConnect(void)
{
// OutputDebugString( "CCommPort::OnConnect\n" );
}
//////////////////////////////////////////////////////////////////////
// OnDisconnect - return true if no command sent
//////////////////////////////////////////////////////////////////////
bool CCommPort::OnDisconnect(void)
{
// OutputDebugString( "CCommPort::OnDisconnect\n" );
return true;
}
//////////////////////////////////////////////////////////////////////
// OnRead
//////////////////////////////////////////////////////////////////////
void CCommPort::OnRead(void)
{
// OutputDebugString( "CCommPort::OnRead\n" );
}
//////////////////////////////////////////////////////////////////////
// OnReadLine
//////////////////////////////////////////////////////////////////////
void CCommPort::OnReadLine(void)
{
// OutputDebugString( "CModem::OnReadLine\n" );
}
//////////////////////////////////////////////////////////////////////
// OnWrite
//////////////////////////////////////////////////////////////////////
void CCommPort::OnWrite(void)
{
// OutputDebugString( "CCommPort::OnWrite\n" );
}
//////////////////////////////////////////////////////////////////////
// SetCommParam
//////////////////////////////////////////////////////////////////////
void CCommPort::SetCommParam( DWORD BaudRate, BYTE ByteSize, BYTE Parity, BYTE StopBits )
{
m_BaudRate = BaudRate;
m_ByteSize = ByteSize;
m_Parity = Parity;
m_StopBits = StopBits;
}
//////////////////////////////////////////////////////////////////////
// SetFlowControl
//////////////////////////////////////////////////////////////////////
void CCommPort::SetFlowControl( bool bDSRFlowControl, bool bCTSFlowControl, bool bSoftFlowControl )
{
m_bDSRFlowControl = bDSRFlowControl;
m_bCTSFlowControl = bCTSFlowControl;
m_bSoftFlowControl = bSoftFlowControl;
}
//////////////////////////////////////////////////////////////////////
// ParseIntoLines
//////////////////////////////////////////////////////////////////////
void CCommPort::ParseIntoLines(void)
{
DWORD i;
for( i = 0; i < m_BytesRead; i++ )
{
if( m_bEolFlag )
{
if( (m_szReadBuff[i] != '\r') && (m_szReadBuff[i] != '\n') )
{
m_bEolFlag = false;
}
}
if( !m_bEolFlag )
{
if( (m_szReadBuff[i] == '\r') || (m_szReadBuff[i] == '\n') )
{
m_bEolFlag = true;
if( m_nLineBuffPtr > 0 )
{
m_szLineBuff[m_nLineBuffPtr] = '\0';
OnReadLine();
InitLineParser();
}
}
else
{
if( m_nLineBuffPtr >= READBUF_SIZE )
{
OutputDebugString( "Line buffer overflow!\n" );
InitLineParser();
}
m_szLineBuff[m_nLineBuffPtr++] = m_szReadBuff[i];
}
}
}
}
void CCommPort::EnableSoftFlowControl( bool bEnable )
{
if( m_bSoftFlowControl )
{
m_dcb.fOutX = (bEnable) ? 1 : 0;
if( !SetCommState( m_hPort, &m_dcb) )
{
OutputDebugString( "Error in SetCommState!\n" );
}
if( bEnable )
{
EscapeCommFunction( m_hPort, SETXON );
}
if ( bEnable )
{
//OutputDebugString( "Enabling software flow control\n" );
}
else
{
//OutputDebugString( "Disabling software flow control\n" );
}
}
}
void CCommPort::OpenDebugLog(void)
{
char szLogFile[MAX_PATH];
if( !m_bDebugLog )
{
return;
}
wsprintf( szLogFile, "%sM_%s.LOG", m_sLogDir.c_str(), m_sPort.c_str() );
m_pLogFile = fopen( szLogFile, "a" );
}
void CCommPort::WriteDebugLog( bool bRead )
{
DWORD dwTicks = GetTickCount();
char szMsg[80];
int nBytes;
char* pData;
int i;
wsprintf( szMsg, "%11d%c", dwTicks, (bRead) ? '<' : '>' );
memset( szMsg+12, ' ', sizeof(szMsg) - 12 );
szMsg[78] = '\n';
szMsg[79] = '\0';
if( m_pLogFile )
{
if( bRead )
{
pData = m_szReadBuff;
nBytes = m_BytesRead;
}
else
{
pData = m_szWriteBuff;
nBytes = m_BytesWritten;
}
while( nBytes > 0 )
{
for( i = 0; i < 16; i++ )
{
if( i < nBytes )
{
szMsg[13+(i*3)] = s_HexDigits[(*pData & 0xf0) >> 4];
szMsg[14+(i*3)] = s_HexDigits[(*pData & 0x0f)];
szMsg[62+i] = ( (*pData < 127) && (*pData > 31) ) ? *pData : '.';
pData++;
}
else
{
szMsg[13+(i*3)] = ' ';
szMsg[14+(i*3)] = ' ';
szMsg[62+i] = ' ';
}
}
fputs( szMsg, m_pLogFile );
nBytes -= 16;
}
}
}
void CCommPort::CloseDebugLog(void)
{
if( !m_bDebugLog )
{
return;
}
if( m_pLogFile )
{
fclose( m_pLogFile );
m_pLogFile = NULL;
m_bDebugLog = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -