当前位置: 首页 > news >正文

QT 使用串口

目录

1.1.1  添加库,添加类

1.1.2 定义串口

1.1.3  搜索串口

1.1.4 设置和打开串口

1.1.5 读取数据

1.1.6 发送数据

1.1.7 关闭串口 

 

1.1.1  添加库,添加类

首先,QT5 是自带 QSerialPort(Qt5 封装的串口类)这个类的,使用时需要在 pro 文件里面添加一行:

QT += serialport

加入下面两个文件:

#include <QSerialPort>
#include <QSerialPortInfo>

QT5 中,串口通信是借助一个 QSerialPort 的对象来实现的,在设置 QSerialPort 对象对串口的名称、波特率、 数据位、校验位、停止位等参数后,方能对串口进行读写。下面,我总结了一下借助 QSerailPort 对串口进行设 置、打开、读、写和关闭的过程。

1.1.2 定义串口

QSerialPort *uart;
uart = new QSerialPort(this);

1.1.3  搜索串口

//availablePorts 是返回当前系统所有能用的串口,返回值是一个容器//QList 是容器类,在此的容器里边装的是 QSerialPortInfoQList<QSerialPortInfo> info = QSerialPortInfo::availablePorts();for(int i=0; i<info.size(); i++){ui->uartNameCombo->addItem(info[i].portName()); //addItem 加入数据到下拉
菜单里}

1.1.4 设置和打开串口

void UiMain::on_openBtn_clicked()
{QString uartName;QSerialPort::BaudRate baudRate;QSerialPort::DataBits dataBit;QSerialPort::StopBits stopBit;QSerialPort::Parity parityBit;//currentText 是获取当前选中的名字uartName = ui->uartNameCombo->currentText();//波特率if(ui->baudrateCombo->currentText() == "9600"){baudRate = QSerialPort::Baud9600;
}else if(ui->baudrateCombo->currentText() == "115200"){baudRate = QSerialPort::Baud115200;}//数据位if(ui->databitCombo->currentText() == "5"){dataBit = QSerialPort::Data5;}else if(ui->databitCombo->currentText() == "6"){dataBit = QSerialPort::Data6;}else if(ui->databitCombo->currentText() == "7"){dataBit = QSerialPort::Data7;}else if(ui->databitCombo->currentText() == "8"){dataBit = QSerialPort::Data8;}//停止位if(ui->stopbitCombo->currentText() == "1"){stopBit = QSerialPort::OneStop;}else if(ui->stopbitCombo->currentText() == "1.5"){stopBit = QSerialPort::OneAndHalfStop;}else if(ui->stopbitCombo->currentText() == "2"){stopBit = QSerialPort::TwoStop;}//校验位if(ui->parityCombo->currentIndex() == 0){parityBit = QSerialPort::OddParity;}else if(ui->parityCombo->currentIndex() == 1){parityBit = QSerialPort::EvenParity;}else if(ui->parityCombo->currentIndex() == 2){parityBit = QSerialPort::NoParity;}//把参数设置到 uart 里uart->setPortName(uartName);uart->setBaudRate(baudRate);uart->setDataBits(dataBit);uart->setStopBits(stopBit);uart->setParity(parityBit);if(!uart->open(QIODevice::ReadWrite)){QMessageBox::critical(this, "fbi warnning", "打开失败");}else{QMessageBox::information(this, "提示", "打开成功");}

1.1.5 读取数据

串口在收到数据后,会将数据存入接收缓冲区。此时,我们可以通过 readAll()函数将接收缓冲区的数据读出 来。当串口的接收缓冲区有数据时,QSerilaPort 对象会发出一个 readyRead()的信号。因此,我们可以编写一个槽 函数来读数据。

connect(uart, SIGNAL(readyRead()), this, SLOT(slotUartReadyRead()));

 QString uartBuf;
//假如没收完断截的数据,就会一直触发槽函数,定时器会被一直重置,不会超时
//当断截数据发完了,定时器就没人重启了,就会超时,这个时候收到的数据就是完整的数据
//也就是说收到一个字节后,20ms 内没有再次收到数据,默认为一帧数据接收完成
void UiMain::slotUartReadyRead()
{timer->start(20); //重启定时器 20ms
uartBuf.append(uart->readAll()); //追加写入到 uartBuf 里
}//超时了,说明数据完成一帧数据
void UiMain::slotTimeout()
{timer->stop();QByteArray ba;ba = uartBuf.toUtf8();//QString(nRecv)不是强制转换//QString(nRecv)是会触发 QString 的构造函数,当然 QString(int)这种构造函数是没有
的,会报错
ui->recvEdit->appendPlainText(ba);
}

1.1.6 发送数据

使用write函数便可以把字节数组中的字节发送出去。

uart->write(buf.toUtf8()); //QString 转 char *的方法: buf.toLocal8Bit().data()

1.1.7 关闭串口 

串口不用时,可通过 close()函数将其关闭。

uart->close();

http://www.lryc.cn/news/100348.html

相关文章:

  • GitHub上怎么寻找项目?
  • 如何快速用Go获取短信验证码
  • 详解Mybatis查询之resultType返回值类型问题【4种情况】
  • Python-Python基础综合案例:数据可视化 - 折线图可视化
  • CSS盒子模型(HTML元素布局)
  • PostgreSQL-Centos7源码安装
  • QTday2信号和槽
  • 信驰达推出RTL8720DN系列2.4G和5G双频Wi-Fi+蓝牙二合一模块
  • 【LeetCode】剑指 Offer Ⅱ 第1章:整数(5道题) -- Java Version
  • 解析数据可视化工具:如何选择最合适的软件
  • 大数据面试题之Elasticsearch:每日三题(七)
  • ubuntu20.04 安装 Qt5.15
  • web之标签元素转换成图片、a标签元素下载图片、获取浏览器窗口名称、重命名、元素定位、旋转、拉伸文字、文字向心对齐
  • 你应该知道的关于PCB布线的31条建议
  • matlab中dir的各种使用方法(包括递归遍历子文件夹)
  • 软件测试/测试开发丨Selenium环境安装与使用
  • WPF实战学习笔记15-使用Memo类的GetAll接口
  • 算法与数据结构-二分查找
  • 【软件测试】什么是selenium
  • redis线程模型
  • 【idea工具】idea工具,build的时候提示:程序包 com.xxx.xx不存在的错误
  • 线性代数——特征值和特征向量
  • 运筹系列83:使用分枝定界求解tsp问题
  • linux 指令 第3期
  • 测试用例实战
  • Unity XML1——XML基本语法
  • 了解Unity编辑器之组件篇Playables和Rendering(十)
  • python的包管理器pip安装经常失败的解决办法:修改pip镜像源
  • 忘记安卓图案/密码锁如何解锁?
  • Bash编程