omron fins 内存区域写入(MEMORY AREA WRITE)
1. 完整的代码如下:
import socket
import binasciiclass Omron:def __init__(self, ip, port=9600):self.ip = ip # PLC的IP地址self.port = port # PLC的端口,默认为9600def send_receive_fins(self):with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sc:sc.settimeout(30) # 设置socket超时时间为30秒try:sc.connect((self.ip, self.port)) # 尝试连接到PLCsc.send(binascii.unhexlify('46494e530000000c000000000000000000000000')) # 发送初始化包response = sc.recv(4096) # 接收PLC的响应if binascii.hexlify(response)[:4] != b'4649': # 检查响应是否以'FINS'开始return False# 第一部分:FINS/TCP Header 字段信息magic_bytes = "46494e53" # FINS, 4个字节# 修改:需要根据写入数据的长度来设置长度字段length = "0000002c" # length以后所有字段的长度,4个字节