revert 更新自动重连串口功能,若人为点击“打开串口”,则只要串口连接失败,就会在10秒后重新尝试连接;直到连接成功或人为点击“关闭串口”
This commit is contained in:
zhangwei 2024-04-18 07:03:30 +00:00
parent 920f3fa341
commit 23c75b57de
8 changed files with 48 additions and 154 deletions

View File

@ -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) {

View File

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

View File

@ -29,15 +29,6 @@ namespace ai_matrix {
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) {
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;

View File

@ -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<QString> queue_{}; // RFID读取的数据队列
MQueue<TrainInfo> queueTrainInfo_{}; // RFID读取的磁钢信息队列
MQueue<TrainInfo> queueTmpTrainInfo_{}; // RFID临时信息队列
QString rfidSourceInfo;
QString tmpRfid; //临时存储的RFID原始数据用于防止接收的数据不完整

View File

@ -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;
}

View File

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

View File

@ -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);