Search code examples
pythonmodbussiemens

Send bit to Siemens Logo 8 with Python (modbus connection)


I am trying to send 1 bit from my pc (192.168.0.2) to Siemens network input (IP: 192.168.0.11:504) with Python. But i can't get it work. The goal is to send bit via modbus connection to trigger BO31 condition.

My Python code:

import socket
from umodbus import conf
from umodbus.client import tcp
 
# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True
 
### Creating connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.11', 504))
 
message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 0, 0])
 
# Response depends on Modbus function code. This particular returns the
# amount of coils written, in this case it is.
response = tcp.send_message(message, sock)
print(response)
sock.close()
print("Transfer finished")

Solution

  • As per my comment tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 0, 0]) writes four coils (1/true to coil 1 then 0/false to coils 2,3 & 4); to write a single coil either use write_single_coil or tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1]) (which is best depends upon your device; not all devices implement both functions but I'd suggest starting with write_single_coil).