From 5a6df73f88f73d2e0fec89f3cb5502ee954dac73 Mon Sep 17 00:00:00 2001 From: suyu <1643689728@qq.com> Date: Fri, 14 Aug 2026 11:09:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B8=85=E7=90=86PLC=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E4=B8=B2=E5=8F=A3=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plc_communication_service.cpp | 44 +++++++++++++------ .../plc_communication_service.h | 1 + app/src/ui/main_window.cpp | 11 ++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/src/infrastructure/plc_communication_service.cpp b/app/src/infrastructure/plc_communication_service.cpp index 8cd36ec..dd300f1 100644 --- a/app/src/infrastructure/plc_communication_service.cpp +++ b/app/src/infrastructure/plc_communication_service.cpp @@ -101,6 +101,11 @@ PlcCommunicationResult PlcCommunicationService::connectDevice( { return {false, "必须填写串口端口,并将 PLC 站号设置为 1~247"}; } + if (state_ == PlcConnectionState::Disconnected + && master_->state() != QModbusDevice::UnconnectedState) + { + closeSerialSession(); + } if (master_->state() != QModbusDevice::UnconnectedState) { return {false, "PLC 连接已经启动,请先断开当前连接"}; @@ -134,9 +139,12 @@ PlcCommunicationResult PlcCommunicationService::connectDevice( setState(PlcConnectionState::Connecting); if (!master_->connectDevice()) { - const QModbusDevice::Error error = master_->error() == QModbusDevice::NoError - ? QModbusDevice::ConnectionError : master_->error(); - handleModbusError(error); + if (last_error_.empty()) + { + const QModbusDevice::Error error = master_->error() == QModbusDevice::NoError + ? QModbusDevice::ConnectionError : master_->error(); + handleModbusError(error); + } return {false, last_error_}; } return {true, {}}; @@ -144,21 +152,12 @@ PlcCommunicationResult PlcCommunicationService::connectDevice( void PlcCommunicationService::disconnectDevice() { - ++connection_generation_; - disconnecting_ = true; - poll_timer_.stop(); - master_->disconnectDevice(); - pending_reply_ = nullptr; - poll_update_pending_ = false; - pending_poll_addresses_.clear(); - serial_session_opened_ = false; - received_valid_response_ = false; + closeSerialSession(); repository_.invalidate(); updateInitialReadCompleted(false, true); last_error_type_ = PlcCommunicationError::None; last_error_.clear(); setState(PlcConnectionState::Disconnected); - disconnecting_ = false; } void PlcCommunicationService::setPollAddresses( @@ -477,6 +476,23 @@ void PlcCommunicationService::handleModbusError(QModbusDevice::Error error) setError(classifyPlcCommunicationError(error, context)); } +void PlcCommunicationService::closeSerialSession() +{ + ++connection_generation_; + disconnecting_ = true; + poll_timer_.stop(); + pending_reply_ = nullptr; + poll_update_pending_ = false; + pending_poll_addresses_.clear(); + if (master_->state() != QModbusDevice::UnconnectedState) + { + master_->disconnectDevice(); + } + serial_session_opened_ = false; + received_valid_response_ = false; + disconnecting_ = false; +} + void PlcCommunicationService::setState(PlcConnectionState state) { if (state_ == state) @@ -502,7 +518,7 @@ void PlcCommunicationService::setError(const PlcCommunicationFailure &failure) || failure.type == PlcCommunicationError::UsbSerialAdapterRemoved; if (disconnected) { - serial_session_opened_ = false; + closeSerialSession(); } setState(disconnected ? PlcConnectionState::Disconnected : PlcConnectionState::Faulted); diff --git a/app/src/infrastructure/plc_communication_service.h b/app/src/infrastructure/plc_communication_service.h index 6b11585..7432cac 100644 --- a/app/src/infrastructure/plc_communication_service.h +++ b/app/src/infrastructure/plc_communication_service.h @@ -67,6 +67,7 @@ private: void updateInitialReadCompleted(bool completed, bool force_notification = false); void handleUnexpectedDisconnect(); void handleModbusError(QModbusDevice::Error error); + void closeSerialSession(); void setState(PlcConnectionState state); void setError(const PlcCommunicationFailure &failure); diff --git a/app/src/ui/main_window.cpp b/app/src/ui/main_window.cpp index a94fde7..1ea9fdc 100644 --- a/app/src/ui/main_window.cpp +++ b/app/src/ui/main_window.cpp @@ -634,7 +634,16 @@ void MainWindow::connectPlc() plc_configuration_); if (!result.succeeded) { - showProjectResult(tr("连接 PLC"), fromUtf8(result.message), false); + const QString message = fromUtf8(result.message); + if (result.message == runtime_mode_service_.plcError()) + { + statusBar()->showMessage(message, 5000); + QMessageBox::warning(this, tr("连接 PLC"), message); + } + else + { + showProjectResult(tr("连接 PLC"), message, false); + } return; } statusBar()->showMessage(tr("PLC 正在连接,等待首次 M/D 读取"), 5000);