Python TCP/IP 通讯
Python TCP/IP Communication
这是我的 python modbus tcp 通信代码,它在此行等待,而不是停止连接,这是我的错:
sock.connect((TCP_IP, TCP_PORT))
(如果我使用从程序也不起作用)
在我的客户端,我正在使用此代码:
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))
这是主控端:
# 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()
我认为您的问题是 IP 地址:10.0.2.2
如此处所述 [。
您可以将 '10.0.2.2'
替换为 'localhost'
或尝试查找您的 IPv4
地址。
如果您使用 Linux,则在命令提示符中键入 ifconfig 或在 windows 中键入 ipconfig 并搜索 IPv4
地址。
我使用了一个简单的客户端-服务器示例来 运行 您的代码并将 '10.0.2.2'
替换为 'localhost'
并且一切正常。
服务器端:
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()
客户端:
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()
确保 运行 服务器和客户端分开 files/terminals
我认为你的问题是"Why is the sock.connect() call hanging?"。那是因为默认情况下它会无限期地等待连接。也就是说,调用默认是'blocking'。如果你只想等待最多 500 毫秒的连接,你需要这样指定:
sock.connect(.5) #wait 500 milliseconds for a connection attempt
另外,参见 Python socket connection timeout
这是我的 python modbus tcp 通信代码,它在此行等待,而不是停止连接,这是我的错:
sock.connect((TCP_IP, TCP_PORT))
(如果我使用从程序也不起作用) 在我的客户端,我正在使用此代码:
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))
这是主控端:
# 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()
我认为您的问题是 IP 地址:10.0.2.2
如此处所述 ['10.0.2.2'
替换为 'localhost'
或尝试查找您的 IPv4
地址。
如果您使用 Linux,则在命令提示符中键入 ifconfig 或在 windows 中键入 ipconfig 并搜索 IPv4
地址。
我使用了一个简单的客户端-服务器示例来 运行 您的代码并将 '10.0.2.2'
替换为 'localhost'
并且一切正常。
服务器端:
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()
客户端:
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()
确保 运行 服务器和客户端分开 files/terminals
我认为你的问题是"Why is the sock.connect() call hanging?"。那是因为默认情况下它会无限期地等待连接。也就是说,调用默认是'blocking'。如果你只想等待最多 500 毫秒的连接,你需要这样指定:
sock.connect(.5) #wait 500 milliseconds for a connection attempt
另外,参见 Python socket connection timeout