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.

Best way to decode Modbus
#1
Hi,

I have a DSE7320 power generator controller and i need to read some alerts.
I use Modbus mapper to store the alert registers to virtual objects and then decode them with script.
   
Code:
dword ='0x'.. event.datahex
myobject = grp.find(event.dstraw)

function getbit(value, nr)
  value = bit.rshift(value, nr)
  return bit.band(value, 1)
end

for i = 0, 15, 4 do
  obj=math.floor((i+1)/4)
  msg=''
  val=''
  for x = 0, 3, 1 do
    ix=i+x
      val = getbit(dword,ix)..val
  end

  bin = string.reverse(val)
    local sum = 0

    for i = 1, string.len(bin) do
      num = string.sub(bin, i,i) == "1" and 1 or 0
        sum = sum + num * math.pow(2, i-1)
    end
 
  msg=msg .. ' Bin:'..val..' Dec:'.. sum
    obj=obj..msg
  alert(myobject.name..'-'..obj)
end

   
   

Can someone please verify if this is the most eficient way or sugest something better?

Thank you in advance
George
Reply


Messages In This Thread
Best way to decode Modbus - by gtsamis - 29.06.2021, 09:30
RE: Best way to decode Modbus - by admin - 30.06.2021, 12:48

Forum Jump: