Join us today!

Counting the number...
 
Notifications
Clear all

Counting the number of items in a STRUCT in TwinCAT 3 using Python

3 Posts
2 Users
2 Reactions
460 Views
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

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
installpyads

 

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. 

struct

 

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. 

localnetid

 

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: 

 

 

Reply
2 Replies
benhar-dev
Posts: 17
(@benhar-dev)
Eminent Member
Joined: 2 years ago

Hi @twinControls, this is an example of the same in Structured Text.  

https://github.com/benhar-dev/tc3-count-structure-elements

Reply
1 Reply
twinControls
Admin
(@twincontrols)
Joined: 2 years ago

Member
Posts: 114

Hi @benhar-dev, very smart solution! I was wondering if this could be done using the json function blocks. Thanks a lot for sharing!

Reply
Share: