Skip to content

Commit 71e5714

Browse files
authored
Merge pull request #3 from loupeteam/feature/get-all-vars
Add getAllVars function
2 parents 7f1e6e4 + a1c28b9 commit 71e5714

File tree

6 files changed

+447
-2
lines changed

6 files changed

+447
-2
lines changed

src/Ar/VarTools/ANSIC.lby

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<?AutomationStudio FileVersion="4.9"?>
3-
<Library Version="0.11.4" SubType="ANSIC" xmlns="http://br-automation.co.at/AS/Library">
3+
<Library Version="0.11.5" SubType="ANSIC" xmlns="http://br-automation.co.at/AS/Library">
44
<Files>
55
<File Description="Exported data types">VarTools.typ</File>
66
<File Description="Exported constants">VarTools.var</File>
@@ -16,6 +16,9 @@
1616
<File>variableBrowser.c</File>
1717
<File Description="Force refresh of variable information in next getValue call">varRefresh.c</File>
1818
<File Description="">varToolsInternal.h</File>
19+
<File Description="Get variable all information">varGetAllVars.c</File>
20+
<File Description="Internal functions for varGetAllVars">varGetAllVarsInternal.h</File>
21+
<File Description="Internal functions for varGetAllVars">varGetAllVarsInternal.c</File>
1922
</Files>
2023
<Dependencies>
2124
<Dependency ObjectName="sys_lib" />

src/Ar/VarTools/VarTools.fun

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ FUNCTION_BLOCK varVariableWatch
7272
END_VAR
7373
END_FUNCTION_BLOCK
7474

75-
FUNCTION_BLOCK variableBrowser (*TODO: Add your comment here*) (*$GROUP=User,$CAT=User,$GROUPICON=User.png,$CATICON=User.png*)
75+
FUNCTION_BLOCK variableBrowser (* *) (*$GROUP=User,$CAT=User,$GROUPICON=User.png,$CATICON=User.png*)
7676
VAR_INPUT
7777
VariableName : STRING[VAR_STRLEN_VALUE];
7878
MemberIndex : INT;
@@ -94,3 +94,21 @@ FUNCTION_BLOCK variableBrowser (*TODO: Add your comment here*) (*$GROUP=User,$CA
9494
iMemberIndex : USINT;
9595
END_VAR
9696
END_FUNCTION_BLOCK
97+
(*Experimental*)
98+
99+
FUNCTION_BLOCK varGetAllVars (*Get all varaiables on system*)
100+
VAR_INPUT
101+
Execute : BOOL; (*Starts searching variables on rising edge*)
102+
AcknowledgeError : BOOL; (*Clears errors*)
103+
PrimitivesOnly : BOOL; (*Return primitives only *)
104+
ExpandStructs : BOOL; (*Search through structures*)
105+
CondenseArrays : BOOL; (*Return one element per array *)
106+
END_VAR
107+
VAR_OUTPUT
108+
Status : UINT; (*Status*)
109+
Variable : varVariable_typ; (*Found varaible info *)
110+
END_VAR
111+
VAR
112+
Internal : varGetAllVars_internal_typ;
113+
END_VAR
114+
END_FUNCTION_BLOCK

src/Ar/VarTools/VarTools.typ

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,21 @@ TYPE
5454
length : UDINT;
5555
dimension : UINT;
5656
END_STRUCT;
57+
varGetAllVars_internal_typ : STRUCT
58+
iVar : UDINT := 65535;
59+
iDeep : UINT;
60+
iMember : ARRAY[0..49]OF UINT;
61+
Browser : variableBrowser;
62+
Deep : ARRAY[0..49]OF STRING[VAR_STRLEN_NAME];
63+
NextVariable : BOOL;
64+
xList : PV_xList_typ;
65+
moList : slMoList;
66+
AppMoName : STRING[32];
67+
MoName : STRING[12];
68+
checkGlobal : BOOL;
69+
isGlobal : BOOL;
70+
validVar : BOOL;
71+
numUniqueVars : UDINT;
72+
uniqueVars : ARRAY[0..1999]OF STRING[VAR_STRLEN_NAME];
73+
END_STRUCT;
5774
END_TYPE

src/Ar/VarTools/varGetAllVars.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/********************************************************************
2+
* COPYRIGHT --
3+
********************************************************************
4+
* Library: VarInfo
5+
* File: varGetValue.c
6+
* Author: davidblackburn
7+
* Created: June 02, 2014
8+
********************************************************************
9+
* Implementation of library VarTools
10+
********************************************************************/
11+
12+
#include <bur/plctypes.h>
13+
#ifdef __cplusplus
14+
extern "C"
15+
{
16+
#endif
17+
18+
#include "varToolsInternal.h"
19+
#include "VarTools.h"
20+
#include "varGetAllVarsInternal.h"
21+
22+
#ifdef __cplusplus
23+
};
24+
#endif
25+
26+
27+
#include <string.h>
28+
29+
30+
//*******************************************************************
31+
// Get All variables on PLC *
32+
//*******************************************************************
33+
34+
35+
// Fix: Seems to skip some variables
36+
void varGetAllVars(varGetAllVars_typ* t)
37+
{
38+
// Check for invalid input
39+
if (t == 0) return;
40+
41+
42+
if(t->AcknowledgeError && isError(t)) {
43+
t->Status = 0;
44+
}
45+
46+
if(t->Execute && isDone(t)) {
47+
t->Internal.iVar = 0xFFFF;
48+
t->Status = 65535;
49+
t->Internal.NextVariable = 1;
50+
t->Internal.numUniqueVars = 0;
51+
}
52+
53+
t->Internal.validVar = 0;
54+
while(!t->Internal.validVar && isBusy(t)) {
55+
if(t->Internal.NextVariable) {
56+
57+
getNextVariable(t);
58+
59+
// Set rest of system bits that need to know next var
60+
t->Internal.moList.first = t->Internal.NextVariable; // moList needs to know to start back at begining
61+
setCheckGlobal(t); // Every new variable we need to check for global
62+
resetIsGlobal(t);
63+
64+
t->Internal.NextVariable = 0;
65+
66+
if(isError(t) || isDone(t)) continue;
67+
}
68+
69+
if(checkGlobal(t)) {
70+
// Check if variable has global
71+
varRefresh(&t->Variable); // TODO: move this to getNextVariable
72+
strcpy(t->Variable.name, t->Internal.xList.name); // TODO: move this to getNextVariable
73+
74+
t->Internal.validVar = checkValidVar(t);
75+
resetCheckGlobal(t);
76+
77+
// getNextVariable returns all variables without a task perfix. So you can get duplicate variables (ex multiple Configurations)
78+
// So we record all varables found so we can see if we already checked this one
79+
// Because global variables cant exist in mulitple tasks we dont need to keep track of those
80+
if(isUniqueVar(t)) {
81+
setAsUniqueVar(t);
82+
} else {
83+
t->Internal.NextVariable = 1; // Skip this var because we already say this variable
84+
continue;
85+
}
86+
87+
if(!t->Internal.validVar) {
88+
continue;
89+
}
90+
91+
setIsGlobal(t);
92+
93+
if(variableIsStructure(t) && t->ExpandStructs || variableIsArray(t) && !t->CondenseArrays) {
94+
goDownALevel(t);
95+
}
96+
else if(variableIsArray(t) && t->CondenseArrays) {
97+
// tODO: Condense array to just [i]
98+
strcat(t->Variable.name, "[");
99+
strcat(t->Variable.name, "]");
100+
}
101+
102+
if(!variableIsPrimitive(t) && t->PrimitivesOnly) {
103+
t->Internal.validVar = 0;
104+
}
105+
106+
if(atTopLevel(t))
107+
t->Internal.NextVariable = 1; // Set new varaible here becuase if something is global then its in each task as well but we just want to say global
108+
109+
}
110+
else if(atTopLevel(t)) {
111+
if(isGlobal(t)) {
112+
// We can get here for global structures or global array of strucutres
113+
t->Internal.NextVariable = 1; // Set new varaible here becuase if something is global then its in each task as well but we just want to say global
114+
continue;
115+
}
116+
117+
getNextTask(t);
118+
119+
if(isError(t)) continue;
120+
121+
if(outOfTasks(t)) {
122+
t->Internal.NextVariable = 1;
123+
continue;
124+
}
125+
126+
if(checkValidTask(t)) continue;
127+
128+
t->Internal.validVar = checkValidVar(t);
129+
130+
if(!t->Internal.validVar) continue;
131+
132+
if(variableIsStructure(t) && t->ExpandStructs || variableIsArray(t) && !t->CondenseArrays) {
133+
goDownALevel(t);
134+
}
135+
else if(variableIsArray(t) && t->CondenseArrays) {
136+
// tODO: Condense array to just [i]
137+
strcat(t->Variable.name, "[");
138+
strcat(t->Variable.name, "]");
139+
}
140+
141+
if(!variableIsPrimitive(t) && t->PrimitivesOnly) {
142+
t->Internal.validVar = 0;
143+
}
144+
145+
}
146+
else {
147+
getMembers(t);
148+
149+
while(outOfMembers(t) && !atTopLevel(t)) {
150+
goUpALevel(t);
151+
getMembers(t);
152+
}
153+
154+
if(atTopLevel(t)) continue;
155+
156+
outputMember(t);
157+
158+
incrementMember(t); // Increment member her so if we go down a level we come back to the next member
159+
160+
if(variableIsStructure(t)) {
161+
goDownALevel(t); // So next pass we will be inside the current variable
162+
}
163+
164+
t->Internal.validVar = !t->PrimitivesOnly || variableIsPrimitive(t);
165+
}
166+
}
167+
168+
if(isError(t)) {
169+
handleErrors(t);
170+
}
171+
172+
173+
if(t->Status == 0) {
174+
memset(&t->Variable, 0, sizeof(t->Variable));
175+
}
176+
177+
return;
178+
}
179+

0 commit comments

Comments
 (0)