How to use pymodbus the first time

594 views
Skip to first unread message

DS

unread,
Sep 16, 2021, 10:22:58 AM9/16/21
to pymodbus
Hi all, it is the first time I use pymodbus and modbus protocol in general. Until now I can connect with my slave but I don't understand how to read and write coil or registers.

The machine owners send me a table with many register number and bit dimension
as for example:

example.PNG



But I don't understand how to read and write from and on slave with pymodbus library. Can someone help me?
many thanks in advance
best regards

Mikael Andersson

unread,
Sep 16, 2021, 11:13:31 AM9/16/21
to pymo...@googlegroups.com
Hi,

From your file, it is not clear if it is holding or input registers.
There are different functions for those. Neither is it clear if the 16
bits integers are signed or unsigned.

Here you can find functions for reading and writing coils and registers:

https://pymodbus.readthedocs.io/en/latest/source/library/pymodbus.client.html

The register numbers in the left most column in your file are the start
addresses for the registers.

In order to read e.g. an input register with 'register number modbus'
equal to 4 (the first row in your file), you would simply do the
following to decode an unsigned 16 bits integer (server id=1):

result = modbus_client.read_input_registers(4, count=1, unit=1)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers())
value = decoder.decode_16bit_uint()

You may have to add arguments to the fromRegisters function above
depending word and byte order.

Have a look at this example for reading/writing from/to registers/coils:

https://github.com/riptideio/pymodbus/blob/master/examples/common/synchronous_client.py

and here for encoding/building and decoding different data types:

https://github.com/riptideio/pymodbus/blob/master/examples/common/modbus_payload.py

Someone more experienced are welcome to correct me if I am wrong about
something. :)

--
Kind regards,

Mikael Andersson

Den 2021-09-16 kl. 16:22, skrev DS:
> Hi all, it is the first time I use pymodbus and modbus protocol in
> general. Until now I can connect with my slave but I don't understand
> how to read and write coil or registers.
>
> The machine owners send me a table with many register number and bit
> dimension
> as for example:
>
> example.PNG
>
>
>
> But I don't understand how to read and write from and on slave with
> pymodbus library. Can someone help me?
> many thanks in advance
> best regards
>
> --
> You received this message because you are subscribed to the Google
> Groups "pymodbus" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pymodbus+u...@googlegroups.com
> <mailto:pymodbus+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pymodbus/ff81ec4d-5de9-453f-ba87-e884b28af3d8n%40googlegroups.com
> <https://groups.google.com/d/msgid/pymodbus/ff81ec4d-5de9-453f-ba87-e884b28af3d8n%40googlegroups.com?utm_medium=email&utm_source=footer>.

DS

unread,
Sep 17, 2021, 6:18:02 AM9/17/21
to pymodbus
thank you really much Mikael.

For the address 4 I obtain for example "result.registers=[520]" and the temperature read on slave is 52.0 °C. while decoding that number I obtain different values for each decoding type (uint, float ecc). How can I understand if decoding is need or how to understand that temperature is 52.0 from 520? Trying with another address relative to version firmware I obtain [805] where the firmware version read on slave is 8.05. 
For address 3000 instead (status of DIO) I obtain [2823] and I cannot understand in slave where read the correct value.
The same for many other parameter. In other case for paramenters where I expect true or false as for status of DIO parameters I try using read_coils but exception appears.

I would add another question about words as defined in the picture in the attachment. How can read content of that words?

example2.PNG

Thank you really much and I'm sorry for disturb
Best Regards

DS

unread,
Sep 17, 2021, 6:25:23 AM9/17/21
to pymodbus
for more details if I try:

result = client.read_holding_registers(10106, count=2, unit=1)
I obtain [17024, 0]
and in the documentation reading Status mask paper I have:
example3.PNG

where I wrong in order to translate the message content?

thank you very much again.
Best Regards

Mikael Andersson

unread,
Sep 18, 2021, 6:20:51 AM9/18/21
to pymodbus
Regarding your queustion: "How can I understand if decoding is need or how to understand that temperature is 52.0 from 520?"

You need to get the proper information from the manufacturer of the machine. They should know the data format.

Can you not contact them and get help?

Denis Sangaletti

unread,
Sep 18, 2021, 7:27:07 AM9/18/21
to pymo...@googlegroups.com
Thank you very much. Just last question and then I will try on my self. Do you know how to read words? I see that some registers have msb and lsb word. But I don't understand how to read them. 
Thank you 

You received this message because you are subscribed to a topic in the Google Groups "pymodbus" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pymodbus/h4H_agEZGCk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pymodbus+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pymodbus/1ffcab6b-bd27-43fa-85d3-202ef948f843n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages