C++通用串口连接方式(QT版)

C++串口通用连接方式
commport.h

#ifndef COMMPORT_H
#define COMMPORT_H

#include <QObject>
#include <Windows.h>

class QCommPort : public QObject
{
    
    
	Q_OBJECT

public:
	QCommPort(QObject *parent = NULL);
	~QCommPort();

	bool open(const QString &port, int BaudRate, unsigned char DataBit = 8, unsigned char Parity = NOPARITY, unsigned char StopBits = ONESTOPBIT);
	void close();

	bool read(QByteArray &ar);
	bool write(const QByteArray &ar);

private:
	HANDLE m_hCom;
};

#endif // COMMPORT_H

commport.cpp

#include "CommPort.h"
#include <qdebug.h>
#include <qthread.h>

QCommPort::QCommPort(QObject *parent)
	: QObject(parent),m_hCom(INVALID_HANDLE_VALUE)
{
    
    

}

QCommPort::~QCommPort()
{
    
    

}

bool QCommPort::open(const QString &port,int BaudRate, unsigned char DataBit, unsigned char Parity, unsigned char StopBits)
{
    
    
	m_hCom = CreateFileA(port.toStdString().c_str(),
		GENERIC_READ|GENERIC_WRITE,               
		FILE_SHARE_READ,                          
		NULL,
		OPEN_EXISTING,                             
		FILE_ATTRIBUTE_NORMAL,                    
		NULL);
	if (INVALID_HANDLE_VALUE == m_hCom)
	{
    
    
		//qDebug() << "barcode OpenDev Error!";
		return false;
	}

	DCB dcb;
	memset(&dcb,0,sizeof(DCB));

	/*if(!GetCommState(m_hCom, &dcb))
	{
		qDebug() << "GetCommState Error";
		return false;
	}*/

	dcb.BaudRate= BaudRate;       
	dcb.ByteSize= DataBit;
	dcb.Parity= Parity;
	dcb.StopBits= StopBits;
	if(!SetCommState(m_hCom, &dcb))
	{
    
    
		//qDebug() << "SetCommState Error";
		return false;
	}

	COMMTIMEOUTS timeOutis;
	GetCommTimeouts(m_hCom,&timeOutis);
	timeOutis.ReadIntervalTimeout = 500;
	timeOutis.ReadTotalTimeoutConstant = 500;
	timeOutis.ReadTotalTimeoutMultiplier = 0;
	timeOutis.WriteTotalTimeoutConstant = 500;
	timeOutis.WriteTotalTimeoutMultiplier = 0;
	SetCommTimeouts(m_hCom,&timeOutis);

	DWORD dwErrorFlags;
	COMSTAT ComStat;
	ClearCommError(m_hCom, &dwErrorFlags, &ComStat);

	SetupComm(m_hCom, 1024, 1024); 
	PurgeComm(m_hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);
	return true;
}

void QCommPort::close()
{
    
    
	if(INVALID_HANDLE_VALUE == m_hCom)return;

	CloseHandle(m_hCom);
	m_hCom = INVALID_HANDLE_VALUE;
}


bool QCommPort::read(QByteArray &ar)
{
    
    
	if(INVALID_HANDLE_VALUE == m_hCom)return false;

	char readBuffer[512];
	memset(readBuffer, 0, 512);
	DWORD wCount= 500;
	BOOL bReadStat;

	bReadStat = ReadFile(m_hCom, readBuffer, wCount, &wCount, NULL);
	if(wCount > 0)
	{
    
    
		ar.append(readBuffer,wCount);

		return true;
	}

	return false;

}

bool QCommPort::write(const QByteArray &ar)
{
    
    
	if(INVALID_HANDLE_VALUE == m_hCom)return false;

	unsigned long dwBytesWrite;
	COMSTAT ComStat;
	DWORD dwErrorFlags;
	BOOL bWriteStat;
	ClearCommError(m_hCom, &dwErrorFlags, &ComStat);

	dwBytesWrite = 0;
	bWriteStat = WriteFile(m_hCom, ar.data(), ar.size(), &dwBytesWrite, NULL);
	if(!bWriteStat)
	{
    
    
		return false;
	}

	return true;
}

猜你喜欢

转载自blog.csdn.net/Liang_ming_/article/details/131656593