diff --git a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/DUTs/D_InputVar.TcDUT b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/DUTs/D_InputVar.TcDUT new file mode 100644 index 0000000..9084923 --- /dev/null +++ b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/DUTs/D_InputVar.TcDUT @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL.TcGVL b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL.TcGVL index f89aa63..8e04d71 100644 --- a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL.TcGVL +++ b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL.TcGVL @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL_markedVariables.TcGVL b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL_markedVariables.TcGVL new file mode 100644 index 0000000..63e8f55 --- /dev/null +++ b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL_markedVariables.TcGVL @@ -0,0 +1,145 @@ + + + + + + diff --git a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/MQTT_Example.plcproj b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/MQTT_Example.plcproj index 3ab7895..93ace93 100644 --- a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/MQTT_Example.plcproj +++ b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/MQTT_Example.plcproj @@ -17,6 +17,9 @@ {e13035b6-8b35-4c2d-bceb-88fea9accf4e} + + Code + Code diff --git a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/TwinCAT Project_MQTT_Example.tsproj b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/TwinCAT Project_MQTT_Example.tsproj index f264867..3f3b5be 100644 --- a/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/TwinCAT Project_MQTT_Example.tsproj +++ b/Interfaces/Python/MQTT-ADS-interface/TwinCAT Project_MQTT_Example/TwinCAT Project1/TwinCAT Project_MQTT_Example.tsproj @@ -1,5 +1,5 @@ - + E_ControlMode @@ -224,11 +224,12 @@ PlcTask Outputs sampleADSGVL.fTempAmbAirADS - + REAL sampleADSGVL.fTemperatureAmbientMeanADS + REAL diff --git a/Interfaces/Python/MQTT-ADS-interface/mqtt.py b/Interfaces/Python/MQTT-ADS-interface/mqtt.py index ee75e34..6c79ca1 100644 --- a/Interfaces/Python/MQTT-ADS-interface/mqtt.py +++ b/Interfaces/Python/MQTT-ADS-interface/mqtt.py @@ -131,4 +131,5 @@ def on_publish(self, client, userdata, msg_id): pass def publish(self, message, topic, client=None): - self.client.publish(topic, message, qos=1) \ No newline at end of file + self.client.publish(topic, message, qos=1) + print("published: " + topic + message) \ No newline at end of file diff --git a/Interfaces/Python/MQTT-ADS-interface/mqtt_ads_interface.py b/Interfaces/Python/MQTT-ADS-interface/mqtt_ads_interface.py index f4a9328..27ff6b2 100644 --- a/Interfaces/Python/MQTT-ADS-interface/mqtt_ads_interface.py +++ b/Interfaces/Python/MQTT-ADS-interface/mqtt_ads_interface.py @@ -380,7 +380,8 @@ def load_config(f='config.ini'): # Variables and Parsing # Get ADS variables from variable list # pub, sub = parsing_and_assignment.getADSVariables(file='TwinCAT Project1/TwinCAT Project1/Untitled1/GVLs/sampleADSGVL.TcGVL') - pub, sub = parsing_and_assignment.getADSvarsFromSymbols(mqtt_ads.ads) + #pub, sub = parsing_and_assignment.getADSvarsFromSymbols(mqtt_ads.ads) + pub, sub = parsing_and_assignment.getMarkedADSvarsFromSymbols(mqtt_ads.ads) # Connect MQTT try: if load_conf == True: diff --git a/Interfaces/Python/MQTT-ADS-interface/parsing_and_assignment.py b/Interfaces/Python/MQTT-ADS-interface/parsing_and_assignment.py index d620dcb..79310ac 100644 --- a/Interfaces/Python/MQTT-ADS-interface/parsing_and_assignment.py +++ b/Interfaces/Python/MQTT-ADS-interface/parsing_and_assignment.py @@ -10,7 +10,7 @@ import json #%% -def getADSVariables(file="TwinCAT Project1/TwinCAT Project1/Untitled1/GVLs/sampleADSGVL.TcGVL"): +def getADSVariables(file="TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL.TcGVL"): startphrase = 'VAR_GLOBAL' endphrase = 'END_VAR' start = 0 @@ -55,11 +55,62 @@ def getADSVariables(file="TwinCAT Project1/TwinCAT Project1/Untitled1/GVLs/sampl print('Done gathering data points. \n') return publish, subscribe +def getMarkedADSVariables(file="TwinCAT Project_MQTT_Example/TwinCAT Project1/MQTT_Example/GVLs/sampleADSGVL_markedVariables.TcGVL"): + startphrase = 'VAR_GLOBAL' + endphrase = 'END_VAR' + start = 0 + finish = 0 + publish = {} + subscribe = {} + + # To mark a variable put the comment //# in the respective line of the *TcGVL file + # Put further comments after the mark with at least one space, like this: //W -> //# W + + with open(file) as f: + for num, line in enumerate(f, 1): + if startphrase in line: + start = num + 1 + if endphrase in line: + finish = num - 1 + if start > 0 and num >= start: + if finish > 0 and num > finish: + break + s = line.split() + if s: # skip empty lines + # skip all comment lines + if re.compile("//").findall(s[0]): + continue + else: + continue + + # the mark is always at position 5 in s, counting from 0 + if (len(s) >= 6) and (s[5] == "//#"): + if "%I*" in s[1] or "%I*" in s[2] or "%I*" in s[3]: + if re.compile("REAL").findall(s[3]) or re.compile("REAL").findall(s[4]): + temptype = pyads.PLCTYPE_REAL + elif re.compile("BOOL").findall(s[3]) or re.compile("BOOL").findall(s[4]): + temptype = pyads.PLCTYPE_BOOL + else: + temptype = pyads.PLCTYPE_INT + subscribe[s[0]] = temptype + else: + if re.compile("REAL").findall(s[3]) or re.compile("REAL").findall(s[4]): + temptype = pyads.PLCTYPE_REAL + elif re.compile("BOOL").findall(s[3]) or re.compile("BOOL").findall(s[4]): + temptype = pyads.PLCTYPE_BOOL + else: + temptype = pyads.PLCTYPE_INT + publish[s[0]] = temptype + + print('Done gathering data points. \n') + return publish, subscribe + def getADSvarsFromSymbols(ads): publish = {} subscribe = {} try: ads_list = ads.plc.get_all_symbols() + #ads_list = ads.get_all_symbols() for i in ads_list: if i.symbol_type == 'BOOL': temptype = pyads.PLCTYPE_BOOL @@ -80,6 +131,37 @@ def getADSvarsFromSymbols(ads): print('There seems to be an issue with the ads connection. Could not fetch data points from plc device via ads.') return publish, subscribe +def getMarkedADSvarsFromSymbols(ads): + publish = {} + subscribe = {} + + # Desired variables have to be marked with a //# comment (add further comments with a space after '#') + try: + symbol_list = ads.plc.get_all_symbols() + for symbol in symbol_list: + sym_comment = symbol.comment + comment_words = sym_comment.split() + if (len(comment_words) >= 1) and (comment_words[0] == '#'): + if symbol.symbol_type == 'BOOL': + temptype = pyads.PLCTYPE_BOOL + elif symbol.symbol_type == 'REAL': + temptype = pyads.PLCTYPE_REAL + else: + temptype = pyads.PLCTYPE_INT + if "OutData" in symbol.name: + #skip + continue + if symbol.index_group == 61472: + subscribe[symbol.name] = {'type': temptype, 'handle': ads.plc.get_handle(symbol.name)} + if symbol.index_group == 61488: + publish[symbol.name] = {'type': temptype, 'handle': ads.plc.get_handle(symbol.name)} + print('Done gathering data points. \n') + except: + print('There seems to be an issue with the ads connection. Could not fetch data points from plc device via ads.') + + return publish, subscribe + + def getRawADSVarListFromSymbols(ads): var_list = {} try: @@ -114,4 +196,4 @@ def parseJSONsubscribe(payload): return temp['name'], temp['value'] if __name__ == "__main__": - pub, sub = getADSVariables() \ No newline at end of file + pub, sub = getADSVariables()