Search code examples
pythontcpmodbus

Python TCP/IP Communication


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()

Solution

  • I think your problem is the IP address: 10.0.2.2 as stated here [Connection to LocalHost/10.0.2.2 from Android Emulator timed out. You can replace '10.0.2.2' with 'localhost' or try to find your IPv4 address.

    To do so type ifconfig in your command prompt if you use Linux or type ipconfig in windows and search for the IPv4 address.

    I used a simple client-server example to run your code and replaced '10.0.2.2' with 'localhost' and everything went fine.

    server side:

    import socket
    import struct
    import time
    
    TCP_IP = 'localhost'                 
    TCP_PORT = 502
    BUFFER_SIZE = 39
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((TCP_IP, TCP_PORT))
    s.listen(1)
    sock, addr = s.accept()
    print 'Connected by', addr
    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)
        while 1:
            sock.send(req)
            print("TX: (%s)" % repr(req))
            rec = sock.recv(BUFFER_SIZE)
            print("RX: (%s)" % repr(rec))
            time.sleep(2)
            break
    
        print("\nSwitching Plug OFF...")
        coilId = 2
        req = struct.pack('12B', 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 
        int(unitId), 
        0x03, 0xff, 0xc0, 0x00,
        0x00)
        while 1:
            sock.send(req)
            print("TX: (%s)" % repr(req))
            rec = sock.recv(BUFFER_SIZE)
            print("RX: (%s)" % repr(rec))
            time.sleep(2)
            break
    finally:
        sock.close()
    

    client side:

    import socket
    
    TCP_IP = 'localhost'    
    TCP_PORT = 502            
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((TCP_IP, TCP_PORT))
    
    data = sock.recv(1024)
    print  repr(data)
    while 1:
        sock.send(data)
        print("send back to server: (%s)" % repr(data))
        break
    
    data = sock.recv(1024)
    print  repr(data)
    while 1:
        sock.send(data)
        print("send back to server: (%s)" % repr(data))
        break
    sock.close()
    

    make sure you run server and client in seperate files/terminals