129
7.4.
Usage example (Modbus RTU)
Task: read two parameters (3-phase system voltage, 3-phase system current) from Modbus
Multimeter 32-bit registers and store the data in the KNX group addresses. Make sure to connect
LM2 with Modbus device correctly, RS485 A with -, RS485 B with +.
1.
-- initmodbus on first script execution
2.
ifnot
mb
then
3.
require
(
'luamodbus'
)
4.
mb
=
luamodbus.rtu
()
5.
mb:open
(
'/dev/ttyS2'
,
9600
,
'E'
,
8
,
1
,
'H'
)
6.
mb:connect
()
7.
end
8.
9.
-- sets slave ID to read/write data from/to
10.
mb:setslave
(
20
)
11.
12.
-- read 3-phase system voltage from 32-bit register
13.
r1, r2
=
mb:readregisters
(
0x1000, 2
)
14.
result
=
bit.lshift
(
r1, 16
)
+ r2
15.
grp.
write
(
'5/5/1'
,result
)
16.
17.
-- read 3-phase system current from 32-bit register
18.
r1, r2
=
mb:readregisters
(
0x100E, 2
)
19.
result
=
bit.lshift
(
r1, 16
)
+ r2
20.
grp.
write
(
'5/5/2'
,result
)
Some Modbus devices keep enocded values in registers, you need to encode them first from
HEX to use in the further scripts.For example,
value = 0x0cba
after executing the below
commands will give temperature equal to
24.2
1.
hex
=
lmcore.inttohex
(
value,
2
)
2.
temp
=
knxdatatype.decode
(
hex, dt.float16
)
Here is an example of function which is doing
byte shift
:
1.
-- get single bit from a numeric value
2.
function
getbit
(
value, bnum
)
3.
value
=
tonumber
(
value
)
or
0
4.
value
=
bit.rshift
(
value, bnum
)
5.
return
bit.band
(
value, 1
)
6.
end
7.
8.
9.
getbit
(
value,
0
)
-- first bit, and so on
Summary of Contents for LogicMachine3 Re:actor
Page 10: ...10 Terminal connection schemes KNX TP...
Page 12: ...12 24V power supply...
Page 13: ...13 Analog inputs e g reed contact...
Page 14: ...14 Analog inputs 0 10V...
Page 15: ...15 Digital output...
Page 16: ...16 Resistive sensor input...
Page 17: ...17 Analog output...
Page 26: ...26 Make sure that bus status is Online press button in ETS...
Page 35: ...35 o Then minimize side bar by pressing on left arrow icon to make the map more visible...
Page 83: ...83 66 if err then 67 alert FTP upload failed s err 68 end...
Page 107: ...107...
Page 108: ...108 1 14 Help Documentation for scripting syntaxes is displayed in Help tab...
Page 126: ...126 Datapoints can be shown also in a way of table which can be later exported as CSV file...
Page 141: ...141...