📄 serial.cpp
字号:
// Serial.cpp
#include "stdafx.h"
#include "Serial.h"
#include "Gps.h"
#include "Winbase.h"
CSerial::CSerial()
{
m_hIDComDev = NULL;
m_bOpened = FALSE;
m_bNoRead = FALSE;
}
CSerial::~CSerial()
{
if(m_hIDComDev != INVALID_HANDLE_VALUE)
Close();
}
BOOL CSerial::Open( int nPort, int nBaud) //open serial port
{
DWORD dwError,dwThreadID;
if( m_bOpened )
return( TRUE );
TCHAR szPort[15];
DCB dcb;
wsprintf( szPort, _T("COM%d:"), nPort );
//open serial device
m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
if( m_hIDComDev ==INVALID_HANDLE_VALUE )
{
MessageBox (NULL, TEXT("unable to create serial device"), TEXT("Error"), MB_OK);
return( FALSE );
}
//configure timeout
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts (m_hIDComDev, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 400;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
if (!SetCommTimeouts (m_hIDComDev, &CommTimeOuts))
{
MessageBox (NULL, TEXT("Unable to set the time-out parameters"), TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
//point port detect event
SetCommMask (m_hIDComDev, EV_RXCHAR);
//configure serial device
dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev, &dcb );
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
//sleep to let dcb be effect
Sleep(1000);
if( !SetCommState( m_hIDComDev, &dcb ) ||
!SetupComm( m_hIDComDev, COM_MAX_INBUFSIZE, COM_MAX_OUTBUFSIZE ))
{
MessageBox (NULL, TEXT("Unable to configure the serial port"), TEXT("Error"), MB_OK);
dwError = GetLastError();
CloseHandle( m_hIDComDev );
return( FALSE );
}
//Clear the input and output buffer
PurgeComm(m_hIDComDev,PURGE_TXCLEAR|PURGE_RXCLEAR);
//create read port thread
if( !m_bNoRead )
{
if (hReadThread = CreateThread (NULL, 0, ReadPortThread, this , 0, &dwThreadID))
{
// SetThreadPriority( hReadThread, THREAD_PRIORITY_HIGHEST );
// CeSetThreadPriority(hReadThread, 252);
CeSetThreadPriority(hReadThread, 0);
// CeSetThreadQuantum(hReadThread, 0);
ResumeThread( hReadThread );
}
else
{
MessageBox (NULL, TEXT("Unable to create the read thread"), TEXT("Error"), MB_OK);
dwError = GetLastError ();
return FALSE;
}
}
m_bOpened = TRUE;
return( m_bOpened );
}
BOOL CSerial::Close( void ) //close serial port
{
if( !m_bOpened || m_hIDComDev == NULL )
return( TRUE );
m_bOpened = FALSE;
//end waiting for WaitCommEvent
SetCommMask(m_hIDComDev,0);
//end read port thread
if(hReadThread)
{
TerminateThread(hReadThread,0);
CloseHandle(hReadThread);
}
EscapeCommFunction(m_hIDComDev,CLRDTR);
PurgeComm(m_hIDComDev,PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle( m_hIDComDev );
m_hIDComDev = NULL;
return( TRUE );
}
BOOL CSerial::WriteCommByte( unsigned char ucByte ) //serial port sends data by byte
{
BOOL bWriteStat;
DWORD dwBytesWritten;
bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, NULL);
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) )
{
//MessageBox(NULL,TEXT("Can't Write String to Comm"),TEXT("Error"),MB_OK);
dwBytesWritten = 0;
}
return( dwBytesWritten!=0 );
}
int CSerial::SendData( const char *buffer, int size ) //serial port sends data
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
DWORD dwBytesWritten = 0;
#if 1
int i;
for( i=0; i<size; i++ )
{
if(WriteCommByte( buffer[i] ))
{
dwBytesWritten++;
}
}
#else
//write to serial port in one shot
BOOL bWriteStat;
bWriteStat = WriteFile( m_hIDComDev, (LPSTR) buffer, size, &dwBytesWritten, NULL);
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) )
{
//MessageBox(NULL,TEXT("Can't Write String to Comm"),TEXT("Error"),MB_OK);
dwBytesWritten = 0;
}
#endif
return( (int) dwBytesWritten );
}
void CSerial::SetGps(CGps *pGps)
{
m_pGps = pGps;
}
#if 0
DWORD WINAPI ReadPortThread(LPVOID lpvoid) //Read Port Thread
{
DWORD dwCommModemStatus;
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
CSerial * pSerial = (CSerial *)lpvoid;
while (pSerial->m_hIDComDev!=INVALID_HANDLE_VALUE)
{
Sleep(100);
SetCommMask( pSerial->m_hIDComDev, EV_RXCHAR );
WaitCommEvent( pSerial->m_hIDComDev, &dwCommModemStatus, 0 );
if (dwCommModemStatus & EV_RXCHAR)
{
ClearCommError( pSerial->m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) continue;
dwBytesRead = (DWORD) ComStat.cbInQue;
if(dwBytesRead>0)
{
char *buf = new char[COM_MAX_INBUFSIZE+1];
memset( buf, 0, COM_MAX_INBUFSIZE + 1 );
bReadStatus=ReadFile(pSerial->m_hIDComDev,buf,dwBytesRead+100,&dwBytesRead,NULL);
if(!bReadStatus)
{
//MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
}
else
{
pSerial->m_pGps->OnReceived( (unsigned char *)buf,(int)dwBytesRead );
}
}
GetCommModemStatus (pSerial->m_hIDComDev, &dwCommModemStatus);
}
else
{
//MessageBox(pSerial->m_pGps->GetViewHandle(), _T("Other event occured"), _T(""), MB_OK);
}
}
return 0;
}
#else
DWORD WINAPI ReadPortThread(LPVOID lpvoid) //Read Port Thread
{
DWORD dwCommModemStatus;
BOOL bReadStatus;
DWORD dwBytesRead;
CSerial * pSerial = (CSerial *)lpvoid;
while (pSerial->m_hIDComDev!=INVALID_HANDLE_VALUE)
{
//pSerial->m_pGps->m_pLog->Write("Before WaitCommEvent");
WaitCommEvent( pSerial->m_hIDComDev, &dwCommModemStatus, 0 );
SetCommMask( pSerial->m_hIDComDev, EV_RXCHAR );
if (dwCommModemStatus & EV_RXCHAR)
{
char *buf = new char[COM_MAX_INBUFSIZE+1];
memset( buf, 0, COM_MAX_INBUFSIZE + 1 );
//pSerial->m_pGps->m_pLog->Write("Before ReadFile");
bReadStatus=ReadFile(pSerial->m_hIDComDev,buf,COM_MAX_INBUFSIZE,&dwBytesRead,NULL);
//pSerial->m_pGps->m_pLog->Write("AfterReadFile");
if( dwBytesRead > 0 )
{
pSerial->m_pGps->OnReceived( (unsigned char *)buf, dwBytesRead );
}
else
{
delete buf;
}
}
}
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -