1 Star 0 Fork 12

MageeLow / Qt小项目之串口助手

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Qt小项目之串口助手控制LED

前言

最近刚学了一点Qt开发上位机,尝试着做个小软件练练手。查找了很多资料,做了一个简单的串口助手,可以实现串口基本发送和接收功能,支持中文显示,还可以控制STM32开发板上的两个LED。

1.软件界面

2.主要功能:

  • 启动自动搜索本机串口,或者手动点击搜索键扫描串口
  • 自定义波特率
  • 支持中文显示
  • 支持发送新行

3.实际效果:

花了大概3天时间吧,找了很多资料,功能很简单, 但想着是自己一点一点开发的,还是挺有成就感的哈!

写这篇文章是为了总结一下开发的过程和一些知识点,主要包括两部分,上位机的实现和STM32端程序的实现。

Qt上位机的实现

0.新建一个Dialog项目

新建一个Dialog项目,这3种基类的区别可以根据你的程序来确定。

  • 如果需要嵌入到其他窗体中,则基于QWidget创建。
  • 如果是主窗体,则基于QMainWindow创建,有菜单栏,状态栏,工具栏等。
  • 如果是顶级对话框,则基于QDialog创建。

1.软件UI界面的设计

使用Qt Designer添加所需要的控件,并进行合理布局,尽量每一个控件,起一个合理易懂的名字。

2.串口库的添加

pro文件添加一行:

QT += serialport

对应的头文件包含:

#include <QSerialPort>
#include <QSerialPortInfo>

3.串口自动搜索功能的实现

自动搜索本机串口,并在ComboBox中添加串口号

ui->cbb_com->clear();
//运行开始查找可用串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
    ui->cbb_com->addItem(info.portName());  //串口号下拉菜单,增加一个条目,为串口号COM4
    qDebug() << "串口搜索完成";
}

4.串口的配置和打开关闭

//打开串口按钮
void Dialog::on_btn_uart_Ctrl_clicked()
{
//    static bool flag;   //也可以用标志位实现
    if(this->ui->btn_uart_Ctrl->text() == "打开串口")   //初始状态,配置串口参数
    {
        serial.setPortName(ui->cbb_com->currentText());     //设置串口号、
        serial.setBaudRate(ui->cbb_baud->currentText().toInt());    //设置波特率
        serial.setDataBits(QSerialPort::Data8);     //设置串口数据位8
        serial.setParity(QSerialPort::NoParity);    //无校验位
        serial.setStopBits(QSerialPort::OneStop);   //1位停止位
        serial.setFlowControl(QSerialPort::NoFlowControl);
        //打开串口
        if(!serial.open(QIODevice::ReadWrite))
        {
            QMessageBox::critical(NULL, "提示", "串口打开失败");
            return;
        }
        qDebug() << "串口打开成功";
        this->ui->btn_uart_Ctrl->setText("关闭串口");
    }
    else
    {
        //关闭串口
        serial.close();
        this->ui->btn_uart_Ctrl->setText("打开串口");
    }
}

5.串口发送数据

    serial.write("A1\n");     //串口发送A1

6.串口数据的接收和显示,支持中文

QT默认的编码是unicode,不能显示中文的,windows默认使用(GBK/GB2312/GB18030),使用了fromLocal8Bit()函数,实现了从Unicode到本地字符集GBK的转换,用于处理汉语显示乱码等问题

槽函数的实现:

//串口数据接收并显示
void Dialog::serialPort_readyRead()
{
    QByteArray rx_buf = serial.readAll();   //读取串口接收的数据
    if(rx_buf.endsWith("\r\n")) //判断接收最后是否是回车换行,即接收完成标志
    {

    }
    QString rx_buf_tmp = QString::fromLocal8Bit(rx_buf);    //转换为中文格式
    qDebug() << rx_buf_tmp;         //控制台输出

    ui->tb_rx_buf->append(rx_buf_tmp);

    rx_buf_tmp.clear();
    rx_buf.clear();
}

connect语句:

connect(&serial, & QSerialPort::readyRead, this, &Dialog::serialPort_readyRead);

7.下拉框自定义波特率的实现

//自定义波特率
void Dialog::on_cbb_baud_currentIndexChanged(const QString &arg1)
{
    if(this->ui->cbb_baud->currentIndex() == 3)
    {
        this->ui->cbb_baud->setItemText(3, ""); //调成自定义波特率时,内容设置为空,准备接收输入
        this->ui->cbb_baud->setEditable(true);
    }
    else
    {
        this->ui->cbb_baud->setItemText(3, "自定义"); //调成自定义波特率时,内容设置为空,准备接收输入
        this->ui->cbb_baud->setEditable(false);
    }
    serial.setBaudRate(ui->cbb_baud->currentText().toInt());    //即使打开串口后,仍然可以设置波特率
}

8.发送新行功能的实现

通过一个全局变量实现,发送新行按钮勾选时,标志位置1,然后发送按钮功能里,根据标志位决定是否在末尾添加换行符。

对应的槽函数实现:

//是否发送新行
void Dialog::on_cb_send_enter_clicked()
{
    if(ui->cb_send_enter->isChecked())
    {
        send_enter_flag = true;
        qDebug() << "发送新行";
    }
    else
    {
        send_enter_flag = false;
        qDebug() << "不发送新行";
    }
}
//发送按钮被按下
void Dialog::on_btn_send_clicked()
{
    //获取多行输入框的数据并转换为UTF8格式
    QByteArray tx_buf = ui->te_tx_buf->toPlainText().toUtf8();

    if(send_enter_flag == true)
        tx_buf += "\n";

    serial.write(tx_buf);     //把数据通过串口发送出去
    tx_buf.clear();
}

9.只改变标签颜色

本来想着通过改变样式表的方式改变颜色

    this->ui->lbe_blue->setStyleSheet("color: rgb(255, 0, 0);");

但是,实际运行时,连字体和大小都改成了默认的,有没有一种只改变颜色其他的格式不变的方法呢?还真有,如下,不过好像只支持标准颜色?

	QPalette colr;
    colr.setColor(QPalette::WindowText,Qt::red);    //设置标签颜色红色
    this->ui->lbe_red->setPalette(colr);

10.按钮的使能失能控制

以下两行语句效果相同,都是失能按钮功能:

this->ui->btn_led1_Ctrl->setDisabled(true); //LED控制按钮不可用
this->ui->btn_led1_Ctrl->setEnabled(false); //LED控制按钮不可用

11.文本显示框设置最大显示行数

this->ui->tb_rx_buf->document()->setMaximumBlockCount(10);

程序的图标、标题设置和打包发布

你不希望窗口的标题是“Dialog”吧,所以添加一个标题和一个好看的图标还是很有必要的。

1.添加标题

添加窗口标题还是很简单的,一行代码:

this->setWindowTitle("串口控制LED - By wcc ");

2.添加icon图标

  • 找一个好看的图标,格式一定要是.ico,像素大小推荐128*128
  • 命名为my_app.ico,名字无所谓,不要有中文就好了,放在工程目录下,即和.pro文件和.cpp文件同一个目录。
  • 打开.pro文件,最底下添加一行:RC_ICONS = my_app.ico

重新编译就可以看到这种效果了。

3.程序文件的生成

构建选项改成Release版本,编译完成后,会在Release目录下生成一个.exe文件,把这个文件单独拷出来放在一个空白的文件夹里,如D:\QT_Prj\Export\UART_Demo.exe,可以运行试一下,会提示缺少运行所需要的dll组件

而且,这个文件如果单独拷贝到其他没有安装Qt环境的电脑上,也是不能运行的。

所以我们需要添加一些当前程序运行所需要的组件才能正常运行,但是需要添加哪些文件呢?不用担心,Qt早已经想好了,运行MinGW工具:

先进入到exe文件所在的文件夹中:cd /d D:\QT_Prj\Export

然后输入命令:windeployqt UART_Demo.exe

此时,打开exe文件所在的文件夹,可以看到Qt已经为我们添加好了,当前程序运行所需要的组件了。

这个时候,如果想给别人分享你开发好的上位机软件,就可以直接把这个文件夹拷贝给他。当然也可以安装一个Enigma Virtual Box软件,把当前目录下的所有文件打包成一个exe文件。

STM32端程序的实现

连接串口模块,发送接收短接,可以看出Qt上位机的的收发都是正常的。下一步就是编写STM32端的程序了,很简单,当接收到字符串"A1"时,点亮红灯;当接收到字符串“A2”时,熄灭红灯;当接收到字符串“B1”时,点亮蓝灯;当接收到字符串“B2”时,熄灭蓝灯,每个字符串结尾都有换行符“\n”,即:

上位机发送 单片机执行
A1 红灯点亮
A2 红灯熄灭
B1 蓝灯点亮
B2 蓝灯熄灭

实现思路也很简单,即把接收到的字符存入一个字符数组,当接收到“\n”换行标志时,意味着接收完成,判断此时数组的内容,分别和命令比较,如果一致,执行相应的操作,串口1中断服务函数:

void USART1_IRQHandler(void)
{
	char dat;
	char flag = 0;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)	//接收中断
	{
		dat = USART1->DR;
		if(usart1Len >= 64)									//防止数据过多,导致内存溢出
			usart1Len = 0;
		if(dat == 0x0D || dat == 0x0A)		//回车换行,接收完成,此时的buf不含回车换行
		{
			if(strcmp(usart1Buf, "A2") == 0)	//字符串比较
			{
				UsartPrintf(USART1, "红灯熄灭\r\n");
				GPIO_SetBits(GPIOB, GPIO_Pin_9);
			}
			else if(strcmp(usart1Buf, "A1") == 0)
			{
				UsartPrintf(USART1, "红灯点亮\r\n");
				GPIO_ResetBits(GPIOB, GPIO_Pin_9);			
			}
			else if(strcmp(usart1Buf, "B2") == 0)
			{
				UsartPrintf(USART1, "蓝灯熄灭\r\n");
				GPIO_SetBits(GPIOB, GPIO_Pin_6);			
			}			
			else if(strcmp(usart1Buf, "B1") == 0)
			{
				UsartPrintf(USART1, "蓝灯点亮\r\n");
				GPIO_ResetBits(GPIOB, GPIO_Pin_6);			
			}	
			usart1Len = 0;
			memset(usart1Buf,0,64);
		}
		else
		{
			usart1Buf[usart1Len++] = dat;
		}
		USART_ClearFlag(USART1, USART_FLAG_RXNE);
	}
}

程序还是很简单。板子是用的中移的麒麟座Mini板,基于F103C8T6的,串口1连接上位机,波特率115200,PB9-红灯,PB6-绿灯,都是低电平点亮。

改进和优化的地方

  • 按钮发送字符可自定义
  • 界面UI的设计优化
  • 数据波形的显示
  • 发送和接收,16进制和字符模式的切换
  • 定时发送功能
  • 接收内容保存成文件
  • 一个小Bug,不支持多个串口的自动搜索。

Qt工程和STM32工程下载

由于国内Github下载速度实在令人着急,Qt工程文件和STM32工程文件,还包括Enigma_Virtual_Box的安装包,我都已经上传到国内的码云Gitee上了,有需要的朋友可以在Git中使用以下命令下载:

git clone https://gitee.com/whik/qt_uart_demo.git

或者是在公众号后台回复**【串口助手】**,我会把下载链接发送给你。

当然,如果有朋友也在学习Qt开发上位机,欢迎互相交流学习。


历史精选文章:


欢迎大家关注我的个人博客

或微信扫码关注我的公众号

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

qt学习之串口控制LED 展开 收起
C++
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/1075504040/qt_uart_demo.git
git@gitee.com:1075504040/qt_uart_demo.git
1075504040
qt_uart_demo
Qt小项目之串口助手
master

搜索帮助