Reputation: 21
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()
Upvotes: 2
Views: 1070
Reputation: 1097
It seems STRUCT
and FUNCTION_BLOCK
properties are simply not registered using pyads.Connection.get_all_symbols()
. Finding all symbols is the result of a single read operation to the magic address ADSIGRP_SYM_UPLOADINFO2
. In the results will be entries for each instance of the objects, with their size and type (as string, e.g. ST_MyStruct
) correct, but not their child properties. I don't think ADS simply supports this.
When using Global Variable Lists the entries are considered standalone variables and will each show up correctly in get_all_symbols()
(until more complex objects are encountered again).
Upvotes: 0