QT上位机开发(串口界面设计)

news2024/9/21 19:00:48

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

        如果上位机要和嵌入式设备进行打交道的话,那么串口可能就是我们遇到的第一个硬件设备。串口的物理接线很简单,基本上就是收、发、地三根线。目前大部分台式机,或者笔记本都已经没有232串口了,所以如果上位机需要和嵌入式设备进行通信的话,还需要通过usb转一下,这样才用的起来。

1、串口的应用场合

        串口的传输速率不是很快,这决定了它基本上只能进行低速数据的传输。但是就算是进行低速传输,最大速度也能达到115200bit每秒。这意味着,如果传输的仅仅是一些打印、传感器、控制数据,那也是绰绰有余了。串口的应用场景很多,串口转屏幕、串口的传感器、串口的gprs,这些都是很常见的串口设备。

2、串口之上的协议

        如果只是串口,的确没有什么难度。关键是,每一种串口设备都有自己的协议。对于上位机来说,它通常当成是主动请求方。而对下位机,也就是嵌入式模块来说,它一般是当成响应方来进行应答。两者之间的应答应该保持在一定的形式。举个例子来说,一个简单的协议应该包括这几个方面,

        1)报文头;

        2)长度;

        3)命令号;

        4)命令详细数据;

        5)校验码。

        报文头,一般用特殊的数据来表示,通常是55aa这种。长度,也就是报文的全部长度。可以用它来进行数据分割,也可以用它来计算校验码。命令号的话,根据内容的多少来设定。如果命令比较多,还有可能分成主命令和子命令。命令详细数据,这就和具体命令相关了,这里不再一一描述。最后就是校验码,为了防止数据在传输的过程中发生错误,一般需要对前面的数据做一个校验的操作。如果检验码没有问题,那么一切ok。反之,接收方一般会要求发送方重新发送数据。

3、练习串口界面设计

        网上关于串口界面设计的代码,或者是开源软件很多。大家不要认为软件比较多,就没有写的必要。正因为多,所以写起来才有很多的内容可以参考。而且,别人能写,不代表自己就可以完成相关的编写工作,这完全是两码事。

4、自己编写和设计串口界面

        串口界面一般分成两个部分,左边是设置,右边是数据显示。设置的话,分成了串口选择、波特率、数据位选择、校验位选择、停止位选择这几个部分。有了这几个参数,我们就可以打开串口了。当然对于收到的数据,我们也可以选择用ascii码显示,还是用16进制显示。

        对于数据的发送也是一样,可以通过选择ascii方式还是hex的方式,判断我们的数据应该怎么发送才对。右侧的部分一般是两个编辑框,一个是收到的数据,一个是发送的数据,在发送的数据框旁边通常包含一个发送按钮。

        最后在窗口的下半部分,还会有一些状态栏的数据,比如串口状态、收发了多少字节、版本号等等。我们在用qt实现的时候,首先需要创建一个widget工程,接下来用designer进行界面设计就可以了。因为控件的数量比较多,这里就不建议大家用cpp硬编码的形式去进行界面的绘制动作了。不管怎么说,哪怕是再简单的东西,还是要动手去练一练,遇到问题了经过思考和反馈,最后才能变成自己能够真正理解的东西。

        这是它对应的ui文件,

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QtWidgetsApplicationClass</class>
 <widget class="QMainWindow" name="QtWidgetsApplicationClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>805</width>
    <height>527</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>QtWidgetsApplication</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>87</x>
      <y>21</y>
      <width>87</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_2">
    <property name="geometry">
     <rect>
      <x>86</x>
      <y>61</y>
      <width>87</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_3">
    <property name="geometry">
     <rect>
      <x>86</x>
      <y>101</y>
      <width>87</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_5">
    <property name="geometry">
     <rect>
      <x>86</x>
      <y>185</y>
      <width>87</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>34</x>
      <y>227</y>
      <width>131</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>打开串口</string>
    </property>
   </widget>
   <widget class="QRadioButton" name="radioButton">
    <property name="geometry">
     <rect>
      <x>24</x>
      <y>268</y>
      <width>115</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>ASCII</string>
    </property>
   </widget>
   <widget class="QRadioButton" name="radioButton_2">
    <property name="geometry">
     <rect>
      <x>124</x>
      <y>268</y>
      <width>61</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>HEX</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox">
    <property name="geometry">
     <rect>
      <x>24</x>
      <y>298</y>
      <width>161</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>显示接收数据时间</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>34</x>
      <y>328</y>
      <width>131</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>清空接受</string>
    </property>
   </widget>
   <widget class="QRadioButton" name="radioButton_3">
    <property name="geometry">
     <rect>
      <x>24</x>
      <y>381</y>
      <width>110</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>ASCII</string>
    </property>
   </widget>
   <widget class="QRadioButton" name="radioButton_4">
    <property name="geometry">
     <rect>
      <x>123</x>
      <y>381</y>
      <width>61</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>HEX</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_2">
    <property name="geometry">
     <rect>
      <x>24</x>
      <y>413</y>
      <width>161</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>发送新行</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_3">
    <property name="geometry">
     <rect>
      <x>25</x>
      <y>448</y>
      <width>91</width>
      <height>19</height>
     </rect>
    </property>
    <property name="text">
     <string>自动发送</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>124</x>
      <y>437</y>
      <width>41</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label_7">
    <property name="geometry">
     <rect>
      <x>37</x>
      <y>498</y>
      <width>111</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>串口已经关闭</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_8">
    <property name="geometry">
     <rect>
      <x>307</x>
      <y>498</y>
      <width>171</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>TX:0Bytes  RX:0Bytes</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_9">
    <property name="geometry">
     <rect>
      <x>687</x>
      <y>498</y>
      <width>51</width>
      <height>20</height>
     </rect>
    </property>
    <property name="text">
     <string>@V1.0</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>11</y>
      <width>580</width>
      <height>351</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_2">
    <property name="geometry">
     <rect>
      <x>213</x>
      <y>381</y>
      <width>471</width>
      <height>101</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>692</x>
      <y>410</y>
      <width>90</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>发送</string>
    </property>
   </widget>
   <widget class="QGroupBox" name="groupBox">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>191</width>
      <height>471</height>
     </rect>
    </property>
    <property name="title">
     <string/>
    </property>
    <widget class="QLabel" name="label_6">
     <property name="geometry">
      <rect>
       <x>159</x>
       <y>438</y>
       <width>21</width>
       <height>16</height>
      </rect>
     </property>
     <property name="text">
      <string>s</string>
     </property>
    </widget>
    <widget class="QSplitter" name="splitter">
     <property name="geometry">
      <rect>
       <x>10</x>
       <y>0</y>
       <width>51</width>
       <height>201</height>
      </rect>
     </property>
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <widget class="QLabel" name="label">
      <property name="text">
       <string>串口 :</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_2">
      <property name="text">
       <string>波特率:</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_3">
      <property name="text">
       <string>数据位:</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_4">
      <property name="text">
       <string>校验位:</string>
      </property>
     </widget>
     <widget class="QLabel" name="label_5">
      <property name="text">
       <string>停止位:</string>
      </property>
     </widget>
    </widget>
    <widget class="QComboBox" name="comboBox_4">
     <property name="geometry">
      <rect>
       <x>76</x>
       <y>130</y>
       <width>87</width>
       <height>21</height>
      </rect>
     </property>
    </widget>
   </widget>
   <zorder>groupBox</zorder>
   <zorder>label</zorder>
   <zorder>label_2</zorder>
   <zorder>label_3</zorder>
   <zorder>label_4</zorder>
   <zorder>label_5</zorder>
   <zorder>comboBox</zorder>
   <zorder>comboBox_2</zorder>
   <zorder>comboBox_3</zorder>
   <zorder>comboBox_5</zorder>
   <zorder>pushButton</zorder>
   <zorder>radioButton</zorder>
   <zorder>radioButton_2</zorder>
   <zorder>checkBox</zorder>
   <zorder>pushButton_2</zorder>
   <zorder>radioButton_3</zorder>
   <zorder>radioButton_4</zorder>
   <zorder>checkBox_2</zorder>
   <zorder>checkBox_3</zorder>
   <zorder>lineEdit</zorder>
   <zorder>label_7</zorder>
   <zorder>label_8</zorder>
   <zorder>label_9</zorder>
   <zorder>textEdit</zorder>
   <zorder>textEdit_2</zorder>
   <zorder>pushButton_3</zorder>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="QtWidgetsApplication.qrc"/>
 </resources>
 <connections/>
</ui>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1354196.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前端-relation-graph实现关系数据展示(关系图/流程图)

目录 前言&#xff1a; 1. relation-graph 2. relation-graph数据关系组件---官方地址relation-graph - A Relationship Graph Componenthttps://www.relation-graph.com/ 3. 选择relation-graph的理由 4. 项目中引用relation-graph 4.1 下载命令 4.2 在Vue 2 中使用 4…

CGAL的无限制的Delaunay图

本章描述了构建L∞距离下线段Delaunay图的算法和几何特征。这些特征还包括绘制L∞距离下线段Delaunay图对偶&#xff08;即L∞距离下线段Voronoi图&#xff09;边缘的方法。L∞算法和特征依赖于欧几里得&#xff08;或L2&#xff09;距离下的线段Delaunay图算法和特征。L∞度量…

【动态规划】C++算法:44 通配符匹配

作者推荐 【动态规划】【字符串】扰乱字符串 本文涉及的基础知识点 动态规划 LeetCode44 通配符匹配 给你一个输入字符串 (s) 和一个字符模式 &#xff0c;请你实现一个支持 ‘?’ 和 ‘’ 匹配规则的通配符匹配&#xff1a; ‘?’ 可以匹配任何单个字符。 ’ 可以匹配…

动手学深度学习之卷积神经网络之池化层

池化层 卷积层对位置太敏感了&#xff0c;可能一点点变化就会导致输出的变化&#xff0c;这时候就需要池化层了&#xff0c;池化层的主要作用就是缓解卷积层对位置的敏感性 二维最大池化 这里有一个窗口&#xff0c;来滑动&#xff0c;每次我们将窗口中最大的值给拿出来 还是上…

大创项目推荐 深度学习人脸表情识别算法 - opencv python 机器视觉

文章目录 0 前言1 技术介绍1.1 技术概括1.2 目前表情识别实现技术 2 实现效果3 深度学习表情识别实现过程3.1 网络架构3.2 数据3.3 实现流程3.4 部分实现代码 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习人脸表情识别系…

【JavaFX】JavaFX11开发踩坑记录

文章目录 技术栈踩坑记录 技术栈 JavaFX 11MavenJDK 11 踩坑记录 这些坑对于初学者很容易踩&#xff0c;JavaFX经常会报错空指针异常遇到其中一个问题可能就会消耗好几天的时间。 JavaFX 采用的是MVC架构设计&#xff0c;页面设计使用 fxml文件&#xff1b;业务逻辑采用Con…

k8s的网络

k8s的网络 k8s中的通信模式&#xff1a; 1、pod内部之间容器与容器之间的通信 在同一个pod中的容器共享资源和网络&#xff0c;使用同一个网络命名空间&#xff0c;可以直接通信的 2、同一个node节点之内&#xff0c;不同pod之间的通信 每个pod都有一个全局的真实的ip地址…

qt 异常汇总

1. C2338 No Q_OBJECT in the class with the signal (编译源文件 ..\..\qt\labelme-master\src\mainwindow.cpp mainwindow头文件中的类没有Q_OBJECT宏定义&#xff0c;或者其子类或者其他依赖没有Q_OBJECT宏定义。 全部qt类都要写上Q_OBJECT. 2. C2385 对connect的访…

AI:116-基于深度学习的视频行为识别与分析

🚀点击这里跳转到本专栏,可查阅专栏顶置最新的指南宝典~ 🎉🎊🎉 你的技术旅程将在这里启航! 从基础到实践,深入学习。无论你是初学者还是经验丰富的老手,对于本专栏案例和项目实践都有参考学习意义。 ✨✨✨ 每一个案例都附带有在本地跑过的关键代码,详细讲解供…

ctfshow——PHP特性

文章目录 web 89web 90web 91web 92web 93web 94web 95web 96web 97web 98web 99 web 89 使用人工分配 ID 键的数值型数组绕过preg_match. 两个函数&#xff1a; preg_match()&#xff1a;执行正则表达式&#xff0c;进行字符串过滤。preg_match函数用法&#xff0c;正则表达式…

uni-app 前后端调用实例 基于Springboot 详情页实现

锋哥原创的uni-app视频教程&#xff1a; 2023版uniapp从入门到上天视频教程(Java后端无废话版)&#xff0c;火爆更新中..._哔哩哔哩_bilibili2023版uniapp从入门到上天视频教程(Java后端无废话版)&#xff0c;火爆更新中...共计23条视频&#xff0c;包括&#xff1a;第1讲 uni…

【Java EE初阶七】多线程案例(生产者消费者模型)

1. 阻塞队列 队列是先进先出的一种数据结构&#xff1b; 阻塞队列&#xff0c;是基于队列&#xff0c;做了一些扩展&#xff0c;适用于多线程编程中&#xff1b; 阻塞队列特点如下&#xff1a; 1、是线程安全的 2、具有阻塞的特性 2.1、当队列满了时&#xff0c;就不能往队列里…

Yapi安装配置(CentOs)

环境要求 nodejs&#xff08;7.6) mongodb&#xff08;2.6&#xff09; git 准备工作 清除yum命令缓存 sudo yum clean all卸载低版本nodejs yum remove nodejs npm -y安装nodejs,获取资源,安装高版本nodejs curl -sL https://rpm.nodesource.com/setup_8.x | bash - #安装 s…

图片中src属性绑定不同的路径

vue3 需求是按钮disable的时候&#xff0c;显示灰色的icon&#xff1b;非disable状态&#xff0c;显示白色的icon 一开始src写成三元表达式&#xff0c;发现不行&#xff0c;网上说src不能写成三元表达式&#xff0c;vue会识别成字符串 最后的解决方案 同时&#xff0c;发现…

win下持续观察nvidia-smi

简介&#xff1a;在Windows操作系统中&#xff0c;没有与Linux中watch命令直接对应的内置工具&#xff0c;但有1种方法快速简单的方法可以实现类似的效果&#xff0c;尤其是用于监控类似于nvidia-smi的命令输出。 历史攻略&#xff1a; Python&#xff1a;查看windows下GPU的…

美国地质调查局历史地形图

简介 美国地质调查局地形图的历史可以追溯到 19 世纪末&#xff0c;当时美国地质调查局开始着手绘制整个美国的详细地图。1:24,000 比例尺&#xff0c;也称为 7.5 分四边形地图&#xff0c;成为最广泛使用的比例尺之一。每张地图覆盖 7.5 分经纬度的区域&#xff0c;从而详细呈…

在Docker中安装Tomact

目录 前言&#xff1a; 一.安装Tomact 查找指定的tomact版本 下载tomact9.0 查看该镜像是否安装成功 安装成功之后就开始运行镜像了 ps&#xff08;用于列出正在运行的Docker容器&#xff09; ​编辑 测试(虚拟机ip:8080) ​编辑 解决措施 ​编辑 完成以上步骤&…

【Nginx】反向代理和负载均衡

反向代理 nginx 反向代理&#xff0c;就是将前端发送的动态请求由 nginx 转发到后端服务器。 server {listen 80;server_name localhost;# 反向代理,处理管理端发送的请求location /api/ {proxy_pass http://localhost:8080/admin/;#proxy_pass http://webservers/…

【Electron】快速建立Vue3+Vite+Electron Ts项目

git https://github.com/electron-vite/electron-vite-vue 创建项目 npm create electron-vite or pnpm create electron-vite 初始化 pnpm install or pnpm i 启动项目 pnpm dev 打包项目 pnpm build 项目创建成功后默认情况下 窗口是H800 W600 在createWindow 函数…

计算机视觉中的神经网络可视化工具与项目

前言 本文介绍了一些关于神经网络可视化的项目&#xff0c;主要有CNN解释器&#xff0c;特征图、卷积核、类可视化的一些代码和项目&#xff0c;结构可视化工具&#xff0c;网络结构手动画图工具。 CNN解释器 这是一个中国博士发布的名叫CNN解释器的在线交互可视化工具。 主要…