From 23c75b57de6e882c44fac155eb8d6cab41da1d5d Mon Sep 17 00:00:00 2001 From: zhangwei Date: Thu, 18 Apr 2024 07:03:30 +0000 Subject: [PATCH] revert 920f3fa341d675d0013e0e7a968703c864bcb373 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert 更新自动重连串口功能,若人为点击“打开串口”,则只要串口连接失败,就会在10秒后重新尝试连接;直到连接成功或人为点击“关闭串口” --- src/ConfigUtil/ConfigUtil.cpp | 1 - src/ConfigUtil/ConfigUtil.h | 2 - src/common/common.h | 9 -- src/qt_source/mainwindow.cpp | 177 ++++++++------------------------- src/qt_source/mainwindow.h | 3 - src/serial/ComDetect.cpp | 3 +- src/serial/ComDetect.h | 1 - src/threads/UpResultThread.cpp | 6 +- 8 files changed, 48 insertions(+), 154 deletions(-) diff --git a/src/ConfigUtil/ConfigUtil.cpp b/src/ConfigUtil/ConfigUtil.cpp index 98082dc..5fc8615 100644 --- a/src/ConfigUtil/ConfigUtil.cpp +++ b/src/ConfigUtil/ConfigUtil.cpp @@ -73,7 +73,6 @@ bool ConfigUtil::readSocketServerConfig(const QString &configFile, QString &erro mset->beginGroup("socket_server"); config.server_ip = mset->value("server_ip", "").toString(); config.server_port = mset->value("server_port", "").toInt(); - config.delayed_upload = mset->value("delayed_upload", "").toInt(); mset->endGroup(); } catch (const std::exception &e) { diff --git a/src/ConfigUtil/ConfigUtil.h b/src/ConfigUtil/ConfigUtil.h index d4edac9..23da3e9 100644 --- a/src/ConfigUtil/ConfigUtil.h +++ b/src/ConfigUtil/ConfigUtil.h @@ -46,8 +46,6 @@ namespace ai_matrix { QString server_ip; // server_port int server_port; - // 识别结果延后X节上传,以等待Socket反馈火车运行方向 - int delayed_upload; }; }; diff --git a/src/common/common.h b/src/common/common.h index 0593c34..c35cf20 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -29,15 +29,6 @@ namespace ai_matrix { std::string strRfidInfo; }; - struct ComeTrain { - QString trainTime; - bool needUpLoad; - ComeTrain () { - this->trainTime = ""; - this->needUpLoad = false; - }; - }; - } diff --git a/src/qt_source/mainwindow.cpp b/src/qt_source/mainwindow.cpp index afa0054..4a85ef0 100644 --- a/src/qt_source/mainwindow.cpp +++ b/src/qt_source/mainwindow.cpp @@ -142,7 +142,7 @@ MainWindow::MainWindow(QWidget *parent) } }); connect(this->tcpClient, &TcpClient::getDirectionSignals, this, [=](int direction) { - if (this->iDirection == 0) + if (this->iDirection != direction) { this->logInfo("获得方向判定:" + QString(direction > -1 ? (direction > 0 ? "需识别" : "待定") : "无需上传")); this->iDirection = direction; @@ -451,33 +451,6 @@ void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reaso } /********************************串口操作*************************************/ -bool MainWindow::connectCom() -{ - this->serial_ = this->comDetect_.openCom(ui->comBox->currentText(), ui->BTBox->currentText().toInt()); - - if (this->serial_ != nullptr) { - QString logs; - logs.append("串口打开成功\n"); - logs.append("串口号:" + ui->comBox->currentText() + "\n"); - logs.append("波特率:" + ui->BTBox->currentText() + "\n"); - if (this->baseConfig_.havaMagnetSteel) - logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder); - this->isOpenCom = true; - this->logInfo(logs); - this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " - "background-color: rgba(74, 221, 108, 225);\ " - "border:1px solid rgba(168, 168, 168, 105);"); - connect(this->serial_, &QSerialPort::readyRead, this, &MainWindow::readCom); // - - connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error); - return true; - } - else - { - return false; - } -} - /** * 打开串口 */ @@ -485,89 +458,52 @@ void MainWindow::openComClicked() { if (ui->openComButton->text() == "打开串口") { - this->auto_reconnect_serial_ = true; - this->ui->openComButton->setText("关闭串口"); - QtConcurrent::run([=](){ - while (this->auto_reconnect_serial_ && !this->isOpenCom) { - if (!this->connectCom()) - { - this->isOpenCom = false; - this->comDetect_.closeCom(); - this->logInfo("串口打开失败!10秒钟后将会重连..."); - } else { - return ; - } - QThread::sleep(10); - } - }); - - - -// this->serial_ = this->comDetect_.openCom(ui->comBox->currentText(), ui->BTBox->currentText().toInt()); -// -// if (this->serial_ != nullptr) -// { -// QString logs; -// logs.append("串口打开成功\n"); -// logs.append("串口号:" + ui->comBox->currentText() + "\n"); -// logs.append("波特率:" + ui->BTBox->currentText() + "\n"); -// if (this->baseConfig_.havaMagnetSteel) -// logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder); -// this->isOpenCom = true; -// this->logInfo(logs); -//// this->ui->openComButton->setText("关闭串口"); -// this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " -// "background-color: rgba(74, 221, 108, 225);\ " -// "border:1px solid rgba(168, 168, 168, 105);"); -// connect(this->serial_,&QSerialPort::readyRead,this,&MainWindow::readCom); // -// -// connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error); -// -// } -// else -// { -// this->isOpenCom = false; -// this->comDetect_.closeCom(); -// this->logInfo("串口打开失败!10秒钟后将会重连..."); -// this->ui->openComButton->setText("关闭串口"); -// -// QtConcurrent::run([=](){ -// QThread::sleep(10); -// if (this->auto_reconnect_serial_ && !this->isOpenCom) { -// ui->openComButton->click(); -// } -// }); -// } - } - else - { - this->auto_reconnect_serial_ = false; - if (this->isOpenCom) + this->serial_ = this->comDetect_.openCom(ui->comBox->currentText(), ui->BTBox->currentIndex()); + if (this->serial_ != nullptr) { - // 弹出对话框要求用户输入密码 - QString password = QInputDialog::getText(this, "密码验证", "请输入密码:", QLineEdit::Password); + QString logs; + logs.append("串口打开成功\n"); + logs.append("串口号:" + ui->comBox->currentText() + "\n"); + logs.append("波特率:" + ui->BTBox->currentText() + "\n"); + if (this->baseConfig_.havaMagnetSteel) + logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder); + this->isOpenCom = true; + this->logInfo(logs); + this->ui->openComButton->setText("关闭串口"); + this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " + "background-color: rgba(74, 221, 108, 225);\ " + "border:1px solid rgba(168, 168, 168, 105);"); + connect(this->serial_,&QSerialPort::readyRead,this,&MainWindow::readCom); // + + connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error); - // 验证账号和密码是否正确 - if (password == "matrix") { - if (this->comDetect_.closeCom()) - { - if (this->needIdentify == true) this->needIdentify = false; - this->isOpenCom = false; - this->logInfo("串口已关闭!"); - this->initParam(); - this->ui->openComButton->setText("打开串口"); - this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " - "background-color: red;\ " - "border:1px solid rgba(168, 168, 168, 105);"); - } - } else { - QMessageBox::warning(this, "验证失败", "密码不正确!"); - } } else { - this->logInfo("串口已停止访问!"); - this->ui->openComButton->setText("打开串口"); + this->isOpenCom = false; + this->logInfo("串口打开失败!"); + } + } + else + { + // 弹出对话框要求用户输入密码 + QString password = QInputDialog::getText(this, "密码验证", "请输入密码:", QLineEdit::Password); + + // 验证账号和密码是否正确 + if (password == "matrix") { + if (this->comDetect_.closeCom()) + { + if (this->needIdentify == true) this->needIdentify = false; + this->isOpenCom = false; + this->logInfo("串口已关闭!"); + this->initParam(); + this->ui->openComButton->setText("打开串口"); + this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " + "background-color: red;\ " + "border:1px solid rgba(168, 168, 168, 105);"); + } + } else { + QMessageBox::warning(this, "验证失败", "密码不正确!"); } } } @@ -587,12 +523,6 @@ void MainWindow::serialPort_error(QSerialPort::SerialPortError error) this->ui->LEDlabel->setStyleSheet("border-radius: 16px; \ " "background-color: red;\ " "border:1px solid rgba(168, 168, 168, 105);"); - QtConcurrent::run([=](){ - QThread::sleep(10); - if (this->auto_reconnect_serial_ && !this->isOpenCom) { - this->connectCom(); - } - }); } } @@ -736,12 +666,6 @@ void MainWindow::upRfid(QString rfidInfo) QString info = this->vecTrain.back(); int order = this->vecTrain.size(); - if (order > this->socketServerConfig_.delayed_upload && this->iDirection == 0) - { - this->iDirection = 1; - } - if (!this->baseConfig_.useSocketServer) this->iDirection = 1; - info = info.mid(1,info.size() - 1); TrainInfo trainInfo; @@ -752,20 +676,7 @@ void MainWindow::upRfid(QString rfidInfo) trainInfo.collectTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString(); trainInfo.strRfidInfo = rfidInfo.toStdString(); - this->queueTmpTrainInfo_.push(trainInfo); - - if (this->iDirection < 0) - { - this->queueTmpTrainInfo_.clear(); - return; - } - if (this->iDirection > 0) - { - while (!this->queueTmpTrainInfo_.isEmpty()) - { - this->queueTrainInfo_.push(this->queueTmpTrainInfo_.pop()); - } - } + this->queueTrainInfo_.push(trainInfo); } void MainWindow::IdentifyTypeUpdate() { @@ -810,7 +721,7 @@ void MainWindow::ErrorSlots(const QString &info) { void MainWindow::readTestInfo() { - if (this->isOpenCom || ui->openComButton->text() == "关闭串口") + if (this->isOpenCom) { logWarn("请先关闭串口后进行测试操作!"); return; diff --git a/src/qt_source/mainwindow.h b/src/qt_source/mainwindow.h index 73c6ac4..c0ab5ed 100644 --- a/src/qt_source/mainwindow.h +++ b/src/qt_source/mainwindow.h @@ -78,7 +78,6 @@ public: void deleteOldFiles(const QString &path, int days); bool rnameRfidLog(); void saveRfidLog(const QString &value); - bool connectCom(); void initParam(); @@ -99,7 +98,6 @@ private: UpResultThread *upResultThread; QString trainTime; - bool auto_reconnect_serial_ = false; QString logRfidRecvName; QFile recvLog; @@ -124,7 +122,6 @@ private: QStringList vecTrain; MQueue queue_{}; // RFID读取的数据队列 MQueue queueTrainInfo_{}; // RFID读取的磁钢信息队列 - MQueue queueTmpTrainInfo_{}; // RFID临时信息队列 QString rfidSourceInfo; QString tmpRfid; //临时存储的RFID原始数据,用于防止接收的数据不完整 diff --git a/src/serial/ComDetect.cpp b/src/serial/ComDetect.cpp index 2389211..51bf9a3 100644 --- a/src/serial/ComDetect.cpp +++ b/src/serial/ComDetect.cpp @@ -19,7 +19,6 @@ QSerialPort* ComDetect::openCom(const QString &com, int baud) { try { - this->serial_ = new QSerialPort; this->serial_->setPortName(com); if (baud == 9600) @@ -54,7 +53,7 @@ QSerialPort* ComDetect::openCom(const QString &com, int baud) } catch (const std::exception &e) { - LogError << e.what(); + std::cout << e.what() << std::endl; return nullptr; } diff --git a/src/serial/ComDetect.h b/src/serial/ComDetect.h index 0103b4a..253b598 100644 --- a/src/serial/ComDetect.h +++ b/src/serial/ComDetect.h @@ -10,7 +10,6 @@ #include #include #include "MQueue.h" -#include "common.h" class ComDetect { public: diff --git a/src/threads/UpResultThread.cpp b/src/threads/UpResultThread.cpp index 7c88f72..3763d9d 100644 --- a/src/threads/UpResultThread.cpp +++ b/src/threads/UpResultThread.cpp @@ -27,7 +27,7 @@ void UpResultThread::run() { while (true) { - QThread::sleep(1); +// if ((*this->iDirection == 0 && this->queueTrainInfo_->size() <= 4) || *this->iDirection < 0) continue; while (!this->queueTrainInfo_->isEmpty()) { @@ -37,7 +37,7 @@ void UpResultThread::run() //this->logError("第" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败!"); } } - + QThread::sleep(1); } } @@ -73,7 +73,7 @@ bool UpResultThread::upWeb(TrainInfo &trainInfo) httplib::Client cli(this->interfaceConfig_.httpIp.toStdString(), this->interfaceConfig_.httpPort); cli.set_connection_timeout(3, 0); - cli.set_read_timeout(2, 0); + cli.set_read_timeout(3, 0); httplib::Headers header; httplib::Params params; header.emplace("blade-auth", this->webToken);