QT-串口工具

一、演示效果

请添加图片描述

二、关键程序

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QMessageBox>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow),listPlugins(QList<TabPluginInterface *>()),translator(new QTranslator())
{ui->setupUi(this);tabCOMSimple = new TabCOMSimple(this);ui->tabMain->addTab(tabCOMSimple, tr("Simple"));tabAdvanced = new TabAdvanced(this);ui->tabMain->addTab(tabAdvanced, tr("Advanced"));decoder = new Decoder(this, tabAdvanced->getListProtocals(), tabAdvanced->getEndianess());// [start] Data stream connections.connect(decoder, &Decoder::rawDataReady, tabCOMSimple, &TabCOMSimple::rawDataReady);connect(decoder, &Decoder::frameReady, tabAdvanced, &TabAdvanced::frameDataReady);// [End]// [Start] Pluginsconnect(ui->actionLoad_Plugin, &QAction::triggered, this, &MainWindow::onLoadPluginTriggered);connect(decoder, &Decoder::frameReady, this, &MainWindow::onDecodedDataReady);QString folderString = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/QSerial Socket Amigo";QFileInfo folder(folderString);if (!folder.exists())QDir().mkdir(folderString);folderString.append("/plugins");QFileInfo pluginsFolder(folderString);if (!pluginsFolder.exists())QDir().mkdir(folderString);// [End]// [Start] i18nconnect(ui->actionEnglish, &QAction::triggered, this, &MainWindow::onActionEnglishTriggered);ui->menuLanguage->addAction(tr(u8"简体中文"), this, &MainWindow::onActionChineseTriggered);// [End]ui->groupNetProperties->setDisabled(true);connect(ui->radioSerial, &QRadioButton::clicked, this, &MainWindow::onConnectionTypeChanged);connect(ui->radioNetSocket, &QRadioButton::clicked, this, &MainWindow::onConnectionTypeChanged);connect(ui->buttonOpen, &QPushButton::clicked, this, &MainWindow::openConnection);serialDevice = new SerialDevice(this, ui->comboPorts, ui->buttonRefreshPorts,ui->comboBaudrate, ui->comboDataBits,ui->comboParity, ui->comboStopBits,ui->comboFlowControl);netSocketDevice = new NetSocketDevice(this, ui->inputNetIPAddr, ui->inputNetPort,ui->radioNetTypeTCP, ui->radioNetTypeUDP,ui->radioNetRoleClient, ui->radioNetRoleServer);connect(serialDevice, &SerialDevice::connected, this, &MainWindow::onDeviceConnected);connect(netSocketDevice, &SerialDevice::connected, this, &MainWindow::onDeviceConnected);connect(serialDevice, &SerialDevice::errorDisconnected, this, &MainWindow::onDeviceErrorDisconnected);connect(netSocketDevice, &SerialDevice::errorDisconnected, this, &MainWindow::onDeviceErrorDisconnected);commDevice = serialDevice;// [start] Log stream connections.connect(tabCOMSimple, &TabCOMSimple::log, this, &MainWindow::log);connect(netSocketDevice, &NetSocketDevice::log, this, &MainWindow::log);// [end]translateTo("zh");this->setWindowTitle(u8"串口工具");
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::onConnectionTypeChanged(bool isChecked)
{Q_UNUSED(isChecked)if (ui->radioSerial->isChecked()) {ui->groupNetProperties->setDisabled(true);ui->groupSerialProperties->setEnabled(true);commDevice = serialDevice;} else if (ui->radioNetSocket->isChecked()) {ui->groupNetProperties->setEnabled(true);ui->groupSerialProperties->setDisabled(true);commDevice = netSocketDevice;}
}void MainWindow::closeDevice()
{commDevice->close();ui->groupConnSel->setEnabled(true);ui->buttonOpen->setText(tr("Open Connection"));tabAdvanced->setAllowRunning(false);
}void MainWindow::openConnection()
{if (ui->groupConnSel->isEnabled()) {// This acts like sending connect command, device will signals conncted if so.int ret = commDevice->open();if (ret != 0) {return;}} else {closeDevice();}
}// Only when device is really connected.
void MainWindow::onDeviceConnected()
{tabCOMSimple->bindIODevice(commDevice->ioDevice);decoder->setConnection(commDevice->ioDevice);ui->groupConnSel->setDisabled(true);ui->buttonOpen->setText(tr("Close Connection"));tabAdvanced->setAllowRunning(true);
}void MainWindow::onDeviceErrorDisconnected()
{closeDevice();
}void MainWindow::log(QString str)
{ui->textLog->append(str);
}void MainWindow::onLoadPluginTriggered()
{QString pluginsFolder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/QSerial Socket Amigo/plugins";
#if defined(Q_OS_WIN)QString type = tr("Dynamic Linked Library (*.dll)");
#elif defined (Q_OS_LINUX)QString type = tr("Shared Library (*.so)");
#else//Not supporting Mac.Q_ASSERT(false);
#endifQString fileName = QFileDialog::getOpenFileName(this,tr("Select Plugin to Load"),pluginsFolder,type);if (fileName.isEmpty())return;else {QPluginLoader loader(fileName);QObject *pluginObject = loader.instance();if (pluginObject) {TabPluginInterface *plugin = qobject_cast<TabPluginInterface *>(pluginObject);plugin->setConnection(currentConnection);listPlugins.append(plugin);QWidget *widget = new QWidget();widget->setLayout(plugin->getLayout());ui->tabMain->addTab(widget, plugin->getName());ui->tabMain->setCurrentIndex(ui->tabMain->count() - 1);} elseQMessageBox::warning(this, tr("error"), tr("plugin read error"));}
}void MainWindow::onDecodedDataReady(int id, QList<double> listValues)
{for (auto plugin : listPlugins)plugin->onFrameUpdated(id, listValues);
}// Two characters locale, eg: en, zh, de.
void MainWindow::translateTo(QString locale)
{QString qmPath = qApp->applicationDirPath().append("/Serial-Amigo_");translator->load(qmPath.append(locale).append(".qm"));qApp->installTranslator(translator);}void MainWindow::retranslateUi()
{ui->tabMain->setTabText(1, QCoreApplication::translate("MainWindow", "Simple"));ui->tabMain->setTabText(2, QCoreApplication::translate("MainWindow", "Advanced"));ui->comboParity->setItemText(0, QCoreApplication::translate("MainWindow", "NoParity"));ui->comboParity->setItemText(1, QCoreApplication::translate("MainWindow", "EvenParity"));ui->comboParity->setItemText(2, QCoreApplication::translate("MainWindow", "OldParity"));ui->comboParity->setItemText(3, QCoreApplication::translate("MainWindow", "SpaceParity"));ui->comboParity->setItemText(4, QCoreApplication::translate("MainWindow", "MarkParity"));ui->comboFlowControl->setItemText(0, QCoreApplication::translate("MainWindow", "No"));ui->comboFlowControl->setItemText(1, QCoreApplication::translate("MainWindow", "Hard"));ui->comboFlowControl->setItemText(2, QCoreApplication::translate("MainWindow", "Soft"));
}void MainWindow::onActionChineseTriggered()
{translateTo("zh");
}void MainWindow::onActionEnglishTriggered()
{translateTo("en");
}void MainWindow::changeEvent(QEvent *event)
{switch (event->type()) {case QEvent::LanguageChange:ui->retranslateUi(this);retranslateUi();break;default:break;}QMainWindow::changeEvent(event);
}void MainWindow::updatePluginConnection()
{for (auto plugin : listPlugins)plugin->setConnection(commDevice->ioDevice);
}

三、下载链接

https://download.csdn.net/download/u013083044/88867720

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Git企业实战开发】Git常用开发流操作总结

【Git企业实战开发】Git常用开发流操作总结 大家好 我是寸铁&#x1f44a; 总结了一篇Git常用开发流操作总结的文章✨ 喜欢的小伙伴可以点点关注 &#x1f49d; 现在刚做项目的伙伴&#xff0c;可能你之前学过git&#xff0c;但是一实战发现不熟悉 没关系&#xff0c;看寸铁这篇…

【Maven】介绍、下载及安装、集成IDEA

目录 一、什么是Maven Maven的作用 Maven模型 Maven仓库 二、下载及安装 三、IDEA集成Maven 1、POM配置详解 2、配置Maven环境 局部配置 全局设置 四、创建Maven项目 五、Maven坐标详解 六、导入Maven项目 方式1&#xff1a;使用Maven面板&#xff0c;快速导入项目 …

Oladance、南卡、韶音开放式耳机怎么样?3个月真实对比测评

​哪款开放式耳机好用&#xff1f;我亲自体验并评测了市场上流行的三个品牌的开放式耳机&#xff1a;Oladance、南卡、韶音。通过深入测试多维度性能表现&#xff0c;确保你能够远离劣质产品可能带来的问题。我想提醒大家&#xff0c;如果选错耳机可能会影响到音乐的真实还原和…

嵌入式学习-qt-Day3

嵌入式学习-qt-Day3 一、思维导图 二、作业 完善对话框&#xff0c;点击登录对话框&#xff0c;如果账号和密码匹配&#xff0c;则弹出信息对话框&#xff0c;给出提示”登录成功“&#xff0c;提供一个Ok按钮&#xff0c;用户点击Ok后&#xff0c;关闭登录界面&#xff0c;跳…

放大电路静态工作点的稳定

什么对Q点有影响&#xff1f; 温度、电源&#xff08;VCC&#xff09;的波动、元器件的老化等。 对于电源的波动&#xff0c;可以用好的电源来抑制&#xff0c;使波动变得小&#xff1b;对于元器件的老化&#xff0c;可以在出厂前老化一下&#xff0c;后面可以稳定用很长时间…

协程源码 launch 流程跟踪学习

为了更深入学习协程的底层实现原理&#xff0c;了解协程线程切换的根本本质。也为了以后在工作中可以根据不同的需求场景&#xff0c;更加随心所欲的使用不同的协程。 今天通过 launch 跟踪一下协程的执行流程。 fun getData() {Trace.beginSection("getData");Log.…

vue3中使用vuedraggable实现拖拽el-tree数据进分组

看效果&#xff1a; 可以实现单个拖拽、双击添加、按住ctrl键实现多个添加&#xff0c;或者按住shift键实现范围添加&#xff0c;添加到框中的数据&#xff0c;还能拖拽排序 先安装 vuedraggable 这是他的官网 vue.draggable中文文档 - itxst.com npm i vuedraggable -S 直接…

构建高效稳定的外卖平台架构设计与实现

外卖行业的快速发展为人们的生活带来了便利&#xff0c;随着外卖市场的扩大和竞争的加剧&#xff0c;外卖平台的架构设计变得至关重要。一个高效稳定的架构可以支持平台的快速发展&#xff0c;提供优质的服务体验&#xff0c;同时保障用户数据的安全性。 用户端架构设计 移动端…

11.CSS3的媒介(media)查询

CSS3 的媒介(media)查询 经典真题 如何使用媒体查询实现视口宽度大于 320px 小于 640px 时 div 元素宽度变成 30% 媒体查询 媒体查询英文全称 Media Query&#xff0c;顾名思义就是会查询用户所使用的媒体或者媒介。 在现在&#xff0c;网页的浏览终端是越来越多了。用户可…

【elementUi-table表格】 滚动条 新增监听事件; 滚动条滑动到指定位置;

1、给滚动条增加监听 this.dom this.$refs.tableRef.bodyWrapperthis.dom.scrollTop 0let _that thisthis.dom.addEventListener(scroll, () > {//获取元素的滚动距离let scrollTop _that.dom.scrollTop//获取元素可视区域的高度let clientHeight this.dom.clientHeigh…

vue实现拖拽(vuedraggable)

实现效果: 左侧往右侧拖动&#xff0c;右侧列表可以进行拖拽排序。 安装引用&#xff1a; npm install vuedraggable import draggable from vuedraggable 使用&#xff1a; data数据&#xff1a; componentList: [{groupName: 考试题型,children: [{componentType: danxua…

Ansible安装管理和模块的使用

目录 Ansible是什么 ansible 环境安装部署 管理端安装 ansible ansible 目录结构 配置主机清单 配置密钥对验证 ansible 命令行模块 1&#xff0e;command 模块 在远程主机执行命令&#xff0c;不支持管道&#xff0c;重定向等shell的特性 常用的参数 2&#xff0e;sh…