Qt QSerialPort 类实现串口通信
使用的时候在 pro 添加这句导入模块 QT += serialport
1.连接串口 . 要先获取到 可连接的所有的串口的名字
QSerialPortInfo::availablePorts()
[static] QListQSerialPortInfo::availablePorts()
Returns a list of available serial ports on the system.
返回系统上可用串行端口的列表
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
qDebug()<<"serialPortName:"<
获取到串口名字列表以后,我们需要选择个需要连接的 (自行根据选择)
2.根据串口名字 打开串口
#includeQSerialPort *m_serialPort = new QSerialPort();//实例化串口类个对象
if(m_serialPort->isOpen())//如果串口已经打开了 先给他关闭了
{
m_serialPort->clear();
m_serialPort->close();
}
//设置串口名字 假设我们上面已经成功获取到了 并且使用个
m_serialPort->setPortName(m_serialPortName[0]);
if(!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
{
qDebug()<setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);//设置波率和读写方向
m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
m_serialPort->setParity(QSerialPort::NoParity); //无校验位
m_serialPort->setStopBits(QSerialPort::OneStop); //位停止位
//连接信号槽 当下位机发送数据QSerialPortInfo 会发送个 readyRead 信号,我们定义个槽void receiveInfo()解析数据
connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
3.收发发送交互数据
//接收单片机的数据
void receiveInfo()
{
QByteArray info = m_serialPort->readAll();
QByteArray hexData = info.toHex();
//这里面的协议 你们自己定义就行 单片机发什么 代表什么 我们这里简单模拟下
if(hexData == "0x10000")
{
//do something
}
else if(hexData == "0x100001")
{
//do something
}
}
//向单片机发送数据
//基本和单片机交互 数据 都是16进制的 我们这里自己写个 Qstring 转为 16进制的函数
void convertStringToHex(const QString &str, QByteArray &byteData)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
byteData.resize(len/2);
char lstr,hstr;
for(int i=0; i= len)
break;
lstr = str[i].toLatin1();
hexdata = convertCharToHex(hstr);
lowhexdata = convertCharToHex(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
byteData[hexdatalen] = (char)hexdata;
hexdatalen++;
}
byteData.resize(hexdatalen);
}
//另个 函数 char 转为 16进制
char SerialPort::convertCharToHex(char ch)
{
/*
0x30等于十进制的48,48也是0的ASCII值,,
1-9的ASCII值是49-57,,所以某个值-0x30,,
就是将字符0-9转换为0-9
*/
if((ch >= '0') && (ch <= '9'))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+10;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+10;
else return (-1);
}
//写两个函数 向单片机发送数据
void sendInfo(char* info,int len){
for(int i=0; iwrite(info,len);//这句是真正的给单片机发数据 用到的是QIODevice::write 具体可以看文档
}
void sendInfo(const QString &info){
QByteArray sendBuf;
if (info.contains(" "))
{
info.replace(QString(" "),QString(""));//我这里是把空格去掉,根据你们定的协议来
}
qDebug()<<"Write to serial: "<write(sendBuf);这句是真正的给单片机发数据 用到的是QIODevice::write 具体可以看文档
}
4.析构的时候 关闭串口
if (m_serialPort->isOpen())
{
m_serialPort->close();
}
delete m_serialPort;
#ifndef WIDGET_H
#define WIDGET_H
#include#include#include#include#includenamespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void initUI();
QStringList getPortNameList();//获取所有可用的串口列表
void openPort();//打开串口
public slots:
void receiveInfo();
private:
Ui::Widget *ui;
QSerialPort* m_serialPort; //串口类
QStringList m_portNameList;
QComboBox* m_PortNameComboBox;
QPushButton* m_OpenPortButton;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include#includeWidget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_serialPort = new QSerialPort();
initUI();
m_portNameList = getPortNameList();
m_PortNameComboBox->addItems(m_portNameList); connect(m_OpenPortButton,&QPushButton::clicked,this,&Widget::openPort);
}
Widget::~Widget()
{
if (m_serialPort->isOpen())
{
m_serialPort->close();
}
delete m_serialPort;
delete ui;
}
void Widget::initUI()
{
this->setWindowTitle("码农小明 test QSerialPort");
m_OpenPortButton = new QPushButton();
m_OpenPortButton->setText("打开串口");
m_PortNameComboBox = new QComboBox();
QHBoxLayout *m_layout = new QHBoxLayout();
m_layout->addWidget(m_PortNameComboBox);
m_layout->addWidget(m_OpenPortButton);
this->setLayout(m_layout);
}
QStringList Widget::getPortNameList()
{
QStringList m_serialPortName;
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
{
m_serialPortName << info.portName();
qDebug()<<"serialPortName:"<isOpen())//如果串口已经打开了 先给他关闭了
{
m_serialPort->clear();
m_serialPort->close();
}
m_serialPort->setPortName(m_PortNameComboBox->currentText());//当前选择的串口名字
if(!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口
{
qDebug()<<"打开失败!";
return;
}
qDebug()<<"串口打开成功!";
m_serialPort->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);//设置波率和读写方向
m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
m_serialPort->setParity(QSerialPort::NoParity); //无校验位
m_serialPort->setStopBits(QSerialPort::OneStop); //位停止位
connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
}
//接收到单片机发送的数据进行解析
void Widget::receiveInfo()
{
QByteArray info = m_serialPort->readAll();
qDebug()<<"receive info:"<
- 上一篇:LINUX串口通信 2021/3/16
- 下一篇:C#延时导致UI界面不能刷新的问题 2021/3/6
