python - Modbus python 脚本
问题描述
我想用 Python 尝试 MODBUS,我正在使用下面的代码和sudo python3 modbus_master.py
代码。但我有这些错误:
Traceback (most recent call last):
File "modbus_master.py", line 22, in <module>
sock.send(req)
BrokenPipeError: [Errno 32] Broken pipe
为什么我看到破点子是什么原因?
这是我的基本代码:
import socket
import struct
import time
TCP_IP='192.166.1.30'
TCP_PORT=502
BUFFER_SIZE=0
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
sock.bind((TCP_IP,TCP_PORT))
sock.listen(5)
unitId=16
functionCode=5
print("\n,Switching plug on")
coilId=1
req = struct.pack('12B', 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
int(unitId), int(functionCode), 0x00, int(coilId),
0xff, 0x00)
sock.send(req)
print("TX: (%s)" % req)
time.sleep(2)
print('\nCLOSING SOCKET')
sock.close()
解决方案
推荐阅读
- html - 如何使标题徽标响应更大的屏幕宽度?
- ios - 铸造一个泛型类
- swagger - 如何在 Loopback 4 的 Swagger 中添加“授权标头”输入?
- javascript - Javascript 按日期过滤 JSON
- java - 为什么 Spring 强调紧密耦合,尽管它可以通过编程方式克服?
- r - R:R中的“折叠”行
- python-3.x - python derived class constructor
- azure - Azure Application Insight:将遥测流式传输到文件
- javascript - 代码中的哪个函数有错误以及如何修复代码以再次正常工作
- html - Safari密码建议更改输入框大小