首页 > 解决方案 > 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()

标签: pythonpython-3.xwiresharkethernet

解决方案


推荐阅读