-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStructuredTextUtils.pas
173 lines (133 loc) · 4.42 KB
/
StructuredTextUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
unit StructuredTextUtils;
{$MODE Delphi}
interface
uses classes, Sysutils, Types;
{
const MaxMRegs = 32;
const MaxMWRegs = 32;
const MaxSRegs = 32;
const MaxTimers = 16;
const MaxSWRegs = 32;
}
const MaxInBits = 48;
const MaxOutBits = 48;
const MaxMemBits = 128;
const MaxSysBits = 128;
const MaxMemWords = 128;
const MaxSysWords = 128;
const MaxTimers = 32;
const MAXIQSlots = 4;
const MaxIChannels : array [0..MAXIQSlots-1] of integer
= (0,MaxInBits,0,0);
const MaxQChannels : array [0..MAXIQSlots-1] of integer
= (0,0,MaxOutBits,0);
type
TTimerType = (TOn, TOff);
type
TPLCTimerState = record
P,V: word; // Trocado para tentar contornar bug 2018 fev 207
Q: boolean;
//Mode : TTimerType;
end;
TArray_InBits = array[0..MaxInBits-1] of boolean;
TArray_OutBits = array[0..MaxOutBits-1] of boolean;
TArray_MemBits = array[0..MaxMemBits-1] of boolean;
TArray_SysBits = array[0..MaxSysBits-1] of boolean;
TArray_MemWords= array[0..MaxMemWords-1] of integer;
TArray_SysWords= array[0..MaxSysWords-1] of integer;
TArray_Timers = array[0..MaxTimers-1] of TPLCTimerState;
TPLCState = record
InBits : TArray_InBits;
OutBits : TArray_OutBits;
MemBits : TArray_MemBits;
SysBits : TArray_SysBits;
MemWords: TArray_MemWords;
SysWords: TArray_SysWords;
Timers : TArray_Timers;
end;
var
PLCState, PrevPLCState,Int_PLCState: TPLCState;
Bool2Str : array[boolean] of string;
_RE_InBits : TArray_InBits;
_FE_InBits : TArray_InBits;
_RE_OutBits: TArray_OutBits;
_FE_OutBits: TArray_OutBits;
_RE_MemBits: TArray_MemBits;
_FE_MemBits: TArray_MemBits;
_RE_SysBits: TArray_SysBits;
_FE_SysBits: TArray_SysBits;
procedure GeneratePLCVarList( vtype: char; maxItems: integer; SL: TStrings);
function BitsToDWord(const bits: array of boolean): DWord;
procedure DWordToBits(var bits: array of boolean; dw: DWord);
procedure WordsToBits(var words_in: array of word; var bits_in_out: array of boolean);
procedure BitsToWords(const bits_in : array of boolean; var words_in_out: array of word);
implementation
uses math;
//function ValidateIndex( idx: integer; var Variable: TPLCVar): boolean;
function ValidateIndex( idx: integer; vtype: char): boolean;
begin
result:=false;
// case Variable.vtype do begin
case vtype of
'i': if (idx >= 0) and (idx < MaxIQSlots) then result:=True;
'q': if (idx >= 0) and (idx < MaxIQSlots) then result:=True;
'm': if (idx >= 0) and (idx < MaxMemBits) then result:=True;
'M': if (idx >= 0) and (idx < MaxMemWords) then result:=True;
's': if (idx >= 0) and (idx < MaxSysBits ) then result:=True;
'S': if (idx >= 0) and (idx < MaxSysWords) then result:=True;
't': if (idx >= 0) and (idx < MaxTimers) then result:=True;
end;
end;
procedure GeneratePLCVarList( vtype: char; maxItems: integer; SL: TStrings);
var i,b: integer;
begin
for i:=0 to maxItems-1 do begin
if ValidateIndex(i,vtype) then begin
case vtype of
'i': for b:=0 to MaxIChannels[i]-1 do begin
SL.add(format('%%I%d.%d',[i,b]));
end;
'q': for b:=0 to MaxQChannels[i]-1 do begin
SL.add(format('%%Q%d.%d',[i,b]));
end;
'm': SL.add(format('%%M%d',[i]));
'M': SL.add(format('%%MW%d',[i]));
's': SL.add(format('%%S%d',[i]));
'S': SL.add(format('%%SW%d',[i]));
't': SL.add(format('%%TM%d',[i]));
end;
end;
end;
end;
function BitsToDWord(const bits: array of boolean): DWORD;
var i: integer;
begin
result:=0;
for i :=0 to min(high(bits),31) do begin
if bits[i] then
result:=result or DWORD(DWORD(1) shl i);
end;
end;
procedure DWordToBits(var bits: array of boolean; dw: DWord);
var i: integer;
begin
for i :=0 to min(high(bits),31) do begin
bits[i]:= (dw and (DWORD(DWORD(1) shl i))) <> 0;
end;
end;
procedure WordsToBits(var words_in: array of word; var bits_in_out: array of boolean);
var i: integer;
begin
for i := 0 to min(high(words_in),high(bits_in_out)) do begin
bits_in_out[i]:= (words_in[i div 16] and (1 shl i)) <> 0;
end;
end;
procedure BitsToWords(const bits_in : array of boolean; var words_in_out: array of word);
var i: integer;
begin
FillChar(words_in_out[0], Length(words_in_out), 0);
for i := 0 to min((high(words_in_out)+1)*16-1,high(bits_in)) do begin
words_in_out[i div 16] := words_in_out[i div 16] or (integer(bits_in[i]) shl (i mod 16));
end;
end;
end.