CommPort.cpp
上传用户:glass0516
上传日期:2010-01-11
资源大小:104k
文件大小:21k
- /*****************************************************************************
- * RelayFax Open Source Project
- * Copyright 1996-2004 Alt-N Technologies, Ltd.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted only as authorized by the RelayFax Open
- * Source License. A copy of this license is available in file LICENSE
- * in the top-level directory of the distribution.
- *
- * RelayFax is a registered trademark of Alt-N Technologies, Ltd.
- *
- * Individual files and/or contributed packages may be copyright by
- * other parties and subject to additional restrictions.
- *****************************************************************************/
- ////////////////////////////////////////////////////////////////////////////////
- //
- // The purpose of CCommPort is to encapsulate all the Win32 communication API
- //
- ////////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommPort.h"
- CLS1SPEEDS cls1Speeds[MAX_CLS1SPEEDS] =
- {
- 0x00, 24, 24, 2400, 0, // 2400, v.27 ter
- 0x04, 48, 48, 4800, 1, // 4800, v.27
- 0x0c, 72, 72, 7200, 2, // 7200, v.29
- 0x08, 96, 96, 9600, 3, // 9600, v.29
- 0x06, 121, 121, 12000, 4, // 12000, V.33
- 0x02, 145, 145, 14400, 5, // 14400, V.33
- 0x0d, 73, 74, 7200, 2, // 7200, v.17
- 0x09, 97, 98, 9600, 3, // 9600, v.17
- 0x05, 121, 122, 12000, 4, // 12000, v.17
- 0x01, 145, 146, 14400, 5, // 14400, v.17
- };
- WORD Cls1ScanTimes_normal[8] = {20,40,10,5,10,20,40,0};
- WORD Cls1ScanTimes_fine[8] = {20,40,10,5,5,10,20,0};
- WORD Cls2ScanTimes_normal[8] = { 0, 5, 10, 10, 20, 20, 40, 40 };
- WORD Cls2ScanTimes_fine[8] = { 0, 5, 5, 10, 10, 20, 20, 40 };
- WORD Cls2FaxParamBitRates[6] = { 2400, 4800, 7200, 9600, 12000, 14400 };
- char CCommPort::s_HexDigits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CCommPort::CCommPort()
- {
- m_hPort = NULL;
- m_bDSRFlowControl = true;
- m_bCTSFlowControl = true;
- m_bSoftFlowControl = true;
- m_BaudRate = CBR_19200;
- m_ByteSize = 8;
- m_Parity = NOPARITY;
- m_StopBits = ONESTOPBIT;
- ZeroMemory( &m_ReadOverlapped, sizeof(m_ReadOverlapped) );
- ZeroMemory( &m_WriteOverlapped, sizeof(m_WriteOverlapped) );
- ZeroMemory( &m_CommEventOverlapped, sizeof(m_CommEventOverlapped) );
- m_ReadOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- m_WriteOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- m_CommEventOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- InitLineParser();
- m_bWriteInProgress = false;
- memset( &m_dcb, 0, sizeof(m_dcb) );
- m_dcb.DCBlength = sizeof(m_dcb);
- m_bDebugLog = false;
- m_pLogFile = NULL;
- }
- CCommPort::~CCommPort()
- {
- CloseHandle( m_ReadOverlapped.hEvent );
- CloseHandle( m_WriteOverlapped.hEvent );
- CloseHandle( m_CommEventOverlapped.hEvent );
- }
- //////////////////////////////////////////////////////////////////////
- // ConnectPort
- //////////////////////////////////////////////////////////////////////
- bool CCommPort::ConnectPort( string& sErr )
- {
- char szComDevice[16];
-
- wsprintf( szComDevice, "\\.\%s", m_sPort.c_str() );
- m_hPort = CreateFile( szComDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
-
- // see if this works before committing
- if( m_hPort == NULL || m_hPort == INVALID_HANDLE_VALUE )
- {
- if( GetLastError() == ERROR_ACCESS_DENIED )
- {
- // wait a second and try again
- Sleep( 1000 );
- m_hPort = CreateFile( szComDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
- }
- }
- if( m_hPort != NULL && m_hPort != INVALID_HANDLE_VALUE )
- {
- PurgeComm( m_hPort, PURGE_RXCLEAR | PURGE_TXCLEAR );
- if( !GetCommState( m_hPort, &m_dcb ) )
- {
- sErr.assign( "Error in GetCommState" );
- CloseHandle( m_hPort );
- return false;
- }
- if( !SetupComm( m_hPort, 20000, 20000 ) )
- {
- sErr.assign( "Error in SetupComm" );
- CloseHandle( m_hPort );
- return false;
- }
- m_dcb.BaudRate = m_BaudRate;
- m_dcb.ByteSize= m_ByteSize;
- m_dcb.fBinary = TRUE;
- m_dcb.Parity = m_Parity;
- m_dcb.StopBits = m_StopBits;
- m_dcb.fOutxCtsFlow = (m_bCTSFlowControl) ? TRUE : FALSE;
- m_dcb.fOutxDsrFlow = (m_bDSRFlowControl) ? TRUE : FALSE;
- m_dcb.fOutX = (m_bSoftFlowControl) ? 1 : 0;
- m_dcb.fInX = 0; // we shouldn't need flow control for receiving data
- // since we are using 20k buffers
- m_dcb.XonChar = XON;
- m_dcb.XoffChar = XOFF;
- // One of these two was causing a problem with Multitech ISI5634/8 multi-port modem card
- // m_dcb.fTXContinueOnXoff = TRUE;
- m_dcb.fRtsControl = RTS_CONTROL_ENABLE;
- m_dcb.fDtrControl = DTR_CONTROL_ENABLE;
- m_dcb.fAbortOnError = FALSE; // Make sure this is reset otherwise any
- //line errors will cause io to terminate
- m_dcb.fErrorChar = FALSE;
- m_dcb.fParity = FALSE;
- // m_dcb.fDsrSensitivity = TRUE;
- m_dcb.fNull = FALSE;
- if ( !SetCommState( m_hPort, &m_dcb) )
- {
- sErr.assign( "Error in SetCommState" );
- CloseHandle( m_hPort );
- return false;
- }
- COMMTIMEOUTS cto;
- //cto.ReadIntervalTimeout = 10;
- //cto.ReadIntervalTimeout = (m_BaudRate / 10)
- cto.ReadIntervalTimeout = 2;
- cto.ReadTotalTimeoutMultiplier = 0;
- cto.ReadTotalTimeoutConstant = 0;
- cto.WriteTotalTimeoutMultiplier = 5;
- cto.WriteTotalTimeoutConstant = 5000;
- if( !SetCommTimeouts( m_hPort, &cto ) )
- {
- sErr.assign( "Error in SetCommTimeouts" );
- CloseHandle( m_hPort );
- return false;
- }
- COMSTAT cs = {0};
- DWORD dwErrs = 0;
- if( !ClearCommError( m_hPort, &dwErrs, &cs ) )
- {
- sErr.assign( "Error in ClearCommError" );
- CloseHandle( m_hPort );
- return false;
- }
- //DWORD dwMask = EV_CTS | EV_ERR | EV_DSR | EV_RLSD | EV_BREAK | EV_RING;
- DWORD dwMask = EV_ERR;
- if (!SetCommMask(m_hPort, dwMask))
- {
- sErr.assign( "Error in SetCommMask!" );
- CloseHandle( m_hPort );
- return false;
- }
- OpenDebugLog();
-
- DoWaitCommEventLoop();
- DoReadLoop();
- OnConnect(); //-- moved to OnWaitTimeout to let the read buffer flush
- return true;
- }
- else
- {
- // could not open port
- char szError[256];
- szError[0] = ' ';
- GetLastErrorText( szError, 255 );
- sErr.assign( szError );
- }
- return false;
- }
- //////////////////////////////////////////////////////////////////////
- // DisconnectPort
- //////////////////////////////////////////////////////////////////////
- void CCommPort::DisconnectPort( void )
- {
- PurgeComm( m_hPort, PURGE_RXABORT | PURGE_RXCLEAR |
- PURGE_TXABORT | PURGE_TXCLEAR );
- CloseHandle( m_hPort );
- m_hPort = NULL;
- CloseDebugLog();
- }
- //////////////////////////////////////////////////////////////////////
- // DoRead
- //////////////////////////////////////////////////////////////////////
- bool CCommPort::DoRead( void )
- {
- BOOL bRet;
- m_BytesRead = 0;
- bRet = ReadFile( m_hPort, m_szReadBuff, READBUF_SIZE, &m_BytesRead, &m_ReadOverlapped );
- if( bRet )
- {
- return true;
- }
- else
- {
- DWORD dwLastError = GetLastError();
- // LastError was ERROR_IO_PENDING, as expected.
- if (dwLastError != ERROR_IO_PENDING)
- {
- OutputDebugString("Unexpected error.n");
- }
- // 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");
- }
- return false;
- }
- }
- void CCommPort::FillWriteQueue( char* szChars, unsigned long nBytes, bool bCrLf )
- {
- bool bAddedCrLf = false;
- while( nBytes > 0 )
- {
- int nbuflen = MAX_WRITE;
- if( nbuflen > nBytes )
- {
- nbuflen = nBytes;
- }
- CWriteBuffer *buf = new CWriteBuffer;
- memcpy( buf->Buffer, szChars, nbuflen );
- buf->Bytes = nbuflen;
- if( bCrLf && (nbuflen < MAX_WRITE) )
- {
- // must be last one
- buf->Buffer[buf->Bytes++] = 'r';
- bAddedCrLf = true;
- }
- m_WriteQueue.push_back( buf );
- szChars += nbuflen;
- nBytes -= nbuflen;
- }
- if( bCrLf && !bAddedCrLf)
- {
- CWriteBuffer *buf = new CWriteBuffer;
- buf->Buffer[0] = 'r';
- buf->Bytes = 1;
- m_WriteQueue.push_back( buf );
- }
- }
- bool CCommPort::WritePacket( char* szChars, unsigned long nBytes )
- {
- m_WriteOverlapped.Offset = 0;
- m_WriteOverlapped.OffsetHigh = 0;
-
- if( WriteFile( m_hPort, szChars, nBytes,
- &m_BytesWritten, &m_WriteOverlapped ) )
- {
- //char szMsg[80];
- //wsprintf( szMsg, "Wrote %d bytes 2n", m_BytesWritten );
- //OutputDebugString( szMsg );
-
- ResetEvent( m_WriteOverlapped.hEvent );
- OnWrite();
- if( m_bDebugLog )
- {
- WriteDebugLog( false );
- }
- }
- else
- {
- //char szMsg[80];
- //wsprintf( szMsg, "Queued up %d bytesn", nBytes );
- //OutputDebugString( szMsg );
- m_bWriteInProgress = true;
-
- DWORD dwLastError = GetLastError();
-
- // LastError was ERROR_IO_PENDING, as expected.
- if (dwLastError != ERROR_IO_PENDING)
- {
- OutputDebugString("Unexpected error.n");
- }
-
- // 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");
- }
-
- return false;
- }
- return true;
- }
- //////////////////////////////////////////////////////////////////////
- // DoWrite
- //////////////////////////////////////////////////////////////////////
- bool CCommPort::DoWrite( char* szChars, unsigned long nBytes, bool bCrLf )
- {
- if( m_bWriteInProgress )
- {
- FillWriteQueue( szChars, nBytes, bCrLf );
- }
- else
- {
- if( nBytes > MAX_WRITE )
- {
- FillWriteQueue( szChars, nBytes, bCrLf );
- DoWriteLoop();
- }
- else
- {
- memcpy( m_szWriteBuff, szChars, nBytes );
- if( bCrLf )
- {
- m_szWriteBuff[nBytes++] = 'r';
- //m_szWriteBuff[nBytes++] = 'n';
- }
- if( WritePacket( m_szWriteBuff, nBytes ) )
- {
- //char szMsg[80];
- //wsprintf( szMsg, "Sent %d bytes 3n", nBytes );
- //OutputDebugString( szMsg );
- }
- }
- }
- return true;
- }
- //////////////////////////////////////////////////////////////////////
- // DoRead
- //////////////////////////////////////////////////////////////////////
- bool CCommPort::DoWaitCommEvent( void )
- {
- BOOL bRet;
- m_BytesRead = 0;
- bRet = WaitCommEvent( m_hPort, &m_CommEvent, &m_CommEventOverlapped );
- if( bRet )
- {
- return true;
- }
- else
- {
- DWORD dwLastError = GetLastError();
- // LastError was ERROR_IO_PENDING, as expected.
- if (dwLastError != ERROR_IO_PENDING)
- {
- OutputDebugString("Unexpected error.n");
- }
- // 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");
- }
- return false;
- }
- }
- //////////////////////////////////////////////////////////////////////
- // DoReadLoop
- //////////////////////////////////////////////////////////////////////
- void CCommPort::DoReadLoop( void )
- {
- int i;
- for( i = 0; i < 100; i++ )
- {
- if( m_hPort == NULL )
- break;
- if( DoRead() )
- {
- m_szReadBuff[m_BytesRead] = ' ';
-
- if( m_bDebugLog )
- {
- WriteDebugLog( true );
- }
- OnRead();
- }
- else
- {
- break;
- }
- }
-
- 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 2n", (*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] = ' ';
- 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 staten" );
- }
- if( m_CommEvent & EV_ERR )
- {
- OutputDebugString( "CCommPort::OnCommEvent: Errorn" );
- }
-
- if( m_CommEvent & EV_DSR )
- {
- OutputDebugString( "CCommPort::OnCommEvent: DSR changed staten" );
- }
- if( m_CommEvent & EV_RLSD )
- {
- OutputDebugString( "CCommPort::OnCommEvent: RLSD changed staten" );
- }
- if( m_CommEvent & EV_BREAK )
- {
- OutputDebugString( "CCommPort::OnCommEvent: Break detectedn" );
- }
- }
- //////////////////////////////////////////////////////////////////////
- // OnConnect
- //////////////////////////////////////////////////////////////////////
- void CCommPort::OnConnect(void)
- {
- // OutputDebugString( "CCommPort::OnConnectn" );
- }
- //////////////////////////////////////////////////////////////////////
- // OnDisconnect - return true if no command sent
- //////////////////////////////////////////////////////////////////////
- bool CCommPort::OnDisconnect(void)
- {
- // OutputDebugString( "CCommPort::OnDisconnectn" );
- return true;
- }
- //////////////////////////////////////////////////////////////////////
- // OnRead
- //////////////////////////////////////////////////////////////////////
- void CCommPort::OnRead(void)
- {
- // OutputDebugString( "CCommPort::OnReadn" );
- }
- //////////////////////////////////////////////////////////////////////
- // OnReadLine
- //////////////////////////////////////////////////////////////////////
- void CCommPort::OnReadLine(void)
- {
- // OutputDebugString( "CModem::OnReadLinen" );
- }
- //////////////////////////////////////////////////////////////////////
- // OnWrite
- //////////////////////////////////////////////////////////////////////
- void CCommPort::OnWrite(void)
- {
- // OutputDebugString( "CCommPort::OnWriten" );
- }
- //////////////////////////////////////////////////////////////////////
- // 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] = ' ';
- 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 controln" );
- }
- else
- {
- //OutputDebugString( "Disabling software flow controln" );
- }
- }
- }
- 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] = ' ';
- 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;
- }
- }