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:
sock.connect((TCP_IP, TCP_PORT))
(if i use slave program also not working) At the my client side i am using this 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))
This is the master side:
# 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()