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);

相关推荐
金灰1 分钟前
HTML5--裸体回顾
java·开发语言·前端·javascript·html·html5
爱上语文5 分钟前
Java LeetCode每日一题
java·开发语言·leetcode
Манго нектар32 分钟前
JavaScript for循环语句
开发语言·前端·javascript
程序猿小D40 分钟前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
阿华的代码王国1 小时前
【JavaEE】——文件IO的应用
开发语言·python
satan–01 小时前
R语言的下载、安装及环境配置(Rstudio&VSCode)
开发语言·windows·vscode·r语言
电饭叔1 小时前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python
Eternal-Student1 小时前
everyday_question dq20240731
开发语言·arm开发·php
卑微求AC2 小时前
(C语言贪吃蛇)11.贪吃蛇方向移动和刷新界面一起实现面临的问题
c语言·开发语言
程序猿小D2 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa