--m10k, each register is -9999~9999 function m2b(context, mvalue) if context.regcnt > 1 then return nil end --we define single as the endian of Modbus value local endian = context.single local high, low if endian % 2 == 0 then -- 2,4 high = mvalue:byte(1) low = mvalue:byte(2) else -- 1 or 3 low = mvalue:byte(1) high = mvalue:byte(2) end if low > 9 then return nil end --invalid value return high + low / 10 end function b2m(context, bvalue) if context.regcnt > 1 then return nil end local endian = context.single if bvalue < 0 then bvalue = 0 end local high = math.floor(bvalue) if high > 255 then high = 255 end local low = math.floor((bvalue - high) * 10 + 0.5) if low > 9 then low = 9 end local mvalue if endian % 2 == 0 then -- 2 or 4 mvalue = string.char(high, low) else -- 1 or 3 mvalue = string.char(low, high) end return mvalue end return m2b, b2m