串口调试助手--Qt
串口调试助手----------该程序使用Qt框架,C ++语言编译而成
项目文件介绍:
main.cpp 该文件为该程序的入口程序
mainwindow.h 该文件为该程序的主要声明部分
mainwindow.cpp 该文件为该程序的主要定义部分
mainwindow.ui 该文件为该程序的ui界面设计
界面.png 界面的显示效果
该文件中获取串口是通过读取Windows系统下的注册表中的信息得到的, - 使用Qt中的定时器来每个3s读取一次注册表
串口通信方面:通过使用Qt的封装的QSerialPort来实现
main.cpp
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QSerialPort>
- #include <QTimer>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
- /*
- * 功能:获取电脑中串口的端口
- * 参数:无
- * 返回值:无
- */
- void Get_Serial_Port(void);
- /*
- * 功能:当串口有数据的时候执行
- * 参数:无
- * 返回值:无
- */
- void readData(void);
- /*
- * 功能:每个3s执行的任务
- * 参数:无
- * 返回值:无
- */
- void myThread(void);
- private slots:
- /*
- * 功能:点击pushButton按钮功能
- * 参数:无
- * 返回值:无
- */
- void on_pushButton_clicked();
- /*
- * 功能:点击清空按钮功能,清空显示区的显示
- * 参数:无
- * 返回值:无
- */
- void on_pushButton_2_clicked();
- void on_pushButton_3_clicked();
- void on_pushButton_4_clicked();
- void on_pushButton_5_clicked();
- private:
- Ui::MainWindow *ui;
- //串口类指针
- QSerialPort *Serial;
- //时间类指针
- QTimer *time;
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include "windows.h"
- #include "QVector"
- #include "QDebug"
- #include "stdio.h"
- #include "QMessageBox"
- #include <stdlib.h>
- #define MAX_KEY_LENGTH 255
- #define MAX_VALUE_NAME 16383
- /*
- * 功能:读取注册表下的子项
- * 参数:hkey:注册表的根
- * lpSubkey:注册表根下的路径
- * retArray:返回要查找的路径下的值的数组
- * 返回值:无
- */
- static void Get_Regedit(HKEY hkey,LPCSTR lpSubKey,QVector<QString> &retArray);
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- //时间类初始化
- time = new QTimer(this);
- connect(time,&QTimer::timeout,this,&MainWindow::myThread);
- time->start();
- //状态栏显示
- ui->statusBar->showMessage("程序运行中...");
- //初始化串口的显示
- this->Get_Serial_Port();
- QStringList temp;
- //波特率的显示
- temp << "" << "" << "" << "" << "" << "";
- ui->comboBox_2->addItems(temp);
- //数据位的显示
- temp.clear();
- temp << "" << "" << "" << "";
- ui->comboBox_3->addItems(temp);
- //奇偶检验位的显示
- temp.clear();
- temp << "" << "" << "";
- ui->comboBox_4->addItems(temp);
- //停止位的显示
- temp.clear();
- temp << "" << "1.5" << "";
- ui->comboBox_5->addItems(temp);
- this->Serial = new QSerialPort(nullptr);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- /*
- * 功能:获取电脑中串口的端口
- * 参数:无
- * 返回值:无
- */
- void MainWindow::Get_Serial_Port()
- {
- QVector<QString> retArray;
- ui->comboBox->clear();
- Get_Regedit(HKEY_LOCAL_MACHINE,\
- "HARDWARE\\DEVICEMAP\\SERIALCOMM",\
- retArray);
- qDebug() << retArray.size();
- QVector<QString>::iterator iter;
- for (iter=retArray.begin();iter!=retArray.end();iter++)
- {
- qDebug() << *iter << "\0";
- ui->comboBox->addItem(*iter);
- }
- }
- /*
- * 功能:点击pushButton按钮功能,打开串口
- * 参数:无
- * 返回值:无
- */
- void MainWindow::on_pushButton_clicked()
- {
- if(!Serial->isOpen())
- {
- qDebug() << ui->comboBox->currentText();
- //设置串口的端口名称
- Serial->setPortName(ui->comboBox->currentText());
- //toInt:将字符串转换为数字
- //设置串口的波特率
- Serial->setBaudRate((ui->comboBox_2->currentText()).toInt(nullptr,));
- //设置串口的数据位
- Serial->setDataBits((QSerialPort::DataBits((ui->comboBox_3->currentText()).toInt(nullptr,))));
- //设置串口的奇偶校验位
- Serial->setParity(QSerialPort::Parity((ui->comboBox_4->currentText()).toInt(nullptr,)));
- //设置串口的停止位
- Serial->setStopBits(QSerialPort::StopBits((ui->comboBox_5->currentText()).toInt(nullptr,)));
- //设置串口的流
- Serial->setFlowControl(QSerialPort::NoFlowControl);
- BOOL isSerial = Serial->open(QIODevice::ReadWrite);
- if(!isSerial)
- {
- qDebug() << "串口打开错误!";
- return;
- }
- //创建一个信号与槽,使得串口有数据可以读取的时候可以执行readData()函数
- connect(Serial,&QSerialPort::readyRead,this,&MainWindow::readData);
- ui->pushButton->setText("已启动");
- }
- else
- {
- ui->pushButton->setText("启动");
- Serial->close();
- }
- }
- /*
- * 功能:读取注册表下的子项
- * 参数:hkey:注册表的根
- * lpSubkey:注册表根下的路径
- * retArray:返回要查找的路径下的值的数组
- * 返回值:无
- */
- static void Get_Regedit(HKEY hkey,LPCSTR lpSubKey,QVector<QString> &retArray)
- {
- HKEY phkey = nullptr;
- BOOL isSuccess = false;
- /*
- * 功能:打开注册表,返回值为是否打开成功
- */
- isSuccess = RegOpenKeyA(hkey,lpSubKey,&phkey);
- if(isSuccess != ERROR_SUCCESS)
- {
- qDebug() << "注册表打开失败!";
- return;
- }
- qDebug() << "注册表打开成功!";
- /*
- * 功能:读取注册表下的子项
- */
- DWORD i =;
- LSTATUS retCode = ERROR_SUCCESS;
- CHAR achValue[MAX_VALUE_NAME];
- DWORD cchValue = MAX_VALUE_NAME;
- BYTE Data[MAX_VALUE_NAME];
- DWORD cbData = MAX_VALUE_NAME;
- do
- {
- cchValue = MAX_VALUE_NAME;
- cbData = MAX_VALUE_NAME;
- achValue[] = '\0';
- Data[] = '\0';
- QString temp = "";
- retCode = RegEnumValueA(phkey, i,achValue,&cchValue,nullptr,nullptr,Data,&cbData);
- if (retCode == ERROR_SUCCESS && achValue[] != '\0')
- {
- qDebug() << i++ << achValue << " ";
- BYTE j = ;
- while(Data[j] != '\0')
- temp += (CHAR)(Data[j++]);
- qDebug() << temp;
- retArray.append(temp);
- }
- }while(achValue[] != '\0');
- /*
- * 功能:关闭注册表,返回值为是否打开成功
- */
- isSuccess = RegCloseKey(phkey);
- if(isSuccess != ERROR_SUCCESS)
- {
- qDebug() << "注册表关闭失败!";
- return;
- }
- qDebug() << "注册表关闭成功!";
- return;
- }
- /*
- * 功能:点击清空按钮功能,清空显示区的显示
- * 参数:无
- * 返回值:无
- */
- void MainWindow::on_pushButton_2_clicked()
- {
- ui->textBrowser->setText("");
- }
- /*
- * 功能:当串口有数据的时候执行,在显示区域显示
- * 串口接受到的值
- * 参数:无
- * 返回值:无
- */
- void MainWindow::readData(void)
- {
- //是否选择了该按钮,选择以16进制进行输出
- if(ui->radioButton->isChecked())
- {
- QByteArray temp = Serial->readAll().toHex();
- for(int i = ;i < temp.length();++i)
- {
- //在16进制开始加入"0x"
- if(i % == )
- ui->textBrowser->insertPlainText("0x");
- ui->textBrowser->insertPlainText((QString)temp.at(i));
- //在16进制结束加上空格" "
- if(i % == )
- ui->textBrowser->insertPlainText(" ");
- }
- }
- //没有选择则按照ASCII码输出
- else
- ui->textBrowser->insertPlainText(Serial->readAll());
- ui->textBrowser->moveCursor(QTextCursor::End);
- }
- /*
- * 功能:向串口中发送数据
- * 参数:无
- * 返回值:无
- */
- void MainWindow::on_pushButton_3_clicked()
- {
- //判断串口是否处于打开状态
- if(Serial->isOpen())
- {
- QByteArray temp = ui->textEdit->toPlainText().toUtf8();
- qDebug() << temp;
- Serial->write(temp);
- }
- else
- {
- //串口没有连接的时候发送数据就会出错
- QMessageBox messageBox(QMessageBox::Icon(),"警告","串口未连接",QMessageBox::Yes,nullptr);
- messageBox.exec();
- }
- }
- /*
- * 功能:清空发送区
- * 参数:无
- * 返回值:无
- */
- void MainWindow::on_pushButton_4_clicked()
- {
- ui->textEdit->clear();
- }
- /*
- * 功能:退出程序
- * 参数:无
- * 返回值:无
- */
- void MainWindow::on_pushButton_5_clicked()
- {
- if(Serial->isOpen())
- Serial->close();
- this->close();
- }
- /*
- * 功能:每个3s执行的任务,判断端口和串口是否打开
- * 参数:无
- * 返回值:无
- */
- void MainWindow::myThread()
- {
- qDebug() << "线程OK ";
- if(Serial->isReadable())
- ui->pushButton->setText("已启动");
- else
- ui->pushButton->setText("启动");
- this->Get_Serial_Port();
- }
mainwindow.ui
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>MainWindow</class>
- <widget class="QMainWindow" name="MainWindow">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>768</width>
- <height>500</height>
- </rect>
- </property>
- <property name="minimumSize">
- <size>
- <width>768</width>
- <height>500</height>
- </size>
- </property>
- <property name="maximumSize">
- <size>
- <width>768</width>
- <height>500</height>
- </size>
- </property>
- <property name="windowTitle">
- <string>串口助手</string>
- </property>
- <widget class="QWidget" name="centralWidget">
- <widget class="QPushButton" name="pushButton">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>230</y>
- <width>93</width>
- <height>28</height>
- </rect>
- </property>
- <property name="text">
- <string>启动</string>
- </property>
- </widget>
- <widget class="QPushButton" name="pushButton_2">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>290</y>
- <width>93</width>
- <height>28</height>
- </rect>
- </property>
- <property name="text">
- <string>清空显示</string>
- </property>
- </widget>
- <widget class="QComboBox" name="comboBox">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>50</y>
- <width>87</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QComboBox" name="comboBox_2">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>80</y>
- <width>87</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QComboBox" name="comboBox_3">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>110</y>
- <width>87</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QComboBox" name="comboBox_4">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>140</y>
- <width>87</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QComboBox" name="comboBox_5">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>170</y>
- <width>87</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QRadioButton" name="radioButton">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>295</y>
- <width>115</width>
- <height>19</height>
- </rect>
- </property>
- <property name="text">
- <string>以16进制输出</string>
- </property>
- </widget>
- <widget class="QPushButton" name="pushButton_3">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>360</y>
- <width>93</width>
- <height>28</height>
- </rect>
- </property>
- <property name="text">
- <string>发送</string>
- </property>
- </widget>
- <widget class="QGroupBox" name="groupBox">
- <property name="geometry">
- <rect>
- <x>230</x>
- <y>4</y>
- <width>551</width>
- <height>331</height>
- </rect>
- </property>
- <property name="title">
- <string>接受显示区</string>
- </property>
- <widget class="QTextBrowser" name="textBrowser">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>20</y>
- <width>521</width>
- <height>301</height>
- </rect>
- </property>
- </widget>
- </widget>
- <widget class="QGroupBox" name="groupBox_2">
- <property name="geometry">
- <rect>
- <x>230</x>
- <y>340</y>
- <width>541</width>
- <height>121</height>
- </rect>
- </property>
- <property name="title">
- <string>发送显示区</string>
- </property>
- <widget class="QTextEdit" name="textEdit">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>20</y>
- <width>521</width>
- <height>91</height>
- </rect>
- </property>
- </widget>
- </widget>
- <widget class="QPushButton" name="pushButton_4">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>400</y>
- <width>93</width>
- <height>28</height>
- </rect>
- </property>
- <property name="text">
- <string>清空发送</string>
- </property>
- </widget>
- <widget class="QLabel" name="label">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>50</y>
- <width>71</width>
- <height>21</height>
- </rect>
- </property>
- <property name="text">
- <string>串口端口</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_2">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>83</y>
- <width>80</width>
- <height>15</height>
- </rect>
- </property>
- <property name="text">
- <string>串口波特率</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_3">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>113</y>
- <width>80</width>
- <height>15</height>
- </rect>
- </property>
- <property name="text">
- <string>串口数据位</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_4">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>143</y>
- <width>80</width>
- <height>15</height>
- </rect>
- </property>
- <property name="text">
- <string>串口校验位</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_5">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>173</y>
- <width>80</width>
- <height>15</height>
- </rect>
- </property>
- <property name="text">
- <string>串口停止位</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_6">
- <property name="geometry">
- <rect>
- <x>25</x>
- <y>5</y>
- <width>191</width>
- <height>41</height>
- </rect>
- </property>
- <property name="font">
- <font>
- <family>楷体</family>
- <pointsize>12</pointsize>
- </font>
- </property>
- <property name="text">
- <string>欢迎使用调试助手</string>
- </property>
- </widget>
- <widget class="QPushButton" name="pushButton_5">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>230</y>
- <width>93</width>
- <height>28</height>
- </rect>
- </property>
- <property name="text">
- <string>退出</string>
- </property>
- </widget>
- </widget>
- <widget class="QStatusBar" name="statusBar"/>
- <widget class="QMenuBar" name="menuBar">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>768</width>
- <height>26</height>
- </rect>
- </property>
- <widget class="QMenu" name="menu">
- <property name="title">
- <string>菜单</string>
- </property>
- <addaction name="separator"/>
- <addaction name="actiondsa"/>
- </widget>
- <widget class="QMenu" name="menu_2">
- <property name="title">
- <string>帮助</string>
- </property>
- </widget>
- <addaction name="menu"/>
- <addaction name="menu_2"/>
- </widget>
- <widget class="QToolBar" name="toolBar">
- <property name="windowTitle">
- <string>toolBar</string>
- </property>
- <attribute name="toolBarArea">
- <enum>TopToolBarArea</enum>
- </attribute>
- <attribute name="toolBarBreak">
- <bool>false</bool>
- </attribute>
- </widget>
- <action name="actiondsa">
- <property name="text">
- <string>退出</string>
- </property>
- </action>
- </widget>
- <layoutdefault spacing="6" margin="11"/>
- <resources/>
- <connections/>
- </ui>
界面的实际效果为:
串口调试助手--Qt的更多相关文章
- Ubuntu Linux TinySerial串口调试助手 可视化界面 安装使用
ubuntu Linux下串口调试助手使用 Tiny Serial为一个开源项目,欢迎大家使用,基于Qt开发的串口调试助手,有一般串口助手的基本功能,更多功能正在完善. Github地址:https: ...
- 问题解决——使用串口调试助手发送控制字符 协议指令 <ESC>!?
外行指挥内行的结果就是,你必须按照他想的去做,等做不出来再用自己的办法,而且必须如此. -------------------------------------------------------- ...
- USB转串口连接线与串口调试助手的使用
---作者吴疆,未经允许,严禁转载,违权必究--- ---欢迎指正,需要源码和文件可站内私信联系--- -----------点击此处链接至博客园原文----------- 功能说明:宇泰UT-890 ...
- 串口调试助手---VB源码
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- 基于串口调试助手的WIFI模块调试-FPGA简单联网(点灯)
根据正点原子的<ATK-ESP8266 WIFI用户手册>,使用XCOM V2.2串口调试助手测试WIFI模块[26].在本系统中运用到的功能主要是TCP/IP模式中的TCP Client ...
- 基于开源串口调试助手修改的qcom
代码已上传码云: https://gitee.com/fensnote/qcom.git 源代码用于串口编程的学习很有价值,谢谢Qter的开源项目,感谢花心萝卜工作室的修改版本. 开源的qt开发的串口 ...
- 11-51单片机ESP8266学习-AT指令(ESP8266作为TCP客户端,连接TCP服务器,用串口调试助手和手机TCP调试助手测试)
写完题目刚想起来一件事情,如果手机作为客户端(不连接路由器的情况下),手机连接模块的无线会分配一个IP地址,,,这个IP地址事先我也不知道....我先看看AT指令里面有没有一个指令可以打印一下连接自己 ...
- 4-51单片机ESP8266学习-AT指令(测试TCP服务器--使用串口调试助手--不连接路由器)
上一篇连接 http://www.cnblogs.com/yangfengwu/p/8757513.html 源码链接:https://pan.baidu.com/s/1wT8KAOIzvkOXXN ...
- C# 串口调试助手源码
本方法,禁用跨进程错误(做法不太好,但是对于单片机出身的人来说,好理解,能用就行). 基本功能: 1.点串口号的下拉菜单自动当前检索设备管理器的COM 2.发送模式可选,hex和string两种 3. ...
随机推荐
- vscode搭建springboot开发环境
1. JDK,Maven 1.1 下载略 1.2 设置系统环境变量 jdk增加环境变量JAVA_HOME=C:\Program Files\Java\jdk1.8.0_191(安装路径) 增加路径Pa ...
- Linux下vim卡死原因
使用vim的时候,偶尔会碰到vim莫名其妙的僵在那里. 解决方案: 经查,原来Ctrl+S在Linux里是锁定屏幕的快捷键,如果要解锁,按下Ctrl+Q就可以了. 经验总结: 牢记这两个VIM组合键 ...
- js对元素判断
$("input[type='text']").attr("readonly","readonly"); $("textarea& ...
- Java的反射是什么?有什么用?
首先我要简单的来说一下什么是Java的反射机制: 在Java里面一个类有两种状态--编译和运行状态,通常我们需要获取这个类的信息都是在编译阶段获得的,也就是直接点出来或者new出来,可是如果需要在类运 ...
- SQL按照顺序时间段统计
借助master..spt_values表 按照时间(半小时)划分统计时间段: select ,dateInfo.dday) as time) StartTime, ,),dateInfo.dday) ...
- JavaWeb基础知识
一.WEB基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web ...
- LTDC/DMA2D——液晶显示
1.显示器的基本参数 (1) 像素像素是组成图像的最基本单元要素,显示器的像素指它成像最小的点. (2) 分辨率一些嵌入式设备的显示器常常以“行像素值 x列像素值”表示屏幕的分辨率.如分辨率 800x ...
- 项目管理工具-OmniPlan 3 for Mac
链接:https://pan.baidu.com/s/1tp_37fHXHwJuklL1nNSwig 密码:l0sf 激活迷药(里面自带的keygen不能用,用这个好使): Name: Appked ...
- 超详细Qt5.9.5移植攻略
本文就来介绍下如何将Qt5.9.5移植到ARM开发板上. 以imx6开发板为例,使用Ubuntu14.04虚拟机作为移植环境. 准备工作 1.主机环境:Ubuntu14.04: 开发板:启扬IAC-I ...
- Django开发简单采集用户浏览器信息的小功能
Django开发简单采集用户浏览器信息的小功能 Centos环境准备 yum install –y python-pip export http_proxy=http://10.11.0.148:80 ...