1

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()
asked Oct 25, 2018 at 12:44
4
  • 1
    What's your question? Commented Oct 25, 2018 at 12:49
  • where is my fault ? Commented Oct 25, 2018 at 12:51
  • How specifically is the program not working? What is the error that you are receiving? Commented Oct 25, 2018 at 13:01
  • i have no error i said program waiting this line and stopping without connection sock.connect((TCP_IP, TCP_PORT)) Commented Oct 25, 2018 at 13:04

2 Answers 2

1

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

answered Oct 25, 2018 at 13:44
Sign up to request clarification or add additional context in comments.

1 Comment

I added do stuff here line this : req = struct.pack('12B', 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, int(unitId), 0x03, 0xff, 0xc0, 0x00, 0x00) sock.send(req) I have this error """ sock.send(req) BrokenPipeError: [Errno 32] Broken pipe """
0

I think your question is "Why is the sock.connect() call hanging?". That's because by default it waits for a connection indefinitely. In other words, the call is 'blocking' by default. If you want to only wait up to 500 milliseconds for a connection, you need to specify this:

sock.connect(.5) #wait 500 milliseconds for a connection attempt

Also, see Python socket connection timeout

answered Oct 25, 2018 at 14:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.