-
Notifications
You must be signed in to change notification settings - Fork 4
/
sharedmodbus.lua
308 lines (298 loc) · 8.79 KB
/
sharedmodbus.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
require('shared')
print('Check shared devices')
if shared_devices ~= nil then
for gateindex, gate in pairs(shared_devices) do
print('Check shared_devices['..gateindex..']')
if gate.DO ~= nil then
for idx = 1, #gate.DO, 1 do
if gate.DO[idx] == nil then
print('ERROR - DO['..idx..'] is null')
gate.DO[idx]:get_state() --trigger error
end
end
end
if gate.DI ~= nil then
for idx = 1, #gate.DI, 1 do
if gate.DI[idx] == nil then
print('ERROR - DI['..idx..'] is null')
gate.DI[idx]:get_state() --trigger error
end
end
end
if gate.AI ~= nil then
for idx = 1, #gate.AI, 1 do
if gate.AI[idx] == nil then
print('ERROR - AI['..idx..'] is null')
gate.AI[idx]:get_value() --trigger error
end
end
end
if gate.AO ~= nil then
for idx = 1, #gate.AO, 1 do
if gate.AO[idx] == nil then
print('ERROR - AO['..idx..'] is null')
gate.AO[idx]:get_value() --trigger error
end
end
end
end
end
if remote_gateways ~= nil then
for gateindex, gate in pairs(remote_gateways) do
print('Check remote_gateways['..gateindex..']')
if gate.DO ~= nil then
for idx = 1, #gate.DO, 1 do
if gate.DO[idx] == nil then
print('ERROR - DO['..idx..'] is null')
gate.DO[idx]:get_state() --trigger error
end
end
end
if gate.DI ~= nil then
for idx = 1, #gate.DI, 1 do
if gate.DI[idx] == nil then
print('ERROR - DI['..idx..'] is null')
gate.DI[idx]:get_state() --trigger error
end
end
end
if gate.AI ~= nil then
for idx = 1, #gate.AI, 1 do
if gate.AI[idx] == nil then
print('ERROR - AI['..idx..'] is null')
gate.AI[idx]:get_value() --trigger error
end
end
end
if gate.AO ~= nil then
for idx = 1, #gate.AO, 1 do
if gate.AO[idx] == nil then
print('ERROR - AO['..idx..'] is null')
gate.AO[idx]:get_value() --trigger error
end
end
end
end
end
function init_gateways( mgates )
local i = 1
for gatename, gate in pairs(mgates) do
if gate.enabled then
if G_PAC_INFO():is_emulator() == false then
gate.mclient = modbus_client( i, gate.ip, gate.port, gate.timeout )
else
local ipl = '127.0.0.1'
if gate.ipemulator ~= nil then
ipl = gate.ipemulator
end
gate.mclient = modbus_client( i, ipl, gate.port, gate.timeout )
end
gate.mclient:set_station( gate.station )
gate.step = 0
gate.timer = get_millisec()
gate.dicnt = 0
gate.docnt = 0
gate.aicnt = 0
gate.aocnt = 0
gate.error = false
gate.lastsuccessquery = get_millisec()
if gate.DO ~= nil then
gate.docnt = #gate.DO
end
if gate.DI ~= nil then
gate.dicnt = #gate.DI
end
if gate.AO ~= nil then
gate.aocnt = #gate.AO
end
if gate.AI ~= nil then
gate.aicnt = #gate.AI
end
i = i + 1
end
end
end
function eval_gateways( mgates )
local mb_reg, mb_reg2, mb_new_state
for gatename, gate in pairs(mgates) do
if gate.enabled then
if G_PAC_INFO():is_emulator() == false or gate.emulation then
if gate.step == 0 then
if gate.dicnt + gate.aicnt > 0 then
if gate.mclient:async_read_holding_registers(0, gate.dicnt + 2 * gate.aicnt) == 1 then
for mb_reg = 0, gate.dicnt - 1, 1 do
mb_new_state = gate.mclient:get_int2(mb_reg)
if mb_new_state == 0 then
gate.DI[mb_reg + 1]:off()
else
gate.DI[mb_reg + 1]:on()
end
end
for mb_reg2 = 0, gate.aicnt - 1, 1 do
gate.AI[mb_reg2 + 1]:set_value(gate.mclient:get_float(gate.dicnt + mb_reg2 *2))
end
gate.step = 1
gate.error = false
gate.lastsuccessquery = get_millisec()
else
if get_delta_millisec(gate.lastsuccessquery) > 7000 then
gate.error = true
end
end
else
gate.step = 1
gate.error = false
gate.lastsuccessquery = get_millisec()
end
elseif gate.step == 1 then
if gate.docnt + gate.aocnt > 0 then
for mb_reg = 0, gate.docnt - 1, 1 do
gate.mclient:set_int2(mb_reg, gate.DO[mb_reg + 1]:get_state())
end
for mb_reg2 = 0, gate.aocnt - 1, 1 do
gate.mclient:set_float(gate.docnt + mb_reg2 * 2, gate.AO[mb_reg2 + 1]:get_value())
end
if gate.mclient:async_write_multiply_registers(0,gate.docnt + 2 * gate.aocnt) == 1 then
gate.step = 2
gate.error = false
gate.lastsuccessquery = get_millisec()
gate.timer = get_millisec()
else
if get_delta_millisec(gate.lastsuccessquery) > 7000 then
gate.error = true
end
end
else
gate.step = 2
gate.error = false
gate.lastsuccessquery = get_millisec()
gate.timer = get_millisec()
end
elseif gate.step == 2 then
if get_delta_millisec(gate.timer) > gate.cycletime then
gate.step = 0
gate.error = false
gate.lastsuccessquery = get_millisec()
end
end
end
end
end
end
function read_hr( n, start_idx, count )
local res = {}
if shared_devices[n] ~= nil then
local SDAI = shared_devices[n].AI
local SDDI = shared_devices[n].DI
local dicnt = 0
local coil_n = 0
if SDDI ~= nil then
dicnt = #SDDI
end
local aicnt = 0
if SDAI ~= nil then
aicnt = #SDAI
end
for coil_n = start_idx, start_idx + count, 1 do
res[ #res + 1 ] = 0 --Добавляем новый элемент.
res[ #res + 1 ] = 0
if coil_n < dicnt then
res[#res - 1] = 1
res[#res] = SDDI[coil_n + 1]:get_state()
elseif coil_n >= dicnt and coil_n < dicnt + aicnt * 2 then
if (coil_n - dicnt) % 2 == 0 then
res[#res - 1] = 2
res[#res] = SDAI[(coil_n - dicnt) / 2 + 1]:get_value()
end
end
end
end
return res
end
function write_hr( n, start_idx, count, buff )
if shared_devices[n] ~= nil then
local SDAO = shared_devices[n].AO
local SDDO = shared_devices[n].DO
local docnt = 0
local coil_n = 0
if SDDO ~= nil then
docnt = #SDDO
end
local aocnt = 0
if SDAO ~= nil then
aocnt = #SDAO
end
if docnt > 0 then
for coil_n = 0, docnt - 1, 1 do
SDDO[coil_n + 1]:set_state(ModbusServ:UnpackInt16(buff, coil_n * 2))
end
end
if aocnt > 0 then
for coil_n = 0, aocnt - 1, 1 do
SDAO[coil_n + 1]:set_value(ModbusServ:UnpackFloat(buff, docnt * 2 + coil_n * 4))
end
end
end
end
--Для обмена между проектами парами DI-DO AI-AO
function read_hr2( n, start_idx, count )
local res = {}
if shared_devices[n] ~= nil then
local SDAO = shared_devices[n].AO
local SDDO = shared_devices[n].DO
local docnt = 0
local coil_n = 0
if SDDO ~= nil then
docnt = #SDDO
end
local aocnt = 0
if SDAO ~= nil then
aocnt = #SDAO
end
for coil_n = start_idx, start_idx + count, 1 do
res[ #res + 1 ] = 0 --Добавляем новый элемент.
res[ #res + 1 ] = 0
if coil_n < docnt then
res[#res - 1] = 1
res[#res] = SDDO[coil_n + 1]:get_state()
elseif coil_n >= docnt and coil_n < docnt + aocnt * 2 then
if (coil_n - docnt) % 2 == 0 then
res[#res - 1] = 2
res[#res] = SDAO[(coil_n - docnt) / 2 + 1]:get_value()
end
end
end
end
return res
end
function write_hr2( n, start_idx, count, buff )
local mb_new_state
if shared_devices[n] ~= nil then
local SDAI = shared_devices[n].AI
local SDDI = shared_devices[n].DI
local dicnt = 0
local coil_n = 0
if SDDI ~= nil then
dicnt = #SDDI
end
local aicnt = 0
if SDAI ~= nil then
aicnt = #SDAI
end
if dicnt > 0 then
for coil_n = 0, dicnt - 1, 1 do
mb_new_state = ModbusServ:UnpackInt16(buff, coil_n * 2)
if mb_new_state == 0 then
SDDI[coil_n + 1]:off()
else
SDDI[coil_n + 1]:on()
end
end
end
if aicnt > 0 then
for coil_n = 0, aicnt - 1, 1 do
SDAI[coil_n + 1]:set_value(ModbusServ:UnpackFloat(buff, dicnt * 2 + coil_n * 4))
end
end
end
end