pyads
pyads copied to clipboard
Connection between Linux and Beckhoff closed by remote
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
- Runs windows
- Runs TwinCat3
- Locally simulates PLC (I did not test my aproach on a actuall harware)
- Has some ip, in my case
TARGET_PC_ID = "172.18.10.112"
Client PC
- Runs Ubuntu
- Runs my Python script with pyads
- Has some ip, in my case 172.18.10.178
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:
- Navigate to TwinCAT -> Router -> Edit Routes:
- Add route, tick Advanced Options and fill in details:
Host Name / IP is ip of Client PC. Route Name does not matter. AmsNetid is ip of client PC and 1.1. Adress info is ip of client PC (probbaly does not matter?) Dont forget to set Remote Route to None, untick Unidirectional, Target route to Static. Than Add Route, I left Secure ADS (on pop-up window) unticked.
2. Obtain TARGET_AMS_ID from Target PC:
- Navigate to TwinCAT -> Router -> Change AMS NetId:
Copy displayed addres. We will use it in our python script.
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