stlehmann / pyads

Python wrapper for TwinCAT ADS
MIT License
254 stars 94 forks source link

Failed to open port on AMS router #395

Closed P3li77ari closed 2 months ago

P3li77ari commented 2 months ago

Hello everyone, I am unable to connect to local or remote TwinCat PLC. I am working with Windows 11, Python 3.12.4. Currently, the code looks like this:

import pyads

AmsNetId = "192.168.38.155.1.1"
IpAddress = "192.168.38.71"

plc= pyads.Connection(AmsNetId, pyads.PORT_TC3PLC1, IpAddress)
plc.open()

Port is number 851 as following. image

The traceback is:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>  
    cli.main()
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code  
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\SimpleTestForPyAds.py", line 7, in <module>
    plc.open()
  File "c:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\Lib\site-packages\pyads\connection.py", line 195, in open
    self._port = adsPortOpenEx()
                 ^^^^^^^^^^^^^^^
  File "c:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\Lib\site-packages\pyads\pyads_ex.py", line 434, in adsPortOpenEx
    raise RuntimeError("Failed to open port on AMS router.")
RuntimeError: Failed to open port on AMS router.

I took IpAddress data from ipconfig and AmsNetId from Router>Change AMS ID image image

Local PLC is running as you can see here: image

I also tried simple commands like pyads.open_port()

Traceback in this case is the following:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>  
    cli.main()
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code  
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\alessandro.pelizzari\.vscode\extensions\ms-python.debugpy-2024.8.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\SimpleTestForPyAds.py", line 9, in <module>
    pyads.open_port()
  File "c:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\Lib\site-packages\pyads\ads.py", line 109, in open_port
    port = port or adsPortOpenEx()
                   ^^^^^^^^^^^^^^^
  File "c:\Users\alessandro.pelizzari\Desktop\Pitone_Progetti\virtualEnv\Lib\site-packages\pyads\pyads_ex.py", line 434, in adsPortOpenEx
    raise RuntimeError("Failed to open port on AMS router.")
RuntimeError: Failed to open port on AMS router.

Same issue happens even working with a remote target, where I can see the route connected and running. Thanks for your kind help.

chrisbeardy commented 2 months ago

hello, thanks for your issue,. In order to assist with issue management, please keep the issue tracker reserved for bugs and feature requests. For any questions, particularly around usage, route creation and ads error messages when reading or writing variables, please use Stack Overflow tagging the question with twincat-ads and state you are using the pyads library.

Have you checked that you have a route to your PLC set up when connecting remotely https://pyads.readthedocs.io/en/latest/documentation/routing.html#creating-routes-on-windows. When you say local, do you mean you are running python on the same windows PC as the Beckhoff PLC? If so, please remove the IP address from the Connection line.