使用Python将位发送到Siemens Logo 8modbus连接

问题描述

我正在尝试使用Python将1位数据从我的PC(192.168.0.2)发送到Siemens网络输入(IP:192.168.0.11:504)。但是我不能使它工作。目的是通过Modbus连接发送位以触发BO31条件。

我的Python代码

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])
 
# 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")

解决方法

根据我的评论tcp.write_multiple_coils(slave_id=1,starting_address=1,values=[1,0]),写了四个线圈(线圈1为1,是true,线圈2,3和4为0 / false);使用write_single_coiltcp.write_multiple_coils(slave_id=1,values=[1])(最好取决于您的设备;不是所有设备都实现这两种功能,但我建议从write_single_coil开始)来写一个线圈。