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