00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013
00014
00033 #include "stdafx.h"
00034 #include "Serial.h"
00035
00036 CSerial::CSerial()
00037 {
00038
00039 memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
00040 memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
00041 m_hIDComDev = NULL;
00042 m_bOpened = FALSE;
00043
00044 }
00045
00046 CSerial::~CSerial()
00047 {
00048
00049 Close();
00050
00051 }
00052
00053 BOOL CSerial::Open( int nPort, int nBaud )
00054 {
00055
00056 if( m_bOpened ) return( TRUE );
00057
00058 char szPort[15];
00059 char szComParams[50];
00060 DCB dcb;
00061
00062 wsprintf( szPort, "COM%d", nPort );
00063 m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
00064 if( m_hIDComDev == NULL ) return( FALSE );
00065
00066 memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
00067 memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
00068
00069 COMMTIMEOUTS CommTimeOuts;
00070 CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
00071 CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
00072 CommTimeOuts.ReadTotalTimeoutConstant = 0;
00073 CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
00074 CommTimeOuts.WriteTotalTimeoutConstant = 5000;
00075 SetCommTimeouts( m_hIDComDev, &CommTimeOuts );
00076
00077 wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );
00078
00079 m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
00080 m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
00081
00082 dcb.DCBlength = sizeof( DCB );
00083 GetCommState( m_hIDComDev, &dcb );
00084 dcb.BaudRate = nBaud;
00085 dcb.ByteSize = 8;
00086 unsigned char ucSet;
00087 ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
00088 ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
00089 ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
00090 if( !SetCommState( m_hIDComDev, &dcb ) ||
00091 !SetupComm( m_hIDComDev, 10000, 10000 ) ||
00092 m_OverlappedRead.hEvent == NULL ||
00093 m_OverlappedWrite.hEvent == NULL ){
00094 DWORD dwError = GetLastError();
00095 if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
00096 if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
00097 CloseHandle( m_hIDComDev );
00098 return( FALSE );
00099 }
00100
00101 m_bOpened = TRUE;
00102
00103 return( m_bOpened );
00104
00105 }
00106
00107 BOOL CSerial::Close( void )
00108 {
00109
00110 if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );
00111
00112 if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
00113 if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
00114 CloseHandle( m_hIDComDev );
00115 m_bOpened = FALSE;
00116 m_hIDComDev = NULL;
00117
00118 return( TRUE );
00119
00120 }
00121
00122 BOOL CSerial::WriteCommByte( unsigned char ucByte )
00123 {
00124 BOOL bWriteStat;
00125 DWORD dwBytesWritten;
00126
00127 bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite );
00128 if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
00129 if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0;
00130 else{
00131 GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE );
00132 m_OverlappedWrite.Offset += dwBytesWritten;
00133 }
00134 }
00135
00136 return( TRUE );
00137
00138 }
00139
00140 int CSerial::SendData( const char *buffer, int size )
00141 {
00142
00143 if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
00144
00145 DWORD dwBytesWritten = 0;
00146 int i;
00147 for( i=0; i<size; i++ ){
00148 WriteCommByte( buffer[i] );
00149 dwBytesWritten++;
00150 }
00151
00152 return( (int) dwBytesWritten );
00153
00154 }
00155
00156 int CSerial::ReadDataWaiting( void )
00157 {
00158
00159 if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
00160
00161 DWORD dwErrorFlags;
00162 COMSTAT ComStat;
00163
00164 ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
00165
00166 return( (int) ComStat.cbInQue );
00167
00168 }
00169
00170 int CSerial::ReadData( void *buffer, int limit )
00171 {
00172
00173 if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
00174
00175 BOOL bReadStatus;
00176 DWORD dwBytesRead, dwErrorFlags;
00177 COMSTAT ComStat;
00178
00179 ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
00180 if( !ComStat.cbInQue ) return( 0 );
00181
00182 dwBytesRead = (DWORD) ComStat.cbInQue;
00183 if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;
00184
00185 bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
00186 if( !bReadStatus ){
00187 if( GetLastError() == ERROR_IO_PENDING ){
00188 WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
00189 return( (int) dwBytesRead );
00190 }
00191 return( 0 );
00192 }
00193
00194 return( (int) dwBytesRead );
00195
00196 }
00197