4

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()
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
CKocar
  • 566
  • 9
  • 25
  • Possible duplicate of [How to prevent errno 32 broken pipe?](https://stackoverflow.com/questions/11866792/how-to-prevent-errno-32-broken-pipe) – Karan Shishoo Oct 30 '18 at 09:29

0 Answers0