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