Join us today!
Counting the number of items in a STRUCT in TwinCAT 3 using Python
In this tutorial, I will explain how to count the number of elements in a STRUCT in TwinCAT 3 using Python. This tutorial is based on the question asked by @IOTea.
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 .py file and name it as count_struct_elements.py. 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
If we check any of the Struct DUT file created in TwinCAT 3, we can see that structure elements are located between the STRUCT and END_STRUCT keywords.
Now we will write a method in python to count the lines between STRUCT and END_STRUCT keywords and we will also check for the blank lines and won't include them if there's any.
def countElementsInStruct(filename): count = 0 first = True with open(filename) as fp: for line in fp: if 'STRUCT' in line: if first: count = 0 first = False else: return count else: if line.strip(): # avoid the blank lines count += 1
This method requires the file path input and returns the number of elements in the struct dut file.
Now we will add the ADS communication logic in python. You would need the note the AMS NET ID of your target system to open the connection.
Double click on 'Routes' in TwinCAT and go to NETID Management and note your local net id.
import pyads # connect to the PLC plc = pyads.Connection('169.254.24.120.1.1', pyads.PORT_TC3PLC1) # open the connection plc.open() # read the device name and the version device_name, version = plc.read_device_info() #read the STRUCT file path sDUTFilePath = plc.read_by_name('MAIN.sDUTFilePath', pyads.PLCTYPE_STRING) #count elements method element_count_in_struct = countElementsInStruct(sDUTFilePath) #write the struct elements count to the variable in TwinCAT 3 plc.write_by_name('MAIN.nElementCountInStruct', element_count_in_struct) # close connection plc.close()
We are opening the ADS connection and getting the struct file path info from TwinCAT, then passing this information to the countElementsStruct method. Then finally we are writing the count info to the MAIN.nElementCountInStruct variable in TwinCAT.
We are done with the python program and we will add the logic in TwinCAT to run this python file. We are going to use the NT_Process function block to execute the python command. You'd need to add the Tc2_Utilities library into the project to use this function block.
Declare the variables:
//struct file path info to python sDUTFilePath :T_MaxString := 'C:/Users/Administrator/Desktop/variableArrayLength/VariableLenghtArray/VariableLenghtArray/PLC/DUTs/ST_Generic.TcDUT'; //elements count from python nElementCountInStruct : INT; bStartWindowsCommand : BOOL; fbStartWindowsCommand : NT_StartProcess; // Start Windows Command sPath : T_MaxString := 'C:\Windows\System32\cmd.exe' ; sDirName : T_MaxString := 'C:\Windows\System32' ; sCommandLine : T_MaxString := '/C py %HOMEPATH%\Desktop\count\count_struct_elements.py'; //pyhton program file path
//========================================================= //run the pyhton file using windows command fbStartWindowsCommand( NETID:= '', PATHSTR:= sPath, DIRNAME:= sDirName, COMNDLINE:= sCommandLine, START:= bStartWindowsCommand, TMOUT:= T#5S, BUSY=> , ERR=> , ERRID=> ); //=========================================================
We can see the application result below:
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/
Hi @twinControls, this is an example of the same in Structured Text.
My blog: codingbytes
My code: https://github.com/benhar-dev
Hi @benhar-dev, very smart solution! I was wondering if this could be done using the json function blocks. Thanks a lot for sharing!
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/
-
Connecting to TwinCAT using Python
2 years ago
-
Enabling ADS Server for the EtherCAT Device
2 years ago
- 17 Forums
- 268 Topics
- 925 Posts
- 3 Online
- 738 Members