Modbus TCP Server
Finally, we can look at Modbus TCP Server. To make it easy, I've based my code on the example from pyModbus. In this programme, server updates their registers you will find here:
The function that is responsible for udpating the Modbus memory - context - is called "updating_writer". In this line, the current values are retrieved:
values = context[slave_id].getValues(register, address, count=5)
Below, the context is being changed:
values = [v + 1 for v in values]
And finally, Modbus memory is updated:
context[slave_id].setValues(register, address, values)
DHT11 - Temperature & Humidity sensor
In my example, Raspberry Pi works as a Modbus TCP Gateway that transfers data about temprature and humidity. I found two Python libraries that enable to read data from this sensor:
However, I recommend to use the second one, because the first one is not working. First of all - there is small mistake in for loop (some bits are omitted), secondly - Raspbian is not Real Time OS, so counting time on the level of μ seconds without any prioritization of threads makes that reading values from DHT11 is highly error-prone. Lucikly, the second library works perfectly (inside the library you will find the code responsbile for assign high prority fot this task).
The program with compiled Python library, you will find here.