You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Siemens LOGO! Comfort V8 Modbus TCP写入网口输入故障求助

解决Siemens LOGO! Comfort V8与Python Modbus TCP通信的ConnectionResetError问题

错误原因分析

ConnectionResetError: [WinError 10054] 是因为LOGO!主动断开了连接,通常是通信参数不匹配、LOGO!端Modbus配置未启用或者寄存器类型/地址错误导致的。

第一步:检查LOGO!端的Modbus配置

  • 启用Modbus TCP服务器:打开LOGO! Soft Comfort,进入「通信」>「Modbus TCP」,勾选「启用Modbus TCP服务器」,确认端口为502(与代码一致)。
  • 确认从站地址:LOGO!默认Modbus从站ID为1,若修改过需同步到代码中。
  • 网口输入地址映射:LOGO!的网口数字输入对应Modbus的线圈(Coils),且软件中显示的是1-based地址,代码中需转换为0-based(比如地址1对应代码中的0)。
  • 运行状态:确保LOGO!处于RUN模式,编辑模式下不会响应Modbus请求。

第二步:验证网络连通性

  • 用命令行执行 ping 192.168.0.2,确认电脑与LOGO!在同一子网(比如电脑IP为192.168.0.x,子网掩码255.255.255.0)。
  • 关闭电脑的Windows防火墙(临时测试),或添加允许502端口出站的规则。

第三步:修正Python代码

Pymodbus 正确示例

from pymodbus.client import ModbusTcpClient

# 配置参数
LOGO_IP = "192.168.0.2"
LOGO_PORT = 502
SLAVE_ID = 1  # 与LOGO!配置的从站ID一致
INPUT_ADDRESS = 0  # 对应LOGO!中1-based的地址1

client = ModbusTcpClient(LOGO_IP, port=LOGO_PORT)
if client.connect():
    try:
        # 写入网口数字输入:True为ON,False为OFF
        response = client.write_coil(address=INPUT_ADDRESS, value=True, slave=SLAVE_ID)
        if not response.isError():
            print("网口输入写入成功")
        else:
            print(f"写入失败: {response}")
    except Exception as e:
        print(f"通信异常: {e}")
    finally:
        client.close()
else:
    print("无法连接到LOGO!,请检查网络或配置")

Umodbus 正确示例

from umodbus import conf
from umodbus.client import tcp

# 配置Modbus参数
conf.SIGNED_VALUES = False
LOGO_IP = "192.168.0.2"
LOGO_PORT = 502
SLAVE_ID = 1
INPUT_ADDRESS = 0  # 对应LOGO!中1-based的地址1

# 建立连接
sock = tcp.connect(host=LOGO_IP, port=LOGO_PORT)
try:
    # 写入单个线圈(网口数字输入)
    request = tcp.write_single_coil(slave_id=SLAVE_ID, address=INPUT_ADDRESS, value=True)
    tcp.send_message(request, sock)
    print("网口输入写入成功")
except Exception as e:
    print(f"通信异常: {e}")
finally:
    sock.close()

额外注意事项

  • 不要混淆寄存器类型:网口数字输入是线圈(Coils),而非离散输入寄存器(Discrete Inputs,这是只读的),错误的寄存器类型会直接导致连接断开。
  • 若仍有问题,用Modbus调试工具(比如Modbus Poll)先测试通信,确认参数正确后再移植到Python代码中。

内容的提问来源于stack exchange,提问作者Joaquin Perez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 07:20:56