Why is there an address offset of +1 between ModbusDeviceContext and BaseModbusDataBlock? #2840
-
|
Hi there, Regarding these 2 lines in ModbusDeviceContext getValues and setValues: pymodbus/pymodbus/datastore/context.py Line 121 in 97c3bbf pymodbus/pymodbus/datastore/context.py Line 132 in 97c3bbf --> There is an offset of +1 between ModbusDeviceContext and BaseModbusDataBlock. In my Modbus Server application, I have to fetch the registers' value on every request (if the last actualisation is too old), so I made my own class based on ModbusSparseDataBlock: class MyModbusSparseDataBlock(ModbusSparseDataBlock):
(...)
def getValues(self, address, count=1) -> list[int] | list[bool] | ExcCodes:
'''Function called when the server reads values from this data block.
We override it to fetch the latest command values before returning them.
'''
self.log.debug(f"{self.name}: DataBlock: Getting values from address {address} count {count}")
values = super().getValues(address, count)
if isinstance(values, ExcCodes):
self.log.error(f"{self.name}: DataBlock: ExcCodes")
return values
(...) # fetching valuesIn my example We see the incoming request asks for register address 2 but ModbusSparseDataBlock.getValues is called with address 3 I think that the two lines Am I wrong somewhere in my implementation? As I could find out these 2 lines come from #2354 for the release 3.8.0. I can make a PR if you agree with me, but I don't know if there is a pytest test case and don't know pytest, so I won't be able to fix the test case in the same PR. But if I do the PR, I will let it editable so any test case can be fixed later. edit: in the same Modbus specification document, the data model is described in the chapter 4.3 as follows:
So the values of *DataBlock should start at 0. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
There is a difference between how you define registers and how you address/request registers because it is very old code. Change to use the simulator datablock if you do not want the +1 difference. |
Beta Was this translation helpful? Give feedback.
-
|
Are the other datablocks (not simulator datablocks) meant to disappear in a future release? |
Beta Was this translation helpful? Give feedback.
They will exist in all v3.x versions, but are expected to be removed in v4.0.
There is a new datastore in the works, a simplified version of the current simulator datastore which will be released in v3.x and in v4.x will be the only datastore. The current server and simulator will grow together in v4.x.
Once v4.0 is released the last v3.x will be in active maintenance for at least a year.