• QT-串口工具


    一、演示效果

    请添加图片描述

    二、关键程序

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    #include 
    
    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] Plugins
        connect(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] i18n
        connect(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);
    #endif
        QString 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);
            } else
                QMessageBox::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);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225

    三、下载链接

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

  • 相关阅读:
    多线程与高并发——并发编程(8)
    python web框架 flask基础入门教程
    云原生核心技术之:Service Mesh 的实现—— Istio
    OpenHarmony 持久化存储 UI 状态:PersistentStorage
    day36(http协议 服务器软件的使用 PHP的简单了解 前后端交互 ajax 同步和异步)
    golang opt-in transparent telemetry
    基于关联规则的多样化推荐技术应用研究
    根据ASCII码值计算 excel单元格名
    python中pydantic库
    數據集成平台:datax將MySQL數據以query方式同步到hive
  • 原文地址:https://blog.csdn.net/u013083044/article/details/136251296