1

I want to use python and the PyADS library to find all the variables available of a TwinCat system/PLC. PyADS has a function called get_all_symbols which almost does what I am looking for. Here you see the variables I want to get in Python. ADS variables visible in TwinCAT

When i use the get_all_symbols() function I see all the variabels from within the MAIN. Now for the problem. I can read the integers, booleans and other primitive variables in the MAIN. Result of reading the variables

The problem is that I can't get the variabels within a STRUCT or FB in the MAIN using the get_all_symbols() function. Therefor I can obviously not read them as well. I do if I should have the full name of the variables e.g. MAIN.fb_path.i_xTest. However I can't find this variable name using with get_all_symbols().

For clarification: I want to read all the variables from the PLC and the script should still work when the code is changed or variables are added. That's why I want to use the get_all_symbols() function to get the variable names.

Is there any way I can also find the variables within a STRUCT/FB in the MAIN so I can read them as well? Thanks

import pyads
import csv

AMSNETID = '.....'

plc = pyads.Connection(AMSNETID, pyads.PORT_TC3PLC1)
plc.open()
print(f"Connected?: {plc.is_open}") #debugging statement, optional
print(f"Local Address? : {plc.get_local_address()}") #debugging statement, optional
symbols = plc.get_all_symbols()

with open('names.csv', 'w') as csvfile:
    fieldnames = ['Name', 'Value', 'Coamment']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()

    for s in symbols:
        try:
            writer.writerow({'Name': s.name, 'Value': s.read(), 'Comment': s.comment})
            print(s.name, ": ", s.read(), " // ", s.comment)

        except Exception as e:
            #print(e)
            #print(plc.get_symbol(index_group=s.index_group, index_offset=1,plc_datatype="BOOL").name)
            print(s.name, ": Failed")

plc.close()
BV_62
  • 11
  • 2
  • I answered a similar question using `C#`, the only difference is that the solution filter the variables per defined attribute, maybe you can have a look and if you find it helpful an upvote would be nice, https://stackoverflow.com/questions/76635839/find-variables-with-user-defined-attribute/76645101#76645101 – DonMiguelSanchez Aug 07 '23 at 07:19
  • the get_all_symbols should also give you all the variables inside the FBs, e.g. MAIN.fb_test.sPath as type STRING as well as MAIN.fb_test as type FB_Test. Is this not the case? – chrisbeardy Aug 07 '23 at 08:53
  • Its not the case. It does not return the variables of the FBs – Daniel Müller Navarro Aug 21 '23 at 19:02

0 Answers0