更新自动重连串口功能,若人为点击“打开串口”,则只要串口连接失败,就会在10秒后重新尝试连接;直到连接成功或人为点击“关闭串口”

This commit is contained in:
Mr.V 2024-04-15 16:28:56 +08:00
parent 6e6113c48a
commit 920f3fa341
8 changed files with 152 additions and 46 deletions

View File

@ -73,6 +73,7 @@ bool ConfigUtil::readSocketServerConfig(const QString &configFile, QString &erro
mset->beginGroup("socket_server"); mset->beginGroup("socket_server");
config.server_ip = mset->value("server_ip", "").toString(); config.server_ip = mset->value("server_ip", "").toString();
config.server_port = mset->value("server_port", "").toInt(); config.server_port = mset->value("server_port", "").toInt();
config.delayed_upload = mset->value("delayed_upload", "").toInt();
mset->endGroup(); mset->endGroup();
} catch (const std::exception &e) { } catch (const std::exception &e) {

View File

@ -46,6 +46,8 @@ namespace ai_matrix {
QString server_ip; QString server_ip;
// server_port // server_port
int server_port; int server_port;
// 识别结果延后X节上传以等待Socket反馈火车运行方向
int delayed_upload;
}; };
}; };

View File

@ -29,6 +29,15 @@ namespace ai_matrix {
std::string strRfidInfo; std::string strRfidInfo;
}; };
struct ComeTrain {
QString trainTime;
bool needUpLoad;
ComeTrain () {
this->trainTime = "";
this->needUpLoad = false;
};
};
} }

View File

@ -142,7 +142,7 @@ MainWindow::MainWindow(QWidget *parent)
} }
}); });
connect(this->tcpClient, &TcpClient::getDirectionSignals, this, [=](int direction) { connect(this->tcpClient, &TcpClient::getDirectionSignals, this, [=](int direction) {
if (this->iDirection != direction) if (this->iDirection == 0)
{ {
this->logInfo("获得方向判定:" + QString(direction > -1 ? (direction > 0 ? "需识别" : "待定") : "无需上传")); this->logInfo("获得方向判定:" + QString(direction > -1 ? (direction > 0 ? "需识别" : "待定") : "无需上传"));
this->iDirection = direction; this->iDirection = direction;
@ -451,16 +451,11 @@ void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reaso
} }
/********************************串口操作*************************************/ /********************************串口操作*************************************/
/** bool MainWindow::connectCom()
*
*/
void MainWindow::openComClicked()
{ {
if (ui->openComButton->text() == "打开串口") this->serial_ = this->comDetect_.openCom(ui->comBox->currentText(), ui->BTBox->currentText().toInt());
{
this->serial_ = this->comDetect_.openCom(ui->comBox->currentText(), ui->BTBox->currentIndex()); if (this->serial_ != nullptr) {
if (this->serial_ != nullptr)
{
QString logs; QString logs;
logs.append("串口打开成功\n"); logs.append("串口打开成功\n");
logs.append("串口号:" + ui->comBox->currentText() + "\n"); logs.append("串口号:" + ui->comBox->currentText() + "\n");
@ -469,22 +464,85 @@ void MainWindow::openComClicked()
logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder); logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder);
this->isOpenCom = true; this->isOpenCom = true;
this->logInfo(logs); this->logInfo(logs);
this->ui->openComButton->setText("关闭串口");
this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ "
"background-color: rgba(74, 221, 108, 225);\ " "background-color: rgba(74, 221, 108, 225);\ "
"border:1px solid rgba(168, 168, 168, 105);"); "border:1px solid rgba(168, 168, 168, 105);");
connect(this->serial_,&QSerialPort::readyRead,this,&MainWindow::readCom); // connect(this->serial_, &QSerialPort::readyRead, this, &MainWindow::readCom); //
connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error); connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error);
return true;
} }
else else
{ {
this->isOpenCom = false; return false;
this->logInfo("串口打开失败!");
} }
}
/**
*
*/
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 else
{
this->auto_reconnect_serial_ = false;
if (this->isOpenCom)
{ {
// 弹出对话框要求用户输入密码 // 弹出对话框要求用户输入密码
QString password = QInputDialog::getText(this, "密码验证", "请输入密码:", QLineEdit::Password); QString password = QInputDialog::getText(this, "密码验证", "请输入密码:", QLineEdit::Password);
@ -506,6 +564,12 @@ void MainWindow::openComClicked()
QMessageBox::warning(this, "验证失败", "密码不正确!"); QMessageBox::warning(this, "验证失败", "密码不正确!");
} }
} }
else
{
this->logInfo("串口已停止访问!");
this->ui->openComButton->setText("打开串口");
}
}
} }
/** /**
@ -523,6 +587,12 @@ void MainWindow::serialPort_error(QSerialPort::SerialPortError error)
this->ui->LEDlabel->setStyleSheet("border-radius: 16px; \ " this->ui->LEDlabel->setStyleSheet("border-radius: 16px; \ "
"background-color: red;\ " "background-color: red;\ "
"border:1px solid rgba(168, 168, 168, 105);"); "border:1px solid rgba(168, 168, 168, 105);");
QtConcurrent::run([=](){
QThread::sleep(10);
if (this->auto_reconnect_serial_ && !this->isOpenCom) {
this->connectCom();
}
});
} }
} }
@ -666,6 +736,12 @@ void MainWindow::upRfid(QString rfidInfo)
QString info = this->vecTrain.back(); QString info = this->vecTrain.back();
int order = this->vecTrain.size(); 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); info = info.mid(1,info.size() - 1);
TrainInfo trainInfo; TrainInfo trainInfo;
@ -676,7 +752,20 @@ void MainWindow::upRfid(QString rfidInfo)
trainInfo.collectTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString(); trainInfo.collectTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString();
trainInfo.strRfidInfo = rfidInfo.toStdString(); trainInfo.strRfidInfo = rfidInfo.toStdString();
this->queueTrainInfo_.push(trainInfo); 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());
}
}
} }
void MainWindow::IdentifyTypeUpdate() { void MainWindow::IdentifyTypeUpdate() {
@ -721,7 +810,7 @@ void MainWindow::ErrorSlots(const QString &info) {
void MainWindow::readTestInfo() void MainWindow::readTestInfo()
{ {
if (this->isOpenCom) if (this->isOpenCom || ui->openComButton->text() == "关闭串口")
{ {
logWarn("请先关闭串口后进行测试操作!"); logWarn("请先关闭串口后进行测试操作!");
return; return;

View File

@ -78,6 +78,7 @@ public:
void deleteOldFiles(const QString &path, int days); void deleteOldFiles(const QString &path, int days);
bool rnameRfidLog(); bool rnameRfidLog();
void saveRfidLog(const QString &value); void saveRfidLog(const QString &value);
bool connectCom();
void initParam(); void initParam();
@ -98,6 +99,7 @@ private:
UpResultThread *upResultThread; UpResultThread *upResultThread;
QString trainTime; QString trainTime;
bool auto_reconnect_serial_ = false;
QString logRfidRecvName; QString logRfidRecvName;
QFile recvLog; QFile recvLog;
@ -122,6 +124,7 @@ private:
QStringList vecTrain; QStringList vecTrain;
MQueue<QString> queue_{}; // RFID读取的数据队列 MQueue<QString> queue_{}; // RFID读取的数据队列
MQueue<TrainInfo> queueTrainInfo_{}; // RFID读取的磁钢信息队列 MQueue<TrainInfo> queueTrainInfo_{}; // RFID读取的磁钢信息队列
MQueue<TrainInfo> queueTmpTrainInfo_{}; // RFID临时信息队列
QString rfidSourceInfo; QString rfidSourceInfo;
QString tmpRfid; //临时存储的RFID原始数据用于防止接收的数据不完整 QString tmpRfid; //临时存储的RFID原始数据用于防止接收的数据不完整

View File

@ -19,6 +19,7 @@ QSerialPort* ComDetect::openCom(const QString &com, int baud)
{ {
try try
{ {
this->serial_ = new QSerialPort;
this->serial_->setPortName(com); this->serial_->setPortName(com);
if (baud == 9600) if (baud == 9600)
@ -53,7 +54,7 @@ QSerialPort* ComDetect::openCom(const QString &com, int baud)
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
std::cout << e.what() << std::endl; LogError << e.what();
return nullptr; return nullptr;
} }

View File

@ -10,6 +10,7 @@
#include <QtSerialPort/QSerialPortInfo> #include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include "MQueue.h" #include "MQueue.h"
#include "common.h"
class ComDetect { class ComDetect {
public: public:

View File

@ -27,7 +27,7 @@ void UpResultThread::run()
{ {
while (true) while (true)
{ {
// if ((*this->iDirection == 0 && this->queueTrainInfo_->size() <= 4) || *this->iDirection < 0) continue; QThread::sleep(1);
while (!this->queueTrainInfo_->isEmpty()) while (!this->queueTrainInfo_->isEmpty())
{ {
@ -37,7 +37,7 @@ void UpResultThread::run()
//this->logError("第" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败!"); //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); httplib::Client cli(this->interfaceConfig_.httpIp.toStdString(), this->interfaceConfig_.httpPort);
cli.set_connection_timeout(3, 0); cli.set_connection_timeout(3, 0);
cli.set_read_timeout(3, 0); cli.set_read_timeout(2, 0);
httplib::Headers header; httplib::Headers header;
httplib::Params params; httplib::Params params;
header.emplace("blade-auth", this->webToken); header.emplace("blade-auth", this->webToken);