I am exploring the option to communicate with a device using the pyModbusTCP module and python. The vendor has sent me the Communication Protocol layout, but I don't understand how to relate the information from the manual and the input that pyModbusTCP wants.
I do not have the device yet and I am also new to Modbus, having read through Wikipedia and some other articles mostly.
Here is an excerpt from the manual (apologies, I do not know how to insert tables):
+------------------------+---------------+--------------------------+
| Modbus address | Bit | Description |
+------------------------+---------------+--------------------------+
| 2 | 0 | Binary, some function |
| | 1 | Binary, some function |
| | 2 | Binary, some function |
| | 3 | Binary, some function |
| | 4 | Binary, some function |
| | 5 | Binary, some function |
| | 6 | Binary, some function |
| | 7 | Binary, some function |
+------------------------+---------------+--------------------------+
| 104 | 0 | Binary, some function |
| | 1 | Binary, some function |
| | 2 | Binary, some function |
| | 3 | Binary, some function |
| | 4 | Binary, some function |
| | 5 | Binary, some function |
| | 6 | Binary, some function |
| | 7 | Binary, some function |
+------------------------+---------------+--------------------------+
| 112 | | Current level, 0-200 |
+------------------------+---------------+--------------------------+
The manual also explains that the MODBUS functions 0x03, 0x01, 0x06, 0x10, and 0x05 are supported.
My plan is to use this function from pyModbusTCP to read the coils: read_coils(bit_addr, bit_nb=1) and write_single_coil(bit_addr, bit_value).
Modbus function READ_COILS (0x01)
Parameters:
bit_addr (int) – bit address (0 to 65535)
bit_nb (int) – number of bits to read (1 to 2000)
Returns:
bits list or None if error
Return type:
list of bool or None
My questions to you - kind reader - are the following:
How would one implement to read i.e. the "Modbus address"=104 "Bit"=3 for example? Am I missing something basic? I would like to avoid having to write a custom communication program but rely on pyModbusTCP if possible. The read bits example from pyModbusTCP is here: https://pymodbustcp.readthedocs.io/en/latest/examples/read_bit.html
How would one implement to write i.e. the "Modbus address"=2 "Bit"=6 for example? Is the following correct:
from pyModbusTCP.client import ModbusClient
import time
SERVER_HOST = "localhost"
SERVER_PORT = 502
c = ModbusClient()
c.host(SERVER_HOST)
c.port(SERVER_PORT)
addr = 2
while True:
# if open() is ok, write coils (modbus function 0x01)
if c.is_open():
is_ok = c.write_single_coil(addr, 0) # bit 0
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 1
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 2
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 3
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 4
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 5
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 1) # bit 6 <<--
time.sleep(0.5)
is_ok = c.write_single_coil(addr, 0) # bit 7
time.sleep(0.5)
time.sleep(1)
Many thanks for any insights and help.