* @brief Handles completed Modbus read operations and publishes the result as RTU-format bytes. */
| 1295 | * @brief Handles completed Modbus read operations and publishes the result as RTU-format bytes. |
| 1296 | */ |
| 1297 | void IO::Drivers::Modbus::onReadReady() |
| 1298 | { |
| 1299 | auto* reply = qobject_cast<QModbusReply*>(sender()); |
| 1300 | if (!reply) { |
| 1301 | m_lastReply = nullptr; |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | if (reply != m_lastReply) { |
| 1306 | reply->deleteLater(); |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | m_lastReply = nullptr; |
| 1311 | |
| 1312 | if (reply->error() != QModbusDevice::NoError) { |
| 1313 | reply->deleteLater(); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | const QModbusDataUnit unit = reply->result(); |
| 1318 | if (!unit.isValid() || unit.valueCount() == 0) { |
| 1319 | reply->deleteLater(); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | try { |
| 1324 | publishReceivedData(buildRtuFrame(unit)); |
| 1325 | } catch (const std::exception& e) { |
| 1326 | qWarning() << "Modbus frame build failed:" << e.what(); |
| 1327 | } catch (...) { |
| 1328 | qWarning() << "Modbus frame build failed: unknown exception"; |
| 1329 | } |
| 1330 | |
| 1331 | reply->deleteLater(); |
| 1332 | |
| 1333 | ++m_currentGroupIndex; |
| 1334 | if (m_currentGroupIndex < m_registerGroups.count()) |
| 1335 | pollNextGroup(); |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * @brief Handles Modbus device state changes by starting/stopping the poll timer. |