ComSerialPort.h
/*_________________________串口________________________________*/
class Com_SerialPort
{
public:
Com_SerialPort();
Com_SerialPort(int port, int baudRate, int byteSize, int parity, int stopBits);
~Com_SerialPort();
public:
bool Connect(bool bMutex = false) override;
void DisConnect(bool bMutex = false) override;
bool clearBuf_c();
bool recvBuf_c(char* pStr, int& len, int waitTimeMS = 0) override;
bool sendBuf_c(char const *pStr, const int& len) override;
private:
BOOL setReadTime(DWORD time);
private:
HANDLE m_hCom;
DCB m_dcb;
COMMTIMEOUTS m_timeOuts;
};
ComSerialPort.cpp
/*_________________________串口________________________________*/
Com_SerialPort::Com_SerialPort()
{
m_dcb.BaudRate = 9600; //波特率为9600
m_dcb.ByteSize = 8; //每个字节有8位
m_dcb.Parity = NOPARITY; //无奇偶校验位
m_dcb.StopBits = ONESTOPBIT; //1个停止位
m_typeName = "串口";
//设定读超时
m_timeOuts.ReadIntervalTimeout = 100;//读间隔超时
m_timeOuts.ReadTotalTimeoutMultiplier = 0;//读时间系数
m_timeOuts.ReadTotalTimeoutConstant = 1000;//读时间常量
//设定写超时
m_timeOuts.WriteTotalTimeoutMultiplier = 500; // 写时间系数
m_timeOuts.WriteTotalTimeoutConstant = 2000;//写时间常量
}
Com_SerialPort::Com_SerialPort(int port, int baudRate, int byteSize, int parity, int stopBits)
{
m_Port = port;
m_dcb.BaudRate = baudRate; //波特率为9600;
m_dcb.ByteSize = byteSize; //每个字节有8位;
m_dcb.Parity = parity; //无奇偶校验位;
m_dcb.StopBits = stopBits; //1个停止位;
m_typeName = "串口";
//设定读超时
m_timeOuts.ReadIntervalTimeout = 100;//读间隔超时
m_timeOuts.ReadTotalTimeoutMultiplier = 0;//读时间系数
m_timeOuts.ReadTotalTimeoutConstant = 1000;//读时间常量
//设定写超时
m_timeOuts.WriteTotalTimeoutMultiplier = 500; // 写时间系数
m_timeOuts.WriteTotalTimeoutConstant = 2000;//写时间常量
}
Com_SerialPort::~Com_SerialPort()
{
}
bool Com_SerialPort::Connect(bool bMutex)
{
QMutexLocker locker(&m_Mutex);
DisConnect(false);
char str_c[256];
memset(str_c, '\0', 256);
snprintf(str_c, sizeof(str_c), "COM%d", m_Port);
std::string ComStr(str_c);
WCHAR wComName[128];
memset(wComName, '\0', sizeof(wComName));
MultiByteToWideChar(CP_ACP, 0, ComStr.c_str(), -1, wComName, sizeof(wComName) / sizeof(wComName[0]));
m_hCom = CreateFile(wComName,//COM1口
GENERIC_READ | GENERIC_WRITE, //允许读和写;
0, //独占方式;
NULL,
OPEN_EXISTING, //打开而不是创建;
0, //同步方式;
NULL);
//if (hCom1 == (HANDLE)-1)
if (m_hCom == INVALID_HANDLE_VALUE)
{
printf("打开COM失败!\n");
m_bConnectOK = false;
return false;
}
SetupComm(m_hCom, CHAR_LEN, CHAR_LEN); //输入缓冲区和输出缓冲区的大小都是1024
//设定读超时
BOOL RTN = SetCommTimeouts(m_hCom, &m_timeOuts); //设置超时
DCB dcb;
GetCommState(m_hCom, &dcb);
dcb.BaudRate = m_dcb.BaudRate; //波特率为9600
dcb.ByteSize = m_dcb.ByteSize; //每个字节有8位
dcb.Parity = m_dcb.Parity; //无奇偶校验位
dcb.StopBits = m_dcb.StopBits; //1个停止位
SetCommState(m_hCom, &dcb);
m_bConnectOK = true;
return true;
}
void Com_SerialPort::DisConnect(bool bMutex)
{
if (bMutex)m_Mutex.lock();
if (m_hCom != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hCom);
m_hCom = INVALID_HANDLE_VALUE;
m_bConnectOK = false;
}
if (bMutex)m_Mutex.unlock();
}
bool Com_SerialPort::clearBuf_c()
{
QMutexLocker locker(&m_Mutex);
setReadTime(200);
DWORD initTime = GetTickCount();
do
{
DWORD readsize = 0;
ReadFile(m_hCom, m_recvBuffer, CHAR_LEN, &readsize, NULL);
if (readsize <= 0)
return true;
if (abs(int(GetTickCount() - initTime)) > 1000)
return false;
} while (1);
return false;
}
bool Com_SerialPort::recvBuf_c(char * pStr, int & len, int waitTimeMS)
{
QMutexLocker locker(&m_Mutex);
DWORD readsize = 0;
setReadTime(waitTimeMS);
ReadFile(m_hCom, pStr, CHAR_LEN, &readsize, NULL);
len = readsize;
return readsize>0;
}
bool Com_SerialPort::sendBuf_c(char const * pStr, const int & len)
{
QMutexLocker locker(&m_Mutex);
BOOL rtn = WriteFile(m_hCom, pStr, len, NULL, NULL);
return rtn != 0;
}
BOOL Com_SerialPort::setReadTime(DWORD time)
{
if (time != m_timeOuts.ReadTotalTimeoutConstant)
{
//读时间常量
m_timeOuts.ReadTotalTimeoutConstant = time;
return SetCommTimeouts(m_hCom, &m_timeOuts); //设置超时
}
return 0;
}