title: Python TCP/IP Communication
body: <p>This is my python modbus tcp communication code and it wait at the this line and than stopping for the connection where is my fault:</p>
  
  <pre><code>sock.connect((TCP_IP, TCP_PORT))
  </code></pre>
  
  <p>(if i use slave program also not working)
  At the my client side i am using this code:</p>
  
  <pre><code>TCP_IP='10.0.2.15'
  TCP_PORT=502
  BUFFER_SIZE=39
  sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  
  sock.connect((TCP_IP,TCP_PORT))
  </code></pre>
  
  <p>This is the master side:</p>
  
  <pre><code># Create a TCP/IP socket
  TCP_IP = '10.0.2.2'
  TCP_PORT = 502
  BUFFER_SIZE = 39
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.connect((TCP_IP, TCP_PORT))
  
  
  
  try:
  
      unitId = 16  # Plug Socket11
      functionCode = 3  # Write coil
  
      print("\nSwitching Plug ON...")
      coilId = 1
      req = struct.pack('12B', 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, int(unitId), 0x03, 0xff, 0xc0, 0x00,
                    0x00)
      sock.send(req)
      print("TX: (%s)" % req)
      rec = sock.recv(BUFFER_SIZE)
      print("RX: (%s)" % rec)
      time.sleep(2)
  
      print("\nSwitching Plug OFF...")
      coilId = 2
      req = struct.pack('12B', 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, int(unitId), 0x03, 0xff, 0xc0, 0x00,
                    0x00)
      sock.send(req)
      print("TX: (%s)" % req)
      rec = sock.recv(BUFFER_SIZE)
      print("RX: (%s)" % rec)
      time.sleep(2)
  
  finally:
      print('\nCLOSING SOCKET')
      sock.close()
  </code></pre>