使用Python(modbus连接)将位发送至西门子Logo 8

2024-09-28 23:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用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")

Tags: fromimportmessageisresponseconfsockettcp
1条回答
网友
1楼 · 发布于 2024-09-28 23:10:17

根据我的评论tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 0, 0])写了四个线圈(1/对线圈1为真,然后0/对线圈2、3和4为假);要编写单个线圈,请使用write_single_coiltcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1])(这最好取决于您的设备;并非所有设备都实现这两种功能,但我建议从write_single_coil开始)

相关问题 更多 >