09.11.2024, 20:05
hi all.
I have a modbus project that LM is Master and Wago PLC Slave. Using word and bits..
I read and write data as word but i don't read and write any bit of word..
I dont use modbus mapping . I love script more easy.
I have a modbus project that LM is Master and Wago PLC Slave. Using word and bits..
I read and write data as word but i don't read and write any bit of word..
I dont use modbus mapping . I love script more easy.
Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.1.102', 502)
mb:connect()
-- -- --
value = grp.getvalue('32/1/2')
mb:writeregisters(0, value)
value = grp.getvalue('32/1/3')
mb:writeregisters(1, value)
value = mb:readregisters(2)
grp.update('32/1/4', value)
-- -- --
-- -- --
value = grp.getvalue('32/1/5')
mb:writeregisters(3, value)
value = grp.getvalue('32/1/6')
mb:writeregisters(4, value)
value = mb:readregisters(5)
grp.update('32/1/7', value)
-- -- --
-- bolean --
valuew6 = mb:readregisters(6) --read w6
-- w6.0 -- read 0. bit of w6
if (grp.getvalue('32/1/8')) then
valuew6 = valuew6 or 1 -- set » xxxx xxxx xxxx xxx(1)
else
valuew6 = valuew6 and 65534 -- set » xxxx xxxx xxxx xxx(0)
end
-- --
-- w6.1 -- read 1. bit of w6
if (grp.getvalue('32/1/9')) then
value6 = value6 or 2 -- set » xxxx xxxx xxxx xx(1)x
else
value6 = value6 and 65533 -- set » xxxx xxxx xxxx xx(0)x
end
-- --
-- w6.2 -- write 2. bit of w6
bValue = (value6 and 4) -- set » 0000 0000 0000 0(x)00
if (bValue==4) then
grp.update('32/1/10', true) -- set » xxxx xxxx xxxx x(1)xx
else
grp.update('32/1/10', false) -- set » xxxx xxxx xxxx x(0)xx
end
mb:writeregisters(6, value06)
grp.update('32/1/11', value06)
mb:close()