我正试图通过TCP与Modbus进行通信。我想在没有库的情况下使用这类代码。
代码按如下方式运行:
 sudo python3 modbus_master.py当我用这段代码运行程序时,我会看到我在Wireshark中定义的Modbus连接。我也使用从Modbus程序(不止一个),但没有与我的主人连接。
,我在下面的代码中做错了什么?
#!/usr/bin/python3           
# This is client.py file
import socket
import struct
import time
# Create a TCP/IP socket
TCP_IP = '192.168.0.107'
TCP_PORT = 502
BUFFER_SIZE = 39
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((TCP_IP, TCP_PORT))
try:
    unitId = 16
    functionCode = 5
    print("\n,Switching plug on")
    coilId = 1
    req = struct.pack('12B', 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, int(unitId), int(functionCode), 0x00, int(coilId),
                      0xff,
                      0x00)
    sock.send(req)
    print("TX: (%s)" % req)
    time.sleep(2)
finally:
    print('\nCLOSING SOCKET')
    sock.close()发布于 2018-10-30 09:12:08
我认为您的问题是在IP或端口与防火墙。
因此,如果代码在同一台计算机上运行,则可以使用localhost或127.0.0.1 IP而不是机器IP。
注意事项
如果您的操作系统是基于*nix系统的,并且有ufw防火墙,则执行以下命令:
$ sudo ufw disablehttps://stackoverflow.com/questions/52982443
复制相似问题