OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Connection between Beckhoff PLC and Linux with PyADS

  • Thread starter Thread starter AndyEverything
  • Start date Start date
A

AndyEverything

Guest
I would like to connect my Beckhoff CX5230 (Win10) to a Linux PC via PyADS. A topic that has been discussed here several times, but no solution has worked for me so far.

Beckhoff PLC Routing on Linux

Pyads connection refused with Beckhoff running Twincat 3

read and write variables in the PLC (beckhoff CX9020) with python pyads module

First, I created a route on the PLC from my Linux:

Code:
import pyads

SENDER_AMS = '5.97.120.143.1.1'
PLC_IP = '192.168.0.1'
PLC_USERNAME = 'Administrator'
PLC_PASSWORD = '1'
ROUTE_NAME = 'RouteToPC'
HOSTNAME = '192.168.0.1'

pyads.open_port()
pyads.set_local_address(SENDER_AMS)
pyads.add_route_to_plc(SENDER_AMS, HOSTNAME, PLC_IP, PLC_USERNAME, PLC_PASSWORD, route_name=ROUTE_NAME)
pyads.close_port()

That has worked well enough so far. I connected a monitor to the Beckhoff and saw that a new route had been created.

I was also able to establish a ping from the PLC to the PC and from the PC to the PLC. I therefore assume that the IP addresses are correct and the connection is established.

Next, I try to establish a route from my PC to the PLC:

Code:
import pyads

TARGET_AMS_ID = '5.97.120.143.1.1'
PLC_IP = '192.168.0.1'

plc = pyads.Connection(TARGET_AMS_ID, pyads.PORT_TC3PLC1, PLC_IP)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())

When I run the script, the following error occurs:

Code:
2024-06-12T17:28:09+0000 Info: Connected to 192.168.0.1
Connected?: True
Local Address? : <AmsAddress 192.168.0.253.1.1:30000>
Traceback (most recent call last):
  File "pyads_test.py", line 24, in <module>
    print(plc.read_state())
  File "/home/builder/.local/lib/python3.7/site-packages/pyads/connection.py", line 237, in read_state
    return adsSyncReadStateReqEx(self._port, self._adr)
  File "/home/builder/.local/lib/python3.7/site-packages/pyads/pyads_ex.py", line 510, in adsSyncReadStateReqEx
    raise ADSError(error_code)
pyads.pyads_ex.ADSError: ADSError: timeout elapsed (1861).
2024-06-12T17:28:14+0000 Info: connection closed by remote

I also tried these instructions here and created the route manually: https://github.com/stlehmann/pyads/issues/281#issuecomment-956370290

Still have a "connection closed by remote" error:

Code:
pyads.open_port()
CLIENT_AMS_NET_ID = "192.168.0.253.1.1"
pyads.set_local_address(CLIENT_AMS_NET_ID)
pyads.close_port()
TARGET_AMS_ID = "5.97.120.143.1.1"
TARGET_PC_ID = "192.168.0.1"

plc = pyads.Connection(TARGET_AMS_ID, pyads.PORT_TC3PLC1, TARGET_PC_ID)
plc.open()
plc.write_by_name("GVL.P1", True)
exit()

builder@user:~$ python3 pyads_test.py
2024-06-18T08:49:10+0000 Info: Connected to 192.168.0.1
2024-06-18T08:49:10+0000 Info: connection closed by remote
<p>I would like to connect my Beckhoff CX5230 (Win10) to a Linux PC via PyADS. A topic that has been discussed here several times, but no solution has worked for me so far.</p>
<p><a href="https://stackoverflow.com/questions/72864873/beckhoff-plc-routing-on-linux/72874470#72874470">Beckhoff PLC Routing on Linux</a></p>
<p><a href="https://stackoverflow.com/questions...ction-refused-with-beckhoff-running-twincat-3">Pyads connection refused with Beckhoff running Twincat 3</a></p>
<p><a href="https://stackoverflow.com/questions...-plc-beckhoff-cx9020-with-python-pyads-module">read and write variables in the PLC (beckhoff CX9020) with python pyads module</a></p>
<p>First, I created a route on the PLC from my Linux:</p>
<pre><code>import pyads

SENDER_AMS = '5.97.120.143.1.1'
PLC_IP = '192.168.0.1'
PLC_USERNAME = 'Administrator'
PLC_PASSWORD = '1'
ROUTE_NAME = 'RouteToPC'
HOSTNAME = '192.168.0.1'

pyads.open_port()
pyads.set_local_address(SENDER_AMS)
pyads.add_route_to_plc(SENDER_AMS, HOSTNAME, PLC_IP, PLC_USERNAME, PLC_PASSWORD, route_name=ROUTE_NAME)
pyads.close_port()
</code></pre>
<p>That has worked well enough so far. I connected a monitor to the Beckhoff and saw that a new route had been created.</p>
<p>I was also able to establish a <em>ping</em> from the PLC to the PC and from the PC to the PLC. I therefore assume that the IP addresses are correct and the connection is established.</p>
<p>Next, I try to establish a route from my PC to the PLC:</p>
<pre><code>import pyads

TARGET_AMS_ID = '5.97.120.143.1.1'
PLC_IP = '192.168.0.1'

plc = pyads.Connection(TARGET_AMS_ID, pyads.PORT_TC3PLC1, PLC_IP)
plc.open()
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())
</code></pre>
<p>When I run the script, the following error occurs:</p>
<pre><code>2024-06-12T17:28:09+0000 Info: Connected to 192.168.0.1
Connected?: True
Local Address? : <AmsAddress 192.168.0.253.1.1:30000>
Traceback (most recent call last):
File "pyads_test.py", line 24, in <module>
print(plc.read_state())
File "/home/builder/.local/lib/python3.7/site-packages/pyads/connection.py", line 237, in read_state
return adsSyncReadStateReqEx(self._port, self._adr)
File "/home/builder/.local/lib/python3.7/site-packages/pyads/pyads_ex.py", line 510, in adsSyncReadStateReqEx
raise ADSError(error_code)
pyads.pyads_ex.ADSError: ADSError: timeout elapsed (1861).
2024-06-12T17:28:14+0000 Info: connection closed by remote
</code></pre>
<p>I also tried these instructions here and created the route manually:
<a href="https://github.com/stlehmann/pyads/issues/281#issuecomment-956370290" rel="nofollow noreferrer">https://github.com/stlehmann/pyads/issues/281#issuecomment-956370290</a></p>
<p>Still have a "connection closed by remote" error:</p>
<pre><code>pyads.open_port()
CLIENT_AMS_NET_ID = "192.168.0.253.1.1"
pyads.set_local_address(CLIENT_AMS_NET_ID)
pyads.close_port()
TARGET_AMS_ID = "5.97.120.143.1.1"
TARGET_PC_ID = "192.168.0.1"

plc = pyads.Connection(TARGET_AMS_ID, pyads.PORT_TC3PLC1, TARGET_PC_ID)
plc.open()
plc.write_by_name("GVL.P1", True)
exit()

builder@user:~$ python3 pyads_test.py
2024-06-18T08:49:10+0000 Info: Connected to 192.168.0.1
2024-06-18T08:49:10+0000 Info: connection closed by remote
</code></pre>
 
Top