Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read symbols by identifier #80

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<DUT Name="D_InputVar" Id="{f8e5ff37-9e8f-41a4-a552-bdcea736ca42}">
<Declaration><![CDATA[TYPE D_InputVar :
STRUCT
INT_Var : INT; // Integer input
Real_Var : REAL; //#
END_STRUCT
END_TYPE
]]></Declaration>
</DUT>
</TcPlcObject>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.3">
<GVL Name="sampleADSGVL" Id="{03c17471-1e5b-428f-8728-646bfb4c56c5}">
<Declaration><![CDATA[{attribute 'qualified_only'}
VAR_GLOBAL
Expand All @@ -11,8 +11,8 @@ VAR_GLOBAL
eModeAHUADS AT %I* : E_ControlMode := E_ControlMode.Auto; //W

//Ambient air datapoints
fTempAmbAirADS AT %Q* : REAL;
fTemperatureAmbientMeanADS AT %Q* : REAL;
fTempAmbAirADS AT %Q* : REAL; //# test variable for ads
fTemperatureAmbientMeanADS AT %Q* : REAL; //#

//District heating station datapoints
//Temperature sensor datapoints
Expand Down Expand Up @@ -140,6 +140,9 @@ VAR_GLOBAL
//SteamHumidifier datapoints
fAHUSHSetpointADS AT %I* : REAL; //W

//Structure variables
strAdsTest : D_InputVar;

END_VAR]]></Declaration>
</GVL>
</TcPlcObject>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<GVL Name="sampleADSGVL" Id="{03c17471-1e5b-428f-8728-646bfb4c56c5}">
<Declaration><![CDATA[{attribute 'qualified_only'}
VAR_GLOBAL

//Modes
eModeMainControlModeADS AT %I* : E_ControlMode := E_ControlMode.Auto; //W
eModeDHSADS AT %I* : E_ControlMode := E_ControlMode.Auto; //W
eModeCCAADS AT %I* : E_ControlMode := E_ControlMode.Auto; //W
eModeAHUADS AT %I* : E_ControlMode := E_ControlMode.Auto; //W

//Ambient air datapoints
fTempAmbAirADS AT %Q* : REAL;
fTemperatureAmbientMeanADS AT %Q* : REAL;

//District heating station datapoints
//Temperature sensor datapoints
fDHSTempSupPrimADS AT %Q* : REAL; //#
fDHSTempRetPrimADS AT %Q* : REAL; //#
//DHS power setpoint
fDHSPowSetADS AT %I* : REAL; //# W

//Concrete core activation datapoints
//Temperature sensor datapoints
fCCATempSupPrimADS AT %Q* : REAL;
fCCATempRetPrimADS AT %Q* : REAL;
fCCATempSupADS AT %Q* : REAL; //#
fCCATempRetADS AT %Q* : REAL;
//Valve datapoints
fCCAValveActADS AT %Q* : REAL;
fCCAValveSetADS AT %I* : REAL; //W
//Pump datapoints
eCCAPumpContModeADS AT %I* : E_PumpControlMode; //# W
bCCAPumpErrorADS AT %Q* : BOOL;
bCCAPumpOperatingADS AT %Q* : BOOL;
fCCAPumpPowerADS AT %Q* : REAL;
fCCAPumpPressureADS AT %Q* : REAL;
bCCAPumpSetOnADS AT %Q* : BOOL;
fCCAPumpSetpointADS AT %I* : REAL; //W
fCCAPumpSpeedADS AT %Q* : REAL;
fCCAPumpVolFlowADS AT %Q* : REAL;

//Air handling unit datapoints
//Flap datapoints
fAHUFlapEHAActADS AT %Q* : REAL; //#
fAHUFlapEHASetADS AT %I* : REAL; //W
fAHUFlapETAActADS AT %Q* : REAL;
fAHUFlapETASetADS AT %I* : REAL; //W
fAHUFlapODAActADS AT %Q* : REAL;
fAHUFlapODASetADS AT %I* : REAL; //# W
fAHUFlapSUPActADS AT %Q* : REAL;
fAHUFlapSUPSetADS AT %I* : REAL; //# W
//Fan datapoints
fAHUFanETASpeedActADS AT %Q* : REAL;
fAHUFanETASpeedSetADS AT %I* : REAL; //W
fAHUFanETAVdpActADS AT %Q* : REAL;
fAHUFanETAVolFlowActADS AT %Q* : REAL; //#
fAHUFanSUPSpeedActADS AT %Q* : REAL;
fAHUFanSUPSpeedSetADS AT %I* : REAL; //W
fAHUFanSUPVdpActADS AT %Q* : REAL;
fAHUFanSUPVolFlowActADS AT %Q* : REAL;
//Temperature sensor datapoints
//fAHUTempAmbAirADS AT %Q* : REAL;
fAHUTempEHAADS AT %Q* : REAL;
fAHUTempETAADS AT %Q* : REAL;
fAHUTempODAADS AT %Q* : REAL;
fAHUTempSUPADS AT %Q* : REAL;
//Humidity sensor datapoints
fAHUHumEHAADS AT %Q* : REAL;
fAHUHumETAADS AT %Q* : REAL; //#
fAHUHumODAADS AT %Q* : REAL;
fAHUHumSUPADS AT %Q* : REAL;
//Frost protection sensor datapoints
bAHUFrostProtADS AT %Q* : BOOL;
//Preheater datapoints
//Temperature sensor datapoints
fAHUPHTempSupPrimADS AT %Q* : REAL;
fAHUPHTempRetPrimADS AT %Q* : REAL;
fAHUPHTempSupADS AT %Q* : REAL; //#
fAHUPHTempRetADS AT %Q* : REAL; //#
//Valve datapoints
fAHUPHValveActADS AT %Q* : REAL;
fAHUPHValveSetADS AT %I* : REAL; //W
//Pump datapoints
eAHUPHPumpContModeADS AT %I* : E_PumpControlMode; //W
bAHUPHPumpErrorADS AT %Q* : BOOL;
bAHUPHPumpOperatingADS AT %Q* : BOOL;
fAHUPHPumpPowerADS AT %Q* : REAL;
fAHUPHPumpPressureADS AT %Q* : REAL;
bAHUPHPumpSetOnADS AT %Q* : BOOL;
fAHUPHPumpSetpointADS AT %I* : REAL; //W
fAHUPHPumpSpeedADS AT %Q* : REAL;
fAHUPHPumpVolFlowADS AT %Q* : REAL;
//Heat recovery datapoints
fAHUHRBypValveAct1ADS AT %Q* : REAL;
fAHUHRBypValveAct2ADS AT %Q* : REAL;
fAHUHRBypValveSetADS AT %I* : REAL; //W
fAHUHRTempOutEstimatedADS AT %Q* : REAL;
//Cooler datapoints
//Temperature sensor datapoints
fAHUCOTempSupPrimADS AT %Q* : REAL;
fAHUCOTempRetPrimADS AT %Q* : REAL;
fAHUCOTempSupADS AT %Q* : REAL;
fAHUCOTempRetADS AT %Q* : REAL;
fAHUCOTempOutSetADS AT %I* : REAL;
fAHUCOTempOutEstimatedADS AT %Q* : REAL;
//Valve datapoints
fAHUCOValveActADS AT %Q* : REAL;
fAHUCOValveSetADS AT %I* : REAL; //W
//Pump datapoints
eAHUCOPumpContModeADS AT %I* : E_PumpControlMode; //W
bAHUCOPumpErrorADS AT %Q* : BOOL;
bAHUCOPumpOperatingADS AT %Q* : BOOL;
fAHUCOPumpPowerADS AT %Q* : REAL;
fAHUCOPumpPressureADS AT %Q* : REAL;
bAHUCOPumpSetOnADS AT %Q* : BOOL;
fAHUCOPumpSetpointADS AT %I* : REAL; //W
fAHUCOPumpSpeedADS AT %Q* : REAL;
fAHUCOPumpVolFlowADS AT %Q* : REAL;
//Reheater datapoints
//Temperature sensor datapoints
fAHURHTempSupPrimADS AT %Q* : REAL;
fAHURHTempRetPrimADS AT %Q* : REAL;
fAHURHTempSupADS AT %Q* : REAL;
fAHURHTempRetADS AT %Q* : REAL;
//Valve datapoints
fAHURHValveActADS AT %Q* : REAL;
fAHURHValveSetADS AT %I* : REAL; //W
//Pump datapoints
eAHURHPumpContModeADS AT %I* : E_PumpControlMode; //W
bAHURHPumpErrorADS AT %Q* : BOOL;
bAHURHPumpOperatingADS AT %Q* : BOOL;
fAHURHPumpPowerADS AT %Q* : REAL;
fAHURHPumpPressureADS AT %Q* : REAL; //#
bAHURHPumpSetOnADS AT %Q* : BOOL; //#
fAHURHPumpSetpointADS AT %I* : REAL; //# W
fAHURHPumpSpeedADS AT %Q* : REAL;
fAHURHPumpVolFlowADS AT %Q* : REAL;
//SteamHumidifier datapoints
fAHUSHSetpointADS AT %I* : REAL; //W

END_VAR]]></Declaration>
</GVL>
</TcPlcObject>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<LibraryReferences>{e13035b6-8b35-4c2d-bceb-88fea9accf4e}</LibraryReferences>
</PropertyGroup>
<ItemGroup>
<Compile Include="DUTs\D_InputVar.TcDUT">
<SubType>Code</SubType>
</Compile>
<Compile Include="DUTs\E_ControlMode.TcDUT">
<SubType>Code</SubType>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<TcSmProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.beckhoff.com/schemas/2012/07/TcSmProject" TcSmVersion="1.0" TcVersion="3.1.4023.119">
<TcSmProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.beckhoff.com/schemas/2012/07/TcSmProject" TcSmVersion="1.0" TcVersion="3.1.4024.7">
<DataTypes>
<DataType>
<Name GUID="{A77DD582-54D2-00CF-0D27-2A53472DBBA9}" AutoDeleteType="true">E_ControlMode</Name>
Expand Down Expand Up @@ -224,11 +224,12 @@
<Name>PlcTask Outputs</Name>
<Var>
<Name>sampleADSGVL.fTempAmbAirADS</Name>
<Comment><![CDATA[Ambient air datapoints]]></Comment>
<Comment><![CDATA[# test variable for ads]]></Comment>
<Type>REAL</Type>
</Var>
<Var>
<Name>sampleADSGVL.fTemperatureAmbientMeanADS</Name>
<Comment><![CDATA[#]]></Comment>
<Type>REAL</Type>
</Var>
<Var>
Expand Down
3 changes: 2 additions & 1 deletion Interfaces/Python/MQTT-ADS-interface/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
self.client.publish(topic, message, qos=1)
print("published: " + topic + message)
3 changes: 2 additions & 1 deletion Interfaces/Python/MQTT-ADS-interface/mqtt_ads_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
86 changes: 84 additions & 2 deletions Interfaces/Python/MQTT-ADS-interface/parsing_and_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -114,4 +196,4 @@ def parseJSONsubscribe(payload):
return temp['name'], temp['value']

if __name__ == "__main__":
pub, sub = getADSVariables()
pub, sub = getADSVariables()