qt里QtSerialPort编程

QT +=serialport

#include <QtSerialPort/QSerialPort>

#include <QtSerialPort/QSerialPortInfo>

QSerialPort * testport=new QSerialPort();

if(testport->isOpen()){

testport->clear();

testport->close();

}

testport->setPortName(portname);

testport->setBaudRate(boud.toInt()); //设置波特率

testport->setDataBits(QSerialPort::Data8); //8个数据位

testport->setParity(QSerialPort::NoParity); //无校验位

testport->setStopBits(QSerialPort::OneStop); //一个停止位

testport->setFlowControl(QSerialPort::NoFlowControl);

if(!testport->open(QIODevice::ReadWrite)){

qDebug()<<"串口打开失败";

war->warnxianshi("请更新串口!!!","串口打开失败!");

war->showNormal();

// QMessageBox::information(this,"串口打开失败","请更新串口!!!!");

复制代码
  testport->setDataTerminalReady(true);
  connect(testport,SIGNAL(readyRead()),this,SLOT(recvdeal()));     //绑定串口读取数据槽

串口的读取槽:

void MainWindow:: recvdeal()

{

QString s=testport->readAll();

}

comboBox串口刷新:

ui->comboBox->clear();

foreach(const QSerialPortInfo & info,QSerialPortInfo::availablePorts()){

ui->comboBox->addItem(info.portName());

}

ui->comboBox->setCurrentIndex(-1);

相关推荐
大鹏说大话9 分钟前
Kotlin vs Java:Android之外,后端开发该怎么选?
开发语言
skywalker_1116 分钟前
注解和反射
java·开发语言
爱代码的小黄人16 分钟前
总谐波畸变率 THD 的计算公式整理:MATLAB 官方公式与论文常用公式对比
开发语言·matlab
云深麋鹿16 分钟前
C++ | AVLTree
开发语言·c++
2zcode17 分钟前
基于Matlab不规则颗粒粒径周长面积测量及计数系统
开发语言·算法·matlab
XerCis19 分钟前
ngrok实现内网穿透(以Python FastAPI为例)
开发语言·python·fastapi·ngrok
xiaoshuaishuai823 分钟前
C# 继承与虚方法
开发语言·windows·c#
Hua-Jay28 分钟前
OpenCV联合C++/Qt 学习笔记(十六)----图像细化、轮廓检测、轮廓信息统计及轮廓外接多边形
c++·笔记·qt·opencv·学习·计算机视觉
我滴老baby31 分钟前
企业级工具链设计从单一工具到分层工具体系的架构实践
java·开发语言·架构
AI玫瑰助手36 分钟前
Python流程控制:if-else与if-elif-else嵌套使用
开发语言·python·信息可视化