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

相关推荐
xcyxiner13 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner20 小时前
DicomViewer (添加模型类)3
qt
xcyxiner2 天前
DicomViewer (目录调整) 2
qt
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.4 天前
C语言--day30
c语言·开发语言