qt 操作串口 QSerialPort
准备工作
*.pro中加入
QT += serialport
初始化
void MainWindow::initPort()
{
//读取串口信息
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
qDebug()<<"Name:"<<info.portName();
qDebug()<<"Description:"<<info.description();
qDebug()<<"Manufacturer:"<<info.manufacturer();
//这里相当于自动识别串口号之后添加到了cmb,如果要手动选择可以用下面列表的方式添加进去
QSerialPort serial;
serial.setPort(info);
if(serial.open(QIODevice::ReadWrite))
{
//将串口号添加到cmb
ui->cmbPortName->addItem(info.portName());
//关闭串口等待人为(打开串口按钮)打开
serial.close();
}
}
QStringList baudList;//波特率
QStringList parityList;//校验位
QStringList dataBitsList;//数据位
QStringList stopBitsList;//停止位
baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300"
<<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600"
<<"14400"<<"19200"<<"38400"<<"56000"<<"57600"
<<"76800"<<"115200"<<"128000"<<"256000";
ui->cmbBaudRate->addItems(baudList);
ui->cmbBaudRate->setCurrentIndex(12);
parityList<<"无"<<"奇"<<"偶";
ui->cmbParity->addItems(parityList);
ui->cmbParity->setCurrentIndex(0);
dataBitsList<<"5"<<"6"<<"7"<<"8";
ui->cmbDataBits->addItems(dataBitsList);
ui->cmbDataBits->setCurrentIndex(3);
stopBitsList<<"1";
stopBitsList<<"1.5";
stopBitsList<<"2";
ui->cmbStopBits->addItems(stopBitsList);
ui->cmbStopBits->setCurrentIndex(0);
//设置按钮可以被按下
ui->openCom->setCheckable(true);
connect(ui->openCom,SIGNAL(clicked()),this,SLOT(on_btnOpen_clicked()));
connect(ui->pushButtonSpeedSet,SIGNAL(clicked()),this,SLOT(on_btnSet_clicked()));
}
打开串口
void MainWindow::on_btnOpen_clicked()
{
my_serialport = new QSerialPort(this);
//设置串口号
my_serialport->setPortName(ui->cmbPortName->currentText());
//以读写方式打开串口
if(my_serialport->open(QIODevice::ReadWrite))
{
//设置波特率
my_serialport->setBaudRate(ui->cmbBaudRate->currentText().toInt());
//设置数据位
my_serialport->setDataBits(QSerialPort::Data8);
//设置校验位
my_serialport->setParity(QSerialPort::NoParity);
//设置流控制
my_serialport->setFlowControl(QSerialPort::NoFlowControl);
//设置停止位
my_serialport->setStopBits(QSerialPort::OneStop);
//每秒读一次
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(readComDataSlot()));
timer->start(1000);
ui->openCom->setEnabled(false);
ui->lineEditSpeed->setText(QString("待传输"));
ui->lineEditTemp->setText("0");
}
else
{
QMessageBox::about(NULL, "提示", "串口没有打开!");
return;
}
}
读
void MainWindow::readComDataSlot()
{
//读取串口数据
QByteArray readComData = my_serialport->readAll();
qDebug() << "size: " << readComData.size();
qDebug() << "data: " << readComData;
//数据显示
if(readComData != NULL)
{
ui->lineEdit->setText(QString(readComData));
}
//清除缓冲区
readComData.clear();
}
写
void MainWindow::on_btnSet_clicked()
{
//获取发送区的数据
QString sendData = ui->lineEditSpeedSet->text();
QByteArray sendData_2 = sendData.toLatin1();
//写入缓冲区
my_serialport->write(sendData_2);
}
qt 操作串口 QSerialPort的更多相关文章
- qt 操作串口
第三方类下载 https://sourceforge.net/projects/qextserialport/files/ 目录介绍 搭建工程 拷贝qextserialbase.cpp.qextser ...
- Qt编写串口通信程序全程图文解说
(说明:我们的编程环境是windows xp下,在Qt Creator中进行,假设在Linux下或直接用源代码编写,程序稍有不同,请自己修改.) 在Qt中并没有特定的串口控制类,如今大部分人使用的是第 ...
- 转:Qt编写串口通信程序全程图文讲解
转载:http://blog.csdn.net/yafeilinux/article/details/4717706 作者:yafeilinux (说明:我们的编程环境是windows xp下,在Q ...
- 树莓派中QT实现串口通讯
树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...
- Qt实现串口通信总结
Qt实现串口通信总结 注意: Qt5发布之前,Qt实现串口通信一般是采用第三方类库qextserialport.Qt5发布后自带了QtSerialPort 能够支持串口通信. 1.Qextserial ...
- Qt开发串口
首先,在工程文件里面, QT += serialport 在头文件里面, #include <QSerialPort> 1.配置打开串口 QSerialPort* myserial = n ...
- Qt编写串口通信程序全程图文讲解 .
在Qt中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,我们这里也是使用的该类.我们可以去 http://sourceforge.net/projects/qex ...
- Qt 编写串口调试助手
一.成品图展示 成品图如下所示: 二.串口通讯步骤 1.在工程文件(.pro)中添加串口通信相关运行库:QT += serialport 2.在头文件中添加: #include <QSerial ...
- 【转】Qt编写串口通信程序全程图文讲解
本文章原创于www.yafeilinux.com 转载请注明出处. (说明:我们的编程环境是windows xp下,在Qt Creator中进行,如果在Linux下或直接用源码编写,程序稍有不同,请自 ...
随机推荐
- zz斯坦福Jure Leskovec图表示学习:无监督和有监督方法
斯坦福Jure Leskovec图表示学习:无监督和有监督方法(附PPT下载) 2017 年 12 月 18 日 专知 专知内容组(编) 不要讲得太清楚 [导读]现实生活中的很多关系都是通过图的形式 ...
- Linux for Matlab中文注释乱码(亲测有效)
中文注释乱码的原因是windows下的m文件采用的是gb2312编码,只要将所有的m文件转成 utf8文件,显示就正常了. 1.首先安装enca:sudo apt-get install enca 2 ...
- input type属性为number时,去掉右边的上下箭头
加上样式: input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; ...
- Vs code背景图
写前端代码时,用过webstorm,sublime,vscode,最终还是选择了vscode,主要原因是(好看)简洁的编程环境,丰富的插件功能.不过无论是哪一个编辑器,长时间看着黑色/白色的背景难免单 ...
- Spring 常用配置、Bean
spring模块 Spring-Core: Core包是框架的最基础部分,并提供依赖注入(Dependency Injection)管理Bean容器功能. Spring-Context:(Spring ...
- MySQL实战45讲学习笔记:第一讲
一.MySQL逻架构图 二.连接器工作原理刨析 1.连接器工作原理图 2.原理图说明 1.连接命令 mysql -h$ip -P$port -u$user -p 2.查询链接状态 3.长连接端连接 1 ...
- [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- skip connections
deep learning初学者,最近在看一些GAN方面的论文,在生成器中通常会用到skip conections,于是就上网查了一些skip connection的博客,虽然东西都是人家的,但是出于 ...
- div 中 id 和 class使用详解【转】
原文地址:https://blog.csdn.net/zxw136511485/article/details/71191053 在div 标签中,我们比较常见的属性是id 和class,那么这两个属 ...