|
本帖最后由 wcl_0308 于 2014-12-2 08:50 编辑
命名为
//////////////////////////////////////////////////////////////////////
OpComm.h
#if !defined(AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_)
#define AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class COpComm
{
public:
// Read the data from the receive buffer of the COM port
BOOL ReadBuffer(unsigned char* pInBuffer, DWORD dwBufferSize);
// write the data to the transmit buffer of the COM port
BOOL WriteBuffer(unsigned char* pOutBuffer, DWORD dwBufferSize);
// Close the COM port
VOID ClosePort();
// Configure the COM Port upon the input parameter,
// Open the COM Port.
BOOL OpenPort(LPTSTR strPortName, // COM port index
DWORD dwBaudRate = 115200, // Baud Rate:115200
BYTE bByteSize = 8, // 8 data bits
BYTE bParity = NOPARITY, // no parity
BYTE bStopBits = ONESTOPBIT); // one stop bit
COpComm();
virtual ~COpComm();
protected:
HANDLE m_hCommPort; // COM port handler
};
#endif // !defined(AFX_OPCOMM_H__82138A00_2098_44B0_8F5F_C96B6BDB8880__INCLUDED_)
OpComm.cpp
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "OpComm.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COpComm::COpComm()
{
m_hCommPort = INVALID_HANDLE_VALUE;
}
COpComm::~COpComm()
{
ClosePort();
}
BOOL COpComm::OpenPort(LPTSTR strPortName, DWORD dwBaudRate,
BYTE bByteSize, BYTE bParity, BYTE bStopBit)
{
BOOL bPortReady = FALSE;
DCB PortDCB;
COMMTIMEOUTS PortTimeOuts;
// If the port was previously open, close it first
ClosePort();
// Open the port and do new settings
m_hCommPort = CreateFile(strPortName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(m_hCommPort == INVALID_HANDLE_VALUE)
return bPortReady;
bPortReady = GetCommState(m_hCommPort, &PortDCB);
if(!bPortReady)
return bPortReady;
PortDCB.BaudRate = dwBaudRate;
PortDCB.ByteSize = bByteSize;
PortDCB.fParity = FALSE; //Disable parity checking
PortDCB.StopBits = bStopBit;
PortDCB.fAbortOnError = TRUE;
bPortReady = SetCommState(m_hCommPort, &PortDCB);
if(!bPortReady)
return bPortReady;
bPortReady = GetCommTimeouts(m_hCommPort, &PortTimeOuts);
if(!bPortReady)
return bPortReady;
PortTimeOuts.ReadIntervalTimeout = 50;
PortTimeOuts.ReadTotalTimeoutConstant = 50;
PortTimeOuts.ReadTotalTimeoutMultiplier = 10;
PortTimeOuts.WriteTotalTimeoutConstant = 50;
PortTimeOuts.WriteTotalTimeoutMultiplier = 10;
bPortReady = SetCommTimeouts(m_hCommPort, &PortTimeOuts);
if(!bPortReady)
return bPortReady;
bPortReady = SetupComm(m_hCommPort, 1024, 1024);
if(!bPortReady)
return bPortReady;
bPortReady = PurgeComm(m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR);
return bPortReady;
}
VOID COpComm::ClosePort()
{
if(m_hCommPort != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hCommPort);
m_hCommPort = INVALID_HANDLE_VALUE;
}
}
BOOL COpComm::WriteBuffer(unsigned char* pOutBuffer, DWORD dwBufferSize)
{
DWORD dwBytesWritten;
if(m_hCommPort == INVALID_HANDLE_VALUE)
return FALSE;
// Clear the TX and RX buffer Error information
if (!PurgeComm(m_hCommPort, PURGE_TXCLEAR | PURGE_RXCLEAR))
{
return FALSE;
}
WriteFile(m_hCommPort, pOutBuffer, dwBufferSize, &dwBytesWritten, NULL);
if(dwBytesWritten != dwBufferSize)
return FALSE;
return TRUE;
}
BOOL COpComm::ReadBuffer(unsigned char* pInBuffer, DWORD dwBufferSize)
{
DWORD dwBytesRead;
if(m_hCommPort == INVALID_HANDLE_VALUE)
return FALSE;
ReadFile(m_hCommPort, pInBuffer, dwBufferSize, &dwBytesRead, NULL);
// if the received buffer is empty, return FALSE
if(dwBytesRead == 0)
return FALSE;
// if received data length is not equal with the want data length
// if(dwBytesRead != dwBufferSize)
// return FALSE;
return TRUE;
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|