SerialCom.cpp
上传用户:sarrow1992
上传日期:2022-07-29
资源大小:2k
文件大小:4k
- #include "stdafx.h"
- #include "SerialCom.h"
- CSerial::CSerial()
- {
- m_hComDev=NULL;
- m_bOpened=FALSE;
- memset(dataRx,' ',50);
- }
- CSerial::~CSerial()
- {
- m_hComDev=NULL;
- m_bOpened=FALSE;
- }
- BOOL CSerial::Open(int nPort, int nBaud)
- {
- if( m_bOpened == TRUE)
- {
- MessageBox(0,"The Com Port has been opened.","",MB_OK);
- return( TRUE );
- }
- char szPort[15];
- DCB dcb;
- wsprintf( szPort, "COM%d", nPort );
- m_hComDev=CreateFile( "COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
- if(m_hComDev == INVALID_HANDLE_VALUE)
- {
- MessageBox(0,"The Com Port has been occupied!","",MB_OK);
- return FALSE;
- }
- memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
- memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
- COMMTIMEOUTS CommTimeOuts;
- CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
- CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
- CommTimeOuts.ReadTotalTimeoutConstant = 0;
- CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
- CommTimeOuts.WriteTotalTimeoutConstant = 5000;
- SetCommTimeouts(m_hComDev, &CommTimeOuts);
- m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL);
- m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL);
- dcb.DCBlength = sizeof( DCB );
- GetCommState( m_hComDev, &dcb );
- dcb.BaudRate = nBaud;
- dcb.ByteSize = 8;
- dcb.Parity = 0;
- if( !SetCommState( m_hComDev, &dcb) || !SetupComm( m_hComDev, 512,512) ||
- m_OverlappedRead.hEvent == NULL || m_OverlappedWrite.hEvent == NULL )
- {
- DWORD dwError = GetLastError();
- if( m_OverlappedRead.hEvent != NULL) CloseHandle( m_OverlappedRead.hEvent);
- if( m_OverlappedWrite.hEvent != NULL) CloseHandle( m_OverlappedWrite.hEvent);
- CloseHandle( m_hComDev );
- return FALSE;
- }
- m_bOpened = TRUE;
- return m_bOpened;
- }
- //***************************************************************************
- int CSerial::InBufferCount( void )
- {
- if( !m_bOpened || m_hComDev == NULL ) return( 0 );
- DWORD dwErrorFlags;
- COMSTAT ComStat;
- ClearCommError( m_hComDev, &dwErrorFlags, &ComStat );
- return (int)ComStat.cbInQue;
- }
- //***************************************************************************
- DWORD CSerial::ReadData( void *buffer, DWORD dwBytesRead)
- {
- if( !m_bOpened || m_hComDev == NULL ) return 0;
- BOOL bReadStatus;
- DWORD dwErrorFlags;
- COMSTAT ComStat;
- ClearCommError( m_hComDev, &dwErrorFlags, &ComStat );
- if( !ComStat.cbInQue ) return 0;
- dwBytesRead = min(dwBytesRead,(DWORD) ComStat.cbInQue);
- // SetFilePointer(m_hComDev,NULL,NULL,FILE_BEGIN);
- bReadStatus = ReadFile( m_hComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
- if( !bReadStatus )
- {
- if( GetLastError() == ERROR_IO_PENDING )
- {
- WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
- return dwBytesRead;
- }
- return 0;
- }
- return dwBytesRead;
- }
- //****************************************************************************
- DWORD CSerial::SendData( const char *buffer, DWORD dwBytesWritten)
- {
- if( !m_bOpened || m_hComDev == NULL ) return( 0 );
- BOOL bWriteStat;
- // SetFilePointer(m_hComDev,NULL,NULL,FILE_BEGIN);
- bWriteStat = WriteFile( m_hComDev, buffer, dwBytesWritten, &dwBytesWritten, &m_OverlappedWrite );
- if( !bWriteStat)
- {
- if ( GetLastError() == ERROR_IO_PENDING )
- {
- WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 );
- return dwBytesWritten;
- }
- return 0;
- }
- return dwBytesWritten;
- }
- //****************************************************************************
- BOOL CSerial::Close(void)
- {
- m_bOpened = FALSE;
- return CloseHandle(m_hComDev);
- }