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, reading registers with strings
#5
More universal solution:
Code:
require('luamodbus')
mb = luamodbus.tcp()

mb:open('192.168.2.1', 502)
mb:connect()

function readstring(mb, address, length)
  local data = { mb:readregisters(address, length) }
  local bytes = {}

  for _, reg in ipairs(data) do
    if type(reg) == 'number' then
      -- high 8 bits
      local b1 = bit.band(bit.rshift(reg, 8), 0xFF)
      if b1 > 0 then
        bytes[ #bytes + 1 ] = b1
      end

      -- low 8 bits
      local b2 = bit.band(reg, 0xFF)
      if b2 > 0 then
        bytes[ #bytes + 1 ] = b2
      end
    end
  end

  -- convert table of byte values to string
  return string.char(unpack(bytes))
end

result = readstring(mb, 71, 16)
log(result)

mb:close()
Reply


Messages In This Thread
RE: Modbus, reading registers with strings - by admin - 22.02.2018, 07:38

Forum Jump: