|
4 | 4 | #include "communication/modbuspoll.h" |
5 | 5 | #include "communicationhelpers.h" |
6 | 6 | #include "testslavemodbus.h" |
| 7 | +#include "testslavemodbusmulti.h" |
7 | 8 | #include "util/modbusdatatype.h" |
8 | 9 |
|
9 | 10 | #include <QSignalSpy> |
@@ -340,6 +341,53 @@ void TestModbusPoll::multiSlaveSuccess_3() |
340 | 341 | CommunicationHelpers::verifyReceivedDataSignal(arguments, expResults); |
341 | 342 | } |
342 | 343 |
|
| 344 | +void TestModbusPoll::multiSlaveSameConnectionSameAddress() |
| 345 | +{ |
| 346 | + /* Reproduce the reported bug: two slaves with different IDs share the same |
| 347 | + * connection (same port) and both read the same register address. |
| 348 | + * Before the operator< fix, both curves showed the value from the last-polled |
| 349 | + * slave because the result map treated (addr=40001, slaveId=1) and |
| 350 | + * (addr=40001, slaveId=4) as the same key. */ |
| 351 | + |
| 352 | + const quint8 newSlaveId = 4; |
| 353 | + const deviceId_t newDevId = Device::cFirstDeviceId + 3; |
| 354 | + |
| 355 | + /* Replace the single-slave server at port 5020 with a multi-slave server |
| 356 | + * that responds to slave ID 1 (device 1) and slave ID 4 (new device) */ |
| 357 | + _testSlaveMap[Device::cFirstDeviceId]->disconnect(); |
| 358 | + |
| 359 | + TestSlaveMultiModbus multiSlave; |
| 360 | + QVERIFY(multiSlave.listenOnPort(5020)); |
| 361 | + |
| 362 | + multiSlave.testDevice(Device::cFirstDeviceId)->configureHoldingRegister(0, true, 1111); |
| 363 | + multiSlave.testDevice(newSlaveId)->configureHoldingRegister(0, true, 4444); |
| 364 | + |
| 365 | + /* Add a 4th device that shares connection ID_1 (port 5020) with device 1 */ |
| 366 | + _pSettingsModel->addDevice(newDevId); |
| 367 | + _pSettingsModel->deviceSettings(newDevId)->setConnectionId(ConnectionTypes::ID_1); |
| 368 | + _pSettingsModel->deviceSettings(newDevId)->setSlaveId(newSlaveId); |
| 369 | + |
| 370 | + ModbusPoll modbusPoll(_pSettingsModel); |
| 371 | + QSignalSpy spyDataReady(&modbusPoll, &ModbusPoll::registerDataReady); |
| 372 | + |
| 373 | + auto modbusRegisters = QList<ModbusRegister>() |
| 374 | + << ModbusRegister(ModbusAddress(40001), Device::cFirstDeviceId, Type::UNSIGNED_16) |
| 375 | + << ModbusRegister(ModbusAddress(40001), newDevId, Type::UNSIGNED_16); |
| 376 | + |
| 377 | + modbusPoll.startCommunication(modbusRegisters); |
| 378 | + |
| 379 | + QVERIFY(spyDataReady.wait(500)); |
| 380 | + QCOMPARE(spyDataReady.count(), 1); |
| 381 | + |
| 382 | + QList<QVariant> arguments = spyDataReady.takeFirst(); |
| 383 | + auto expResults = ResultDoubleList() << ResultDouble(1111, State::SUCCESS) |
| 384 | + << ResultDouble(4444, State::SUCCESS); |
| 385 | + |
| 386 | + CommunicationHelpers::verifyReceivedDataSignal(arguments, expResults); |
| 387 | + |
| 388 | + multiSlave.stopListening(); |
| 389 | +} |
| 390 | + |
343 | 391 | void TestModbusPoll::multiSlaveSingleFail() |
344 | 392 | { |
345 | 393 | _testSlaveMap[Device::cFirstDeviceId]->disconnect(); |
|
0 commit comments