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

使用pyads连接PLC时遇'Connection'无'_open'属性等报错求助

问题:pyads连接PLC时出现AttributeError:'Connection'对象无'_open'属性及AdsException找不到

环境信息

  • Ubuntu 20.04.2 LTS
  • Python 3.7(虚拟环境)
  • pyads库

报错输出

File "testOne.py", line 28, in <module>
    connect_to_plc(plc_ip, ams_net_id, ams_port)
  File "testOne.py", line 19, in connect_to_plc
    except pyads.AdsException as e:
AttributeError: module 'pyads' has no attribute 'AdsException'
Exception ignored in: <function Connection.__del__ at 0x7f3985d93e60>
Traceback (most recent call last):
  File "/home/ubuntu/virtual_env/venv_with_python3.7/lib/python3.7/site-packages/pyads/connection.py", line 168, in __del__
    self.close()
  File "/home/ubuntu/virtual_env/venv_with_python3.7/lib/python3.7/site-packages/pyads/connection.py", line 204, in close
    if not self._open:
AttributeError: 'Connection' object has no attribute '_open'

代码示例

import pyads

def connect_to_plc(plc_ip, ams_net_id, ams_port):
    try:
        # Connect to the PLC
        ads = pyads.Connection(plc_ip, ams_net_id, ams_port)

        # Open a connection
        ads.open()

        print(f"Connected to PLC {plc_ip} on {ams_net_id}:{ams_port}")

        # Add your PLC communication logic here

        # Close the connection when done
        ads.close()
        print("Connection closed.")

    except pyads.AdsException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    # Replace these values with your PLC information
    plc_ip = "<ip>"  # Replace with your PLC IP address
    ams_net_id = "<net_id>"  # Replace with your AMS Net ID
    ams_port = 851  # Replace with your PLC AMS port number

    connect_to_plc(plc_ip, ams_net_id, ams_port)

已排查步骤

  • 查看pyads GitHub相关issue,按建议降级Python至3.7并创建虚拟环境,问题未解决
  • 确认PLC IP和AMS Net ID正确,可正常ping通PLC

解决方案

1. 版本兼容性问题排查

问题根源是pyads版本与Python版本不匹配,或安装的pyads版本存在API结构变更:

  • 新版本pyads(3.x+)中,AdsException已移至pyads.exceptions模块
  • Connection类的初始化参数顺序发生了变更,旧版本参数顺序为(plc_ip, ams_net_id, ams_port),新版本为(ams_net_id, ams_port, ip_address=None)

先检查当前pyads版本:

pip show pyads

2. 适配新版本pyads的代码修正

如果使用pyads 3.x+,修改代码如下:

import pyads
from pyads.exceptions import AdsException

def connect_to_plc(plc_ip, ams_net_id, ams_port):
    try:
        # 调整参数顺序:ams_net_id → ams_port → ip_address
        ads = pyads.Connection(ams_net_id, ams_port, ip_address=plc_ip)

        ads.open()
        print(f"Connected to PLC {plc_ip} on {ams_net_id}:{ams_port}")

        # 添加PLC通信逻辑

        ads.close()
        print("Connection closed.")

    except AdsException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    plc_ip = "<ip>"  # 替换为实际PLC IP
    ams_net_id = "<net_id>"  # 替换为实际AMS Net ID
    ams_port = 851  # 替换为实际PLC AMS端口

    connect_to_plc(plc_ip, ams_net_id, ams_port)

3. 安装适配Python3.7的旧版本pyads

若不想修改代码,可安装适配Python3.7的pyads 2.x版本:

pip uninstall pyads
pip install pyads==2.3.1

该版本中pyads.AdsException可直接使用,Connection参数顺序与原代码一致。

4. 验证参数正确性

若仍出现_open属性缺失,说明Connection初始化失败,需确认:

  • AMS Net ID格式正确(例如192.168.1.100.1.1)
  • AMS端口号与PLC配置匹配(851为常见端口,但需以PLC实际配置为准)

内容的提问来源于stack exchange,提问作者Selva Vignesh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:58:34