@@ -42,6 +42,37 @@ QModbusDataUnitMap::Iterator ModbusDataUnitMap::end()
4242 return _modbusDataUnitMap.end ();
4343}
4444
45+ // /
46+ // / \brief getDataValue
47+ // / \param modbusMap
48+ // / \param pointType
49+ // / \param pointAddress
50+ // / \return
51+ // /
52+ quint16 getDataValue (const QModbusDataUnitMap& modbusMap, QModbusDataUnit::RegisterType pointType, quint16 pointAddress)
53+ {
54+ const auto length = modbusMap[pointType].valueCount ();
55+ const auto startAddress = modbusMap[pointType].startAddress ();
56+ if (pointAddress < startAddress || pointAddress > startAddress + length)
57+ return 0 ;
58+ else
59+ return modbusMap[pointType].value (pointAddress - startAddress);
60+ }
61+
62+ // /
63+ // / \brief setDataValue
64+ // / \param modbusMap
65+ // / \param pointType
66+ // / \param pointAddress
67+ // / \param value
68+ // /
69+ void setDataValue (QModbusDataUnitMap& modbusMap, QModbusDataUnit::RegisterType pointType, quint16 pointAddress, quint16 value)
70+ {
71+ const auto startAddress = modbusMap[pointType].startAddress ();
72+ const auto idx = pointAddress - startAddress;
73+ if (idx >= 0 ) modbusMap[pointType].setValue (idx, value);
74+ }
75+
4576// /
4677// / \brief ModbusDataUnitMap::setData
4778// / \param data
@@ -52,11 +83,8 @@ void ModbusDataUnitMap::setData(const QModbusDataUnit& data)
5283 const auto length = data.valueCount ();
5384 const auto type = data.registerType ();
5485
55- const auto idx = addr - _modbusDataUnitMap[type].startAddress ();
5686 for (int i = 0 ; i < length; i++)
57- {
58- _modbusDataUnitMap[type].setValue (idx + i, data.value (i));
59- }
87+ setDataValue (_modbusDataUnitMap, type, addr + i, data.value (i));
6088}
6189
6290// /
@@ -85,10 +113,10 @@ void ModbusDataUnitMap::updateDataUnitMap()
85113 if (length > 0 )
86114 modbusMap.insert (type, {type, startAddress, length});
87115
88- const auto idx = qAbs (_modbusDataUnitMap[type].startAddress () - startAddress);
89116 for (int i = 0 ; i < length; i++)
90117 {
91- modbusMap[type].setValue (i, _modbusDataUnitMap[type].value (i));
118+ const auto value = getDataValue (_modbusDataUnitMap, type, startAddress + i);
119+ setDataValue (modbusMap, type, startAddress + i, value);
92120 }
93121 }
94122 _modbusDataUnitMap = modbusMap;
0 commit comments