我正试图用Python从我的pc(192.168.0.2)向西门子网络输入(IP:192.168.0.11:504)发送1位数据。但我不能让它工作。目标是通过modbus连接发送bit以触发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, 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")
根据我的评论
tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 0, 0])
写了四个线圈(1/对线圈1为真,然后0/对线圈2、3和4为假);要编写单个线圈,请使用write_single_coil
或tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1])
(这最好取决于您的设备;并非所有设备都实现这两种功能,但我建议从write_single_coil
开始)相关问题 更多 >
编程相关推荐