QT5有专门的串口类: 

QSerialPort:提供访问串口的功能 

QSerialPortInfo:提供系统中存在的串口的信息 

具体使用方法: 

1.在pro文件中加入: QT += serialport

2.mainwindow.h中添加头文件及成员:

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <QSerialPort></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <QSerialPortInfo></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>:
QSerialPort serial;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//串口实例</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> initSeialPort();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化串口函数</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> slots://必须加slots
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">serialRead</span>(); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

3.在主窗口添加一个comboBox,一个label,初始化串口

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> MainWindow<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::initSeialPort</span>()
{
connect(<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&</span>serial,SIGNAL(readyRead()),this,SLOT(serialRead())); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//连接槽</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取计算机上所有串口并添加到comboBox中</span>
QList<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>QSerialPortInfo<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> infos <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> QSerialPortInfo<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::availablePorts</span>();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(infos<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>isEmpty())
{
ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>comboBox<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>addItem(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"无串口"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
}
foreach (QSerialPortInfo info, infos) {
ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>comboBox<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>addItem(info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName());
qDebug() <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName();
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

3.comboBox选择改变事件函数:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> MainWindow<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::on_comboBox_currentIndexChanged</span>(const QString <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&</span>arg1)
{
QSerialPortInfo info;
QList<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span>QSerialPortInfo<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> infos <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> QSerialPortInfo<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::availablePorts</span>();
int i <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
foreach (info, infos) {
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(info<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>portName() <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> arg1) break;
i<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">++</span>;
}
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(i <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> infos<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>size ()){<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//can find</span>
ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>lblStatus<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口打开成功"</span>);
serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>close();
serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setPort(info);
serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>open(QIODevice<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">::ReadWrite</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//读写打开</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setBaudRate(QSerialPort::Baud9600); //波特率</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setDataBits(QSerialPort::Data8); //数据位</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setParity(QSerialPort::NoParity); //无奇偶校验</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setStopBits(QSerialPort::OneStop); //无停止位</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.setFlowControl(QSerialPort::NoFlowControl); //无控制</span>
}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{
serial<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>close();
ui<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>lblStatus<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>setText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口打开失败"</span>);
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>

4.串口发送

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> MainWindow::on_btnSend_clicked()
{
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// QByteArray arr = "abcd";</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// serial.write(arr);</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> dat[] = {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x55</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x01</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x05</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xAA</span>};
serial.write(dat,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

5.串口接收,需添加一个textEdit。

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">void <span class="hljs-constant" style="box-sizing: border-box;">MainWindow::</span>serialRead()
{
<span class="hljs-constant" style="box-sizing: border-box;">QByteArray</span> qa = serial.readAll();
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(qa.at(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)==<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'a'</span>)
ui->textEdit->append(qa);
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> <span class="hljs-constant" style="box-sizing: border-box;">QMessageBox::</span>warning (this,tr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"提示信息"</span>),tr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"串口接收成功"</span>));
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><div class="save_code tracking-ad" data-mod="popu_249" style="box-sizing: border-box; position: absolute; height: 60px; right: 30px; top: 5px; color: rgb(255, 255, 255); cursor: pointer; z-index: 2;"><a target=_blank target="_blank" style="color: rgb(106, 57, 6); box-sizing: border-box;"><img src="http://static.blog.csdn.net/images/save_snippets.png" style="border: none; box-sizing: border-box; max-width: 100%;" alt="" /></a></div>

可以用虚拟串口软件和丁丁串口助手调试。 

【Qt开发】Qt5.7串口开发的更多相关文章

  1. 【神经网络与深度学习】【Qt开发】【VS开发】从caffe-windows-visual studio2013到Qt5.7使用caffemodel进行分类的移植过程

    [神经网络与深度学习][CUDA开发][VS开发]Caffe+VS2013+CUDA7.5+cuDNN配置成功后的第一次训练过程记录<二> 标签:[神经网络与深度学习] [CUDA开发] ...

  2. Qt5.12.2开发Android环境搭建

    Qt-Android开发环境概要qt-opensource-windows-x86-5.12.2----armv7jdk-8u201-windows-x64android-ndk-r18b-windo ...

  3. 快速阅读《QT5.9 c++开发指南》1

    简介:<QT5.9 c++开发指南>的作者是和i三位主要从事地球物理探测仪器设计.数据处理方法研究和软件开发等工作的博士们,这本书以QT Widget为主要内容,比较全面地教授了QT开发桌 ...

  4. 【Qt开发】【VS开发】【Linux开发】OpenCV、Qt-MinGw、Qt-msvc、VS2010、VS2015、Ubuntu Linux、ARM Linux中几个特别容易混淆的内容

    [Qt开发][VS开发][Linux开发]OpenCV.Qt-MinGw.Qt-msvc.VS2010.VS2015.Ubuntu Linux.ARM Linux中几个特别容易混淆的内容 标签:[Qt ...

  5. C# 串口开发

    在单片机项目开发中,上位机也是一个很重要的部分,主要用于数据显示(波形.温度等).用户控制(LED,继电器等),下位机(单片机)与 上位机之间要进行数据通信的两种方式都是基于串口的: USB转串口 - ...

  6. KL25开发板利用串口蓝牙与PC通信

    KL25开发板芯片本身支持三个串口,uart0,uart1,uart2.其中uart0不太一样,在数据手册里面单独一章介绍:而uart1和uart2则是一样的. 我所使用的串口蓝牙模块是BC04,支持 ...

  7. android 串口开发第二篇:利用jni实现android和串口通信

    一:串口通信简介 由于串口开发涉及到jni,所以开发环境需要支持ndk开发,如果未配置ndk配置的朋友,或者对jni不熟悉的朋友,请查看上一篇文章,android 串口开发第一篇:搭建ndk开发环境以 ...

  8. iTOP-4418开发板和6818开发板-第五路串口介绍

    iTOP-4418开发板和6818开发板 的除去默认 4 个串口的配置和用法. 4418 的开发板最多支持 5 路串口,如下图所示,4418 的 datasheet.   6818 的开发板最多支持 ...

  9. JAVA串口开发帮助类分享-及写在马年末

    摘要: 在系统集成开发过程中,存在着各式的传输途径,其中串口经常因其安全性高获得了数据安全传输的重用,通过串口传输可以从硬件上保证数据传输的单向性,这是其它介质所不具备的物理条件.下面我就串口java ...

随机推荐

  1. 针对windows10 10.4号升级后VMware低版本不能使用的问题

    windows在10.4号下午发布的新版补丁升级,VMware老版本不能打开,之后发现好多小伙伴都在贴吧发帖子VMware不能用,提示升级,犯愁升还是不升,老版本的安装了好几个虚拟机,换成新版不知道能 ...

  2. Python 面向对象Ⅳ

    类的继承 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制. 通过继承创建的新类称为子类或派生类,被继承的类称为基类.父类或超类. 继承语法 在python中继承中的 ...

  3. Window 环境升级node版本

    https://github.com/Kenshin/gnvm 下载gnvm,安装在node文件目录下 gnvm version 然后打开cmd命令行窗口,输入:gnvm update latest, ...

  4. TTTTTTTTTTTT 百度之星D map+hash

    Problem D  Accepts: 2806  Submissions: 8458  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  5. [POI2011]DYN-Dynamite

    题目链接:Click here Solution: 直接做似乎不太可行,我们考虑二分 我们设\(f[x]\)表示以\(x\)为根的子树中选择了的节点到\(x\)的距离的最小值,初值为\(inf\) \ ...

  6. 【Python】PIL模块

    Python自建库,在爬虫等基础应用中更加简单好记,做整理以备自查. 目录 Image模块 open类.Save类.format类.Mode类.convert类.Size类.Info类.new类.Co ...

  7. Spring Boot教程(三十二)多数据源配置与使用

    之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...

  8. DVWA--Command Injection

    首先我们查看源代码一下 <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ]; / ...

  9. Java并发编程的艺术笔记(八)——线程池

    一.线程池的主要处理流程 ThreadPoolExecutor执行execute方法分下面4种情况. 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步需要获 ...

  10. 在linux 系统下 使用命令行对mysql 数据库进行操作

    1.连接mysql root@test:/home# mysql -uroot -proot <uroot是用户名,proot是密码> 2.查询所有的库 mysql> show da ...