This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Modbus variables: import massive
#20
Hi
Here is modified slave script.  I doubled the arrays for registers and added a shift.  Fill accordingly all the arrays.  registers1 will start from 4096 and registers2 will start from 8192. Make sure you add corresponding regdt for each register.  Shift can be changes by shiftR1 or shiftR2 parameter.

Code:
   if not mb then
     require('genohm-scada.eibdgm')
     require('luamodbus')
   
     -- list of coil mapping, starting from 0
     coils = { }
   
         -- registers table 1 shift starting from 1
         shiftR1 = 4095
             -- list of register mapping, starting from 4096
     registers1 = { '3/1/26', '3/1/29' }
       -- list of register data types, element count must match registers table
     regdt1 = { dt.int16, dt.int16 }
 
       
         -- registers table 2 shift starting from 1
            shiftR2 = 8191
          -- list of register mapping, starting from 8192
     registers2 = { '3/1/32', '3/1/35' }
       -- list of register data types, element count must match registers table
     regdt2 = { dt.int16, dt.int16 }  
 
 
   
   
   
     -- knx group write callback
     function knxgroupwrite(event)
       local value
   
       -- try to find matching coil
       for id, addr in ipairs(coils) do
         if event.dst == addr then
           value = knxdatatype.decode(event.datahex, dt.bool)
           mb:setcoils(id - 1, value)
         end
       end
   
       -- try to find matching register1
       for id, addr in ipairs(registers1) do
         if event.dst == addr then
           value = knxdatatype.decode(event.datahex, regdt1[ id ])
           mb:setregisters(id + shiftR1, value)
         end
       end
            -- try to find matching register2
       for id, addr in ipairs(registers2) do
         if event.dst == addr then
           value = knxdatatype.decode(event.datahex, regdt2[ id ])
           mb:setregisters(id + shiftR2, value)
         end
       end
   
     end
   
     -- coil write callback
     function mbwritecoils(coil, value)
       local addr = coils[ coil + 1 ]
       if addr then
         grp.write(addr, value, dt.bool)
       end
     end
   
     -- register write callback
     function mbwriteregisters(register, value)
       local addr1 = registers1[ register -shiftR1 ]
           local addr2 = registers2[ register -shiftR2 ]
       if addr1 then
         grp.write(addr1, value, regdt1[ register - shiftR1])
              elseif addr2 then
         grp.write(addr2, value, regdt2[ register - shiftR2])
       end
     end
 
     
     -- knx group monitor, handles group writes
     knxclient = eibdgm:new({ timeout = 0.1 })
     knxclient:sethandler('groupwrite', knxgroupwrite)
   
     -- modbus slave, listen on all interfaces and default port 502
     mb = luamodbus.tcp()
     mb:open('0.0.0.0', 502)
   
     -- setting slave id is optional
     -- mb:setslave(1)
   
     mb:setreceivetimeout(0.1)
     mb:setmapping(#coils, 0, shiftR2 + 1 + #registers2, 0)
   
     -- init coils
     for id, addr in ipairs(coils) do
       value = grp.getvalue(addr)
       mb:setcoils(id - 1, value)
     end
   
     -- init registers1
     for id, addr in ipairs(registers1) do
       value = grp.getvalue(addr)
       mb:setregisters(id + shiftR1, value)
     end
       -- init registers2
     for id, addr in ipairs(registers2) do
       value = grp.getvalue(addr)
       mb:setregisters(id + shiftR2, value)
     end
   
     -- set callbacks for coil and register write
     mb:setwritecoilcb(mbwritecoils)
     mb:setwriteregistercb(mbwriteregisters)
   end
   
   -- handle modbus and knx
   mb:handleslave()
   knxclient:step()
------------------------------
Ctrl+F5
Reply


Messages In This Thread
RE: Modbus variables: import massive - by Daniel - 04.04.2019, 07:44

Forum Jump: