diff --git a/formats/controlexpert/read_xsy.py b/formats/controlexpert/read_xsy.py index 602583b..1c93e49 100644 --- a/formats/controlexpert/read_xsy.py +++ b/formats/controlexpert/read_xsy.py @@ -1,11 +1,47 @@ -import xml.etree.ElementTree as et +import xmltodict import pandas as pd -def get_ +from iec import datatypes, ElementaryVariable, UDT -def main(inputfile): - root = et.parse(inputfile) - dataBlock = root.find('dataBlock') +def get_ddts(): + """Extract the DDT definitions from an XSY file.""" + + ddt_sources = root.findall('DDTSource') + + for ddt in ddt_sources: + df = pd.read_xml(ddt) + +def handle_element(path, item): + nodename = path[1][0] + match nodename: + case 'DDTSource': + ddtname = item['@DDTName'] + ddtcomment = item['comment'] + ddtvariables = dict() + for var in item['structure']['variables']: + ddtvariables[var['@name']] = ElementaryVariable( + name = var['@name'], + type = var['@typeName'], + address = 0, + comment = var['comment'] + ) + print(f"DDT Name: {ddtname}") + print(f"DDT Comment: {ddtcomment}") + print(f"DDT Variables: {ddtvariables}") + case 'dataBlock': + pass + return True # Continue processing + +def read_xsy(inputfile): + """Read an XSY file into a dataframe.""" + with open(inputfile, 'rb') as f: + xmltodict.parse( + f, + item_depth=2, + item_callback=handle_element + ) + + # data_block = root.find('dataBlock') if __name__ == '__main__': - main('builtins.xsy') \ No newline at end of file + read_xsy('D:\\OneDrive - Prime Controls\\Repositories\\AuDaGe\\formats\\controlexpert\\builtins.xsy') \ No newline at end of file diff --git a/iec.py b/iec.py new file mode 100644 index 0000000..d65fbe2 --- /dev/null +++ b/iec.py @@ -0,0 +1,57 @@ +""" +Definitions for IEC 61131 programming paradigms. +""" + +class DataType: + """Class for IEC 61131-3 data types.""" + def __init__( + self, + name: str, + length_bits: int, + word_length = 16, + ): + self.name = name + self.length_bits = length_bits + self.length_bytes = length_bits // 8 + self.length_words = length_bits // word_length + +datatypes = { + 'bool': DataType('bool', 1), + 'byte': DataType('byte', 8), + 'char': DataType('char', 8), + 'int': DataType('int', 16), + 'uint': DataType('uint', 16), + 'word': DataType('word', 16), + 'date': DataType('date', 32), + 'dt': DataType('dt', 32), + 'dint': DataType('dint', 32), + 'real': DataType('real', 32), + 'time': DataType('time', 32), + 'udint': DataType('udint', 32), +} + +class ElementaryVariable: + """""" + def __init__( + self, + name: str, + type: str, + address: str, + comment: str, + ): + self.name = name + self.type = type + self.address = address + self.comment = comment + +class UDT: + """""" + def __init__( + self, + name: str, + comment: str, + variables: dict, + ): + self.name = name + self.comment = comment + self.variables = variables \ No newline at end of file