Ethernet Socket Reader 2 PLC (Mitsubishi)
接受模式选择: 0 Normal 1 固定长度
LD SM412 (1S 时钟)
LD SD1282.0 (端口打开完成)
Out M8015(通讯中)
------
import socket def read_data_from_plc(host, port, plc_command): try: # 创建socket对象 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # 连接到PLC s.connect((host, port)) # 发送读取命令 s.sendall(plc_command.encode()) # 接收数据 data = b'' while True: part = s.recv(1024) if not part: break data += part return data.decode() except ConnectionRefusedError: print("Connection refused. Please check the PLC's IP address and port number.") except socket.timeout: print("Socket timeout occurred. Check network connectivity.") except socket.error as e: print(f"Socket error occurred: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") if __name__ == '__main__': # PLC IP