Join us today!

Connecting to TwinC...
 
Notifications
Clear all

Connecting to TwinCAT using Python

1 Posts
1 Users
1 Reactions
2,515 Views
twinControls
Posts: 115
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

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
installpyads

 

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. 

localnetid

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))
deviceinfo

 

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. 

datatypes

 

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. 

valuesbefore

After running the python script, we can see that we have received the acknowledgment from python as well as the message. 

valuesafter

Please be aware of the security issues and take necessary security measures in your application when using this communication method. 

 

Reply
Share: