stlehmann / pyads

Python wrapper for TwinCAT ADS
MIT License
247 stars 93 forks source link

Connection between Linux and Beckhoff closed by remote #391

Open AndyEverything opened 2 weeks ago

AndyEverything commented 2 weeks ago
          I finally managed to get the connection working. I decided to make this little "guide" on how to setup the routes.

Lets assume two computers on the same lan (I have disabled firewalls on both of them):

Target PC

Client PC

Goal is to read and change variables of PLC (running on Target PC) from Python script (Client PC)

1. SET ROUTE ON TARGET PC, USING TWINCAT:

2. Obtain TARGET_AMS_ID from Target PC:

3. Finnaly our python script on Client PC:

import pyads

pyads.open_port()
# CLIENT PC IP with .1.1 at the end (we added this when creating route via TwinCat):
CLIENT_AMS_NET_ID = "172.18.10.178.1.1"
pyads.set_local_address(CLIENT_AMS_NET_ID)
pyads.close_port()
# pass
# connect to plc and open connection
# route is added automatically to client on Linux, on Windows use the TwinCAT router
# we obtained TARGET_AMS_ID in step 2:
TARGET_AMS_ID = "192.168.0.31.1.1"
# regular ip address of target pc:
TARGET_PC_ID = "172.18.10.112"
plc = pyads.Connection(TARGET_AMS_ID, 851, TARGET_PC_ID)
plc.open()
# in MAIN in my PLC i have bool variable bRunOnlyOnce
b = plc.read_by_name("MAIN.bRunOnlyOnce")
print(b)
plc.write_by_name("MAIN.bRunOnlyOnce", False)
exit()

Don't forget to run your PLC. As I discovered both TwinCAT and pyads very recently feel free to edit/correct this. This issue helped me to make everything working and create this post.

Originally posted by @CreaM129129 in https://github.com/stlehmann/pyads/issues/281#issuecomment-956370290

AndyEverything commented 2 weeks ago

Many thanks for the detailed instructions. I have a Linux PC (Ubuntu) and would like to connect to a Beckhoff. The firewalls are off on both devices and the PLC is in run mode. I have created the route from the PLC to the PC manually according to the instructions.

I run my Python script on the Linux PC:

import pyads
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()
device_name, version = plc.read_device_info()
print(str(device_name) + ' ' + str(version))
print(f"Connected?: {plc.is_open}")
print(f"Local Address? : {plc.get_local_address()}")
print(plc.read_state())

plc.read_by_name("MAIN.test", pyads.PLCTYPE_INT)
exit()

I receive the following feedback:

~$ python3 pyads_test.py
2024-06-19T07:44:04+0000 Info: Connected to 192.168.0.1
Plc30 App <pyads.structs.AdsVersion object at 0x7f67198859d0>
Connected?: True
Local Address? : <AmsAddress 192.168.0.253.1.1:30000>
(5, 0)
2024-06-19T07:44:04+0000 Info: connection closed by remote

For me, it looks like I can make a connection. Otherwise I wouldn't be able to read out the status. I could also create the route via add_route_to_plc. In this case, I have done it manually again. Nevertheless, the connection is closed by romete. Do you have a clue as to what the problem could be?

chrisbeardy commented 2 weeks ago

Yes, it looks like you are creating a connection ok as you can ready the running state of the PLC (5 is PLC run). I am not sure what is now causing this to disconnect. What versions of python, twincat, ubuntu and pyads are you using? Are there any firewalls set up?

AndyEverything commented 2 weeks ago

I have an Ubuntu 18.04.6 LTS with

The PLC has TwinCAT 3.1.4024.29. I have deactivated the firewall on the Beckhoff and I have not set any special rules on the Linux.

I started a Testserver on the Linux PC according to the documentation and was able to connect to it without any problems.

chrisbeardy commented 2 weeks ago

I can't see anything wrong with that setup, you could try using an older version of pyads, we have recently changed how we are doing the build on Linux, so that may have untoward issues.

AndyEverything commented 2 weeks ago

I have installed pyads on the Linux via pip. Since the installation and import works, I left it like this for a start. I should install it from source as recommended in the documentation.