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 小时前
Java-82 深入浅出 MySQL 内部架构:服务层、存储引擎与文件系统全覆盖
java·开发语言·数据库·学习·mysql·spring·微服务
惜.己2 小时前
pytest中使用skip跳过某个函数
开发语言·python·测试工具·pytest
姜暮儿2 小时前
C++ 性能优化
开发语言·c++
啊呦.超能力3 小时前
QT开发---多线程编程
开发语言·qt
铭哥的编程日记3 小时前
《从C风格到C++风格:内存管理的进化之路》
开发语言·c++
秃了也弱了。3 小时前
reflections:Java非常好用的反射工具包
java·开发语言
Joker—H4 小时前
【Java】Reflection反射(代理模式)
java·开发语言·经验分享·代理模式·idea
程序员编程指南5 小时前
Qt 与 SQLite 嵌入式数据库开发
c语言·数据库·c++·qt
阿里巴巴淘系技术团队官网博客5 小时前
面向互联网2C业务的分布式类Manus Java框架
java·开发语言·分布式
fyzy6 小时前
qt编译时一直循环报错打印-spec win32-g++ “CONFIG+=debug“ “CONFIG+=qml_debug“
qt