Join us today!
Connecting to TwinCAT using Python
We will explain how you can connect to an IPC and read/write the variables using Python in this tutorial.
Before we start writing the python program, create a TwinCAT project and add the variables below in MAIN program. Activate the configuration and log in the PLC.
bReadCmd : BOOL ; bACKFromPython : BOOL; nMyNumber : INT; fMyRealNumber : REAL; sMessageToPython : T_MaxString := 'Hi Python!'; sMessageFromPython : T_MaxString;
If you don't already have a python IDE, you can download the PyCharm Community Edition for free from here. Download the python interpreter as well.
After installing the programs, start the PyCharm IDE and create a python project . To communicate with ADS from python, we will use the pyads library.
Install this library in PyCharm using the terminal and typing;
pip install pyads
Import the pyads library in the project and connect to IPC using the AMS NET ID and the Port Number.
import pyads # connect to the PLC plc = pyads.Connection('169.254.24.120.1.1', pyads.PORT_TC3PLC1)
You can double click on 'Routes' in TwinCAT and go to NETID Management to find out your local net ID.
pyads.PORT_TC3PLC1 is pyads constant and its value set to 851.
Open the ADS connection.
# open the connection plc.open()
You can read the device information as well.
# read the device name and the version device_name, version = plc.read_device_info() print(str(device_name) + ' ' + str(version))
We will read the value of the 'bReadCmd' boolean and write this value to 'bAckFromPython' as a confirmation. When reading a tag value, you need to know the path of the variable and its datatype.
#read a boolean bReadCommand = plc.read_by_name('MAIN.bReadCmd', pyads.PLCTYPE_BOOL) print(bReadCommand) #write ack plc.write_by_name('MAIN.bACKFromPython', bReadCommand)
Now we will read an integer and a real variable.
#read int number int_number = plc.read_by_name('MAIN.nMyNumber', pyads.PLCTYPE_INT) print(int_number) #read real number real_number = plc.read_by_name('MAIN.fMyRealNumber', pyads.PLCTYPE_REAL) print(real_number)
After typing pyads.PLCTYPE you can see all the dataypes available in the list.
We will read the message from TwinCAT and we will send a message back when the message from TwinCAT is not empty.
#read string message_from_twincat = plc.read_by_name('MAIN.sMessageToPython', pyads.PLCTYPE_STRING) print(message_from_twincat) #write string if len(message_from_twincat) > 1: message_to_twincat = 'Hi TwinCAT!' plc.write_by_name('MAIN.sMessageFromPython', message_to_twincat, plc_datatype=pyads.PLCTYPE_STRING)
When you are done, close the connection.
# close connection plc.close()
Here's the variable values in TwinCAT before running the python script.
After running the python script, we can see that we have received the acknowledgment from python as well as the message.
Please be aware of the security issues and take necessary security measures in your application when using this communication method.
In case you want to say thank you !)
We'd be very grateful if you could share this community with your colleagues and friends. You can also buy us a coffee to keep us fueled 😊 This is the best way to say thank you to this project and support your community.
twinControls - https://twincontrols.com/
- 17 Forums
- 267 Topics
- 942 Posts
- 5 Online
- 722 Members