Explorar el Código

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

main
suyu hace 1 mes
padre
commit
5a6df73f88
Se han modificado 3 ficheros con 41 adiciones y 15 borrados
  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 Ver fichero

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


+ 1
- 0
app/src/infrastructure/plc_communication_service.h Ver fichero

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



+ 10
- 1
app/src/ui/main_window.cpp Ver fichero

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


Cargando…
Cancelar
Guardar