连接到多个客户端TCP modbus



我有一个工作的modbus服务器和客户端,使用我在occomputer上运行的python脚本,它通过使用socket.connect与客户端的ip和端口进行连接,并允许交换modbus请求。

我不知道如何将此应用于多个ip。我想让我的程序连接到10个不同的modbus客户端,并向它们查询信息。

是否可以调用多个socket.connect来打开多个流?如果是的话,当每个客户端都需要一个唯一的从属ID时,它的功能会类似于串行连接吗?

编辑:

#sets all the standard values
PLC1_IP = '10.0.238.34'
PLC1_Port = 504
PLC2_IP = '10.0.188.34'
PLC2_Port = 505

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except (socket.error, msg):
    print("Unable to create socket. " + msg[1])
    sys.exit()
#connects to the plc using the port and ip    
s.connect((PLC1_IP, PLC2_Port))
PDU = struct.pack(">BHH", Function_Write, coil_number, Coil_On)
MBPA = struct.pack('>HHHB', 100, PLC1_Protocall, PLC1_Length, PLC1_Unit)
s.send(MBPA+PDU)
s.recv(1024)

#insert main code here
#s.close

这是我现在用来打开一个连接并发送modbus数据包的代码。如果我想打开与第二个PLC的连接,我该怎么做?

def make_socket(ip):
    s= socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.connecnt((ip,port))
    return s
my_clients = [make_socket(ip) for ip in ip_addresses]
for client in my_clients:
    client.send("Hello")
    print client.recv(100) # or whatever

也许是这样的。。。很难不看到你的任何代码。。。

最新更新