第三方类下载

https://sourceforge.net/projects/qextserialport/files/



目录介绍



搭建工程

拷贝qextserialbase.cpp、qextserialbase.h、win_qextserialport.cpp和win_qextserialport.h到自己的工程下

//添加头文件
#include "win_qextserialport.h"
//添加串口定义
Win_QextSerialPort *Com;
//添加槽函数
private slots:
void readCom(); //当接收到数据,会有信号
void on_pushButton_clicked(); //发送按钮,产生信号

初始化

//定义串口对象,指定串口名和查询模式,这里使用事件驱动EventDriven
Com = new Win_QextSerialPort("COM2", QextSerialBase::EventDriven); //以读写方式打开串口
if(Com->open(QIODevice::ReadWrite) == false)
{
QMessageBox::warning(this, tr("warning"), tr("COM2 open failed."));
} Com->setBaudRate(BAUD9600);
Com->setDataBits(DATA_8);
Com->setParity(PAR_NONE);
Com->setStopBits(STOP_1);
Com->setFlowControl(FLOW_OFF); connect(Com,SIGNAL(readyRead()),this,SLOT(readCom()));
connect(button,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));

void MainWindow::readCom()
{
qDebug() << "read: "<< Com->bytesAvailable() << "bytes"; //读取串口缓冲区的所有数据给临时变量data
QByteArray data = Com->readAll(); //将串口的数据显示在窗口的文本浏览器中
ui->textBrowser->insertPlainText(data);
}

void MainWindow::on_pushButton_clicked() //发送数据
{
//以ASCII码形式将数据写入串口
Com->write(ui->lineEdit->text().toAscii()); qDebug() << "write: "<< Com->bytesToWrite() << "bytes";
}

实际效果

经测试,在Win10运行,串口数据异常,还未找到原因

建议使用:https://blog.csdn.net/zhangxuechao_/article/details/81154296

qt 操作串口的更多相关文章

  1. qt 操作串口 QSerialPort

    准备工作 *.pro中加入 QT += serialport 初始化 void MainWindow::initPort() { //读取串口信息 foreach (const QSerialPort ...

  2. Qt编写串口通信程序全程图文解说

    (说明:我们的编程环境是windows xp下,在Qt Creator中进行,假设在Linux下或直接用源代码编写,程序稍有不同,请自己修改.) 在Qt中并没有特定的串口控制类,如今大部分人使用的是第 ...

  3. 转:Qt编写串口通信程序全程图文讲解

    转载:http://blog.csdn.net/yafeilinux/article/details/4717706  作者:yafeilinux (说明:我们的编程环境是windows xp下,在Q ...

  4. Qt编写串口通信程序全程图文讲解 .

    在Qt中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,我们这里也是使用的该类.我们可以去 http://sourceforge.net/projects/qex ...

  5. 【转】Qt编写串口通信程序全程图文讲解

    本文章原创于www.yafeilinux.com 转载请注明出处. (说明:我们的编程环境是windows xp下,在Qt Creator中进行,如果在Linux下或直接用源码编写,程序稍有不同,请自 ...

  6. QT 操作数据库

    整理一下 QT 操作数据库的一些要点,以备以后的查询学习(主要是操作 mysql ). 首先,要查询相关的驱动是否已经装好了,可以用以下的程序进行验证: #include <QtCore/QCo ...

  7. C#操作串口总结

    Technorati 标签: C#,SerialPort,ReadTo,ReadTimeout        最近几天一直在调一个要长时间连续不断的操作串口,并且是多线程运行,不允许中断的服务.后来服 ...

  8. 树莓派中QT实现串口通讯

    树莓派中QT实现串口通讯 开发平台为QT 此博客QT使用的为WiringPi驱动 我使用的串口调试助手为 cutecom 先简单说一些开发过程中需要注意的问题 Linux 下设备为 tty ,对应在 ...

  9. MSComm控件与Win32 API操作串口有何区别?

    MSComm控件与Win32 API操作串口有何区别? [问题点数:50分,结帖人shell_shell]   收藏帖子 回复 我是一个小兵,在战场上拼命!   结帖率 83.33% 我以前用MSCo ...

随机推荐

  1. Maven 中 dependencyManagement 元素,知识点

    Maven 提供的 dependencyManagement 元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性.在 dependencyManagement 元素下的依赖声明不会 ...

  2. USACO Balanced Lineup

    poj 3264 http://poj.org/problem?id=3264 洛谷 P2880 https://www.luogu.org/problemnew/show/P2880 题目描述 Fo ...

  3. C++ vector初始化方式

    C++的初始化方法很多,各种初始化方法有一些不同. (1): vector<int> ilist1; 默认初始化,vector为空, size为0,表明容器中没有元素,而且 capacit ...

  4. 忍者钩爪 ( ninja) 题解———2019.10.19

    可以到这里测..嘿嘿嘿 题目: [问题 描述 ] 小 Q 是一名酷爱钩爪的忍者, 最喜欢飞檐走壁的感觉, 有一天小 Q 发现一个练习使用钩 爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一 ...

  5. [LeetCode] 406. Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  6. [LeetCode] 41. First Missing Positive 首个缺失的正数

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. uniApp配置文件几个注意点

    虽然有文档,但是偶尔还是会又找不到的,写下来遇到过的问题,随时补充.好记性不如烂笔头. 1.打包完安装之后,app 有时候会弹出一个提示框.如下: 修改配置项,设置 ignoreVersion 为 t ...

  8. springcloud(五,多个服务注册中心eureka)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  9. enum一个最不像class的class

    enum一个最不像class的classjava枚举类型是jdk5出现的.它的出现主要为了解决一些有特殊意义,已经确定的,长度不会改变的集合. //月份描述 public class Month { ...

  10. openresty nginx systemtap netdata

    https://openresty.org/cn/getting-started.html https://github.com/openresty https://github.com/openre ...