Logic Machine Forum
LM master modbus, PLC slave - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: LM master modbus, PLC slave (/showthread.php?tid=5731)



LM master modbus, PLC slave - Simuzer - 09.11.2024

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 [Image: smile.png] 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()



   

   


RE: LM master modbus, PLC slave - CristianAgata - 10.11.2024

Hi,
Look at this if can help you.
https://forum.logicmachine.net/showthread.php?tid=5510&highlight=Bit+mask
BR Cristian


RE: LM master modbus, PLC slave - Simuzer - 10.11.2024

Thank you for your reply. But thats not work correctly.

When changing one bit, the other bits should not change. 


(10.11.2024, 08:13)CristianAgata Wrote: Hi,
Look at this if can help you.
https://forum.logicmachine.net/showthread.php?tid=5510&highlight=Bit+mask
BR Cristian



RE: LM master modbus, PLC slave - admin - 11.11.2024

That's exactly why you should use profiles because it's already handled there Smile

You are using logical and / or in your script. Use bit.band / bit.bor instead.