pyads - “连接”对象没有属性“_open”属性错误

问题描述 投票:0回答:1

我创建了一个基本的 python 脚本,使用后端服务器中的 pyads 连接到 PLC。但是,我收到属性错误:“连接”对象有错误没有属性“_open”

其他详细信息:

Ubuntu 20.04.2 LTS

错误输出-

  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 并发现了最近的 问题 关于 pyads 无法工作。

建议的解决方案是将python版本降级到3.7

我按照这个线程的说明创建了一个虚拟环境并安装了python3.7。

但是,出现了同样的错误代码。

检查我的 PLC IP 地址和 AMS 网络 ID。一切都正确,我能够 ping 我的 PLC,没有任何问题

plc twincat-ads
1个回答
0
投票

您必须在尝试连接之前创建一条路线。您可以按照pyads文档页面中的步骤操作。

问题解决了

© www.soinside.com 2019 - 2024. All rights reserved.