猿问

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

我正在尝试使用 Python 从我的电脑 (192.168.0.2) 发送 1 位到西门子网络输入 (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, 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")


忽然笑
浏览 177回答 1
1回答

慕码人8056858

根据我的评论tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 0, 0])写四个线圈(1/true 到线圈 1,然后 0/false 到线圈 2,3 和 4);编写单个线圈使用write_single_coil或tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1])(这最好取决于您的设备;并非所有设备都实现这两种功能,但我建议从 开始write_single_coil)。
随时随地看视频慕课网APP

相关分类

Python
我要回答