ソースを参照

fix: 清理PLC断开后的串口会话

main
suyu 1ヶ月前
コミット
5a6df73f88
3個のファイルの変更41行の追加15行の削除
  1. +30
    -14
      app/src/infrastructure/plc_communication_service.cpp
  2. +1
    -0
      app/src/infrastructure/plc_communication_service.h
  3. +10
    -1
      app/src/ui/main_window.cpp

+ 30
- 14
app/src/infrastructure/plc_communication_service.cpp ファイルの表示

@@ -101,6 +101,11 @@ PlcCommunicationResult PlcCommunicationService::connectDevice(
{ {
return {false, "必须填写串口端口,并将 PLC 站号设置为 1~247"}; return {false, "必须填写串口端口,并将 PLC 站号设置为 1~247"};
} }
if (state_ == PlcConnectionState::Disconnected
&& master_->state() != QModbusDevice::UnconnectedState)
{
closeSerialSession();
}
if (master_->state() != QModbusDevice::UnconnectedState) if (master_->state() != QModbusDevice::UnconnectedState)
{ {
return {false, "PLC 连接已经启动,请先断开当前连接"}; return {false, "PLC 连接已经启动,请先断开当前连接"};
@@ -134,9 +139,12 @@ PlcCommunicationResult PlcCommunicationService::connectDevice(
setState(PlcConnectionState::Connecting); setState(PlcConnectionState::Connecting);
if (!master_->connectDevice()) 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 {false, last_error_};
} }
return {true, {}}; return {true, {}};
@@ -144,21 +152,12 @@ PlcCommunicationResult PlcCommunicationService::connectDevice(


void PlcCommunicationService::disconnectDevice() 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(); repository_.invalidate();
updateInitialReadCompleted(false, true); updateInitialReadCompleted(false, true);
last_error_type_ = PlcCommunicationError::None; last_error_type_ = PlcCommunicationError::None;
last_error_.clear(); last_error_.clear();
setState(PlcConnectionState::Disconnected); setState(PlcConnectionState::Disconnected);
disconnecting_ = false;
} }


void PlcCommunicationService::setPollAddresses( void PlcCommunicationService::setPollAddresses(
@@ -477,6 +476,23 @@ void PlcCommunicationService::handleModbusError(QModbusDevice::Error error)
setError(classifyPlcCommunicationError(error, context)); 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) void PlcCommunicationService::setState(PlcConnectionState state)
{ {
if (state_ == state) if (state_ == state)
@@ -502,7 +518,7 @@ void PlcCommunicationService::setError(const PlcCommunicationFailure &failure)
|| failure.type == PlcCommunicationError::UsbSerialAdapterRemoved; || failure.type == PlcCommunicationError::UsbSerialAdapterRemoved;
if (disconnected) if (disconnected)
{ {
serial_session_opened_ = false;
closeSerialSession();
} }
setState(disconnected setState(disconnected
? PlcConnectionState::Disconnected : PlcConnectionState::Faulted); ? PlcConnectionState::Disconnected : PlcConnectionState::Faulted);


+ 1
- 0
app/src/infrastructure/plc_communication_service.h ファイルの表示

@@ -67,6 +67,7 @@ private:
void updateInitialReadCompleted(bool completed, bool force_notification = false); void updateInitialReadCompleted(bool completed, bool force_notification = false);
void handleUnexpectedDisconnect(); void handleUnexpectedDisconnect();
void handleModbusError(QModbusDevice::Error error); void handleModbusError(QModbusDevice::Error error);
void closeSerialSession();
void setState(PlcConnectionState state); void setState(PlcConnectionState state);
void setError(const PlcCommunicationFailure &failure); void setError(const PlcCommunicationFailure &failure);




+ 10
- 1
app/src/ui/main_window.cpp ファイルの表示

@@ -634,7 +634,16 @@ void MainWindow::connectPlc()
plc_configuration_); plc_configuration_);
if (!result.succeeded) 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; return;
} }
statusBar()->showMessage(tr("PLC 正在连接,等待首次 M/D 读取"), 5000); statusBar()->showMessage(tr("PLC 正在连接,等待首次 M/D 读取"), 5000);


読み込み中…
キャンセル
保存