简述:
????????在Qt5以上提供了QtSerialPort模块,方便编程人员快速的开发应用串口的应用程序。????????
????????QtSerialPort模块中提供了两个C++类,分别是QSerialPort 和QSerialPortInfo。
????????????????QSerialPort 类是Qt5封装的串口类,可与串口进行通信,提供了操作串口的各种接口。
????????????????QSerialPortInfo类是一个辅助类,可以提供计算机中可用串口的各种信息。如可用的串口名称,描述,制造商,序列号,串口16位产品编号等。利用QSerialPortInfo提供的可用串口信息,可设置串口波特率,并打开需要的串口进行通信。
????????使用Qt5进行串口通信大致步骤为:配置串口参数->打开串口->收发数据。
????????要使用QtSerialPort模块,需要在工程文件.pro文件或.pri中增加语句:
????????????????QT += serialport
????????Qt版本:5.12.8
1.QSerialPortInfo类? ?
????????列举出电脑上全部的串口设备,Cpp?文件如下:
#include <QCoreApplication>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
qDebug() << "Serial Number: " << info.serialNumber();
qDebug() << "System Location: " << info.systemLocation();
}
return a.exec();
}
????????显示的结果如下:
Name : "COM17"
Description : "USB-SERIAL CH340"
Manufacturer: "wch.cn"
Serial Number: ""
System Location: "\\\\.\\COM17"
Name : "COM1"
Description : "通信端口"
Manufacturer: "(标准端口类型)"
Serial Number: ""
System Location: "\\\\.\\COM1"
Name : "COM16"
Description : "Prolific USB-to-Serial Comm Port"
Manufacturer: "Prolific"
Serial Number: "A400G3UXA"
System Location: "\\\\.\\COM16"
Name : "COM11"
Description : "Prolific USB-to-Serial Comm Port"
Manufacturer: "Prolific"
Serial Number: ""
System Location: "\\\\.\\COM11"
????????若USB串口每次插在不同的USB口上时获得的串口名称可能有变化,这时可以利用串口的序列号,指定程序使用某一个确定的串口。
#include <QCoreApplication>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSerialPortInfo com_info;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
if( info.serialNumber() == "A400G3UXA" )
{
com_info = info;
break;
}
}
qDebug() << "Name : " << com_info.portName();
qDebug() << "Description : " << com_info.description();
qDebug() << "serialNumber: " << com_info.serialNumber();
return a.exec();
}
2.QSerialPort类
????????QSerialPort 类提供了操作串口的各种接口。
? ? ? ? 头文件:
class MySerial : public QObject
{
Q_OBJECT
public:
MySerial(QString portName = "ttyUSB1", QObject *parent = nullptr);
~MySerial();
bool WriteToSerial(QByteArray cmd);
QByteArray ReadFromSerial();
bool SerialIsOpen() { return m_SerialPort->isOpen(); }
void closeSerial();
signals:
void sig_SendTipInfo(bool result, QString info);
void sig_SendReadData(QByteArray data);
private:
QSerialPort *m_SerialPort = nullptr;
};
? ? ? ? cpp文件:
MySerial::MySerial(QString portName, QObject *parent) :
QObject(parent)
{
m_SerialPort = new QSerialPort();
m_SerialPort->setPortName(portName); //设置串行端口的名称
m_SerialPort->setBaudRate(QSerialPort::Baud115200); //波特率:115200 bps
m_SerialPort->setDataBits(QSerialPort::Data8); //数据位:8 位
m_SerialPort->setStopBits(QSerialPort::OneStop); //停止位:1 位
m_SerialPort->setParity(QSerialPort::NoParity); //校验位:无
m_SerialPort->setFlowControl(QSerialPort::NoFlowControl);
qDebug() << "Port Name:" << portName;
if(!m_SerialPort->open(QIODevice::ReadWrite))
{
emit sig_SendTipInfo(false, "opened failed");
qDebug() << "open failed";
}
else qDebug() << "open successfully";
}
MySerial::~MySerial()
{
m_SerialPort->clear(); //丢弃缓冲区中的所有字符, 同时终止挂起的读取或写入操作
m_SerialPort->close();
m_SerialPort->deleteLater();
}
bool MySerial::WriteToSerial(QByteArray cmd)
{
if(cmd.isEmpty()) return false;
// qDebug() << "[ == MySerial == ] Send Data:" << cmd.toHex();
this->m_SerialPort->clear();
this->m_SerialPort->write(cmd);
this->m_SerialPort->flush(); //尽可能多地从内部写缓冲区写入底层串口而不阻塞
this->m_SerialPort->waitForBytesWritten(10);
return true;
}
QByteArray MySerial::ReadFromSerial()
{
QByteArray readData;
this->m_SerialPort->waitForReadyRead(5);
readData.append(this->m_SerialPort->readAll());
while (this->m_SerialPort->waitForReadyRead(5)) readData.append(this->m_SerialPort->readAll());
if(readData.isEmpty()) return QByteArray();
// qDebug() << "[ == MySerial == ] Read Data:" << readData.toHex();
return readData;
}
void MySerial::closeSerial()
{
if(!m_SerialPort->isOpen()) return;
m_SerialPort->clear();
m_SerialPort->close();
}
|