资源简介

C++串口读写操作封装类,简单易用,注释详细

资源截图

代码片段和文件信息

// Serial.cpp

#include “stdafx.h“
#include “Serial.h“

CSerial::CSerial()
{

memset( &m_OverlappedRead 0 sizeof( OVERLAPPED ) );
  memset( &m_OverlappedWrite 0 sizeof( OVERLAPPED ) );
m_hIDComDev = NULL;
m_bOpened = FALSE;
m_nCommNo = 0;

}

CSerial::~CSerial()
{

Close();
// TRACE(“Close Com%d\n“ m_nCommNo);
}

BOOL CSerial::Open( UINT nPort DWORD dwBaud BYTE byByteSize BYTE byParity BYTE byStopBits)
{

if( m_bOpened ) return TRUE;

char szPort[15];

m_nCommNo = nPort;
//wsprintf( szPort “COM%d“ m_nCommNo );
wsprintf( szPort “\\\\.\\COM%d“ m_nCommNo ); //这种格式才能打开大于9的串口!2003.12
m_hIDComDev = CreateFile( szPort GENERIC_READ | GENERIC_WRITE 0 NULL OPEN_EXISTING FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED NULL );
if( m_hIDComDev == NULL )
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_hIDComDev &CommTimeOuts );

m_OverlappedRead.hEvent = CreateEvent( NULL TRUE FALSE NULL );
if (m_OverlappedRead.hEvent == NULL)
{
return FALSE;
}

m_OverlappedWrite.hEvent = CreateEvent( NULL TRUE FALSE NULL );
if (m_OverlappedWrite.hEvent == NULL)
{
return FALSE;
}

DCB dcb;
ZeroMemory(&dcb sizeof(DCB));
dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev &dcb );
dcb.BaudRate = dwBaud;
dcb.ByteSize = byByteSize;
dcb.Parity  = byParity;
dcb.StopBits = byStopBits;

//以下三行用于设置流控制为无
dcb.fOutX  = FALSE;
dcb.fRtsControl  = FALSE;
dcb.fOutxCtsFlow = FALSE;

if (!SetCommState( m_hIDComDev &dcb ))
{
DWORD dwError = GetLastError();
return FALSE;
}
if (!SetupComm( m_hIDComDev 2000 2000 ))
{
DWORD dwError = GetLastError();
return FALSE;
}

m_bOpened = TRUE;

SetCommMask(m_hIDComDevEV_RXCHAR|EV_TXEMPTY);

return m_bOpened;
}

BOOL CSerial::Close( void )
{

// if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

if( m_OverlappedRead.hEvent)
{
CloseHandle( m_OverlappedRead.hEvent );
m_OverlappedRead.hEvent = NULL;
}
if( m_OverlappedWrite.hEvent)
{
CloseHandle( m_OverlappedWrite.hEvent );
m_OverlappedWrite.hEvent = NULL;
}

if (m_hIDComDev)
CloseHandle( m_hIDComDev );
m_bOpened = FALSE;
m_hIDComDev = NULL;

return TRUE;
}

BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
BOOL bWriteStat;
DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev (LPSTR) &ucByte 1 &dwBytesWritten &m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleobject( m_OverlappedWrite.hEvent 1000 ) ) dwBytesWritten = 0;
else{
GetOverlappedResult( m_h

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6886  2006-03-08 10:12  Serial\Serial.cpp

     文件        983  2006-03-06 10:56  Serial\Serial.h

     目录          0  2014-04-30 20:53  Serial

----------- ---------  ---------- -----  ----

                 7869                    3


评论

共有 条评论