I want to try MODBUS with Python, I am using the below code with sudo python3 modbus_master.py
code. But I have these errors:
Traceback (most recent call last):
File "modbus_master.py", line 22, in <module>
sock.send(req)
BrokenPipeError: [Errno 32] Broken pipe
Why I see broken pip what is the reason for?
That is my basic code:
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()