print("\r\n\r\n---- Cal Read ----") --MB.writeName("LUA_NO_WARN_TRUNCATION", 1) -- Supress truncation warnings. LJ_Product_ID = MB.R(60000, 3) print("Device type: ", LJ_Product_ID) sn = MB.RA(60028, 0, 2) print("Serial Number: ", string.format("0x%04X%04X", sn[1], sn[2])) --print("Self Diagnostic: ", string.format("0x%02X", MB.R(60090, 0))) --Read AIN15 a15 = MB.R(30, 3) print("AIN15: ", a15) -- Read the self diag reg diag = MB.R(60090, 0) print("diag: ", diag) print("\r\nReading device calibration...") local EFAdd_CalValues local EFKey_CalValues local numCalValues if LJ_Product_ID == 7 then -- Modbus addresses EFKey_CalValues = {0xA786, 0x3777} -- Juggling 32-bit values EFAdd_CalValues = 0x3C4000 numCalValues = 41 elseif LJ_Product_ID == 4 then EFKey_CalValues = {0xA786, 0x3777} -- Juggling 32-bit values EFAdd_CalValues = 0x3C4000 numCalValues = 19 else print("Unknown Device. Exiting") MB.W(6000, 1, 0) end function sleep(time_ms) LJ.IntervalConfig(7, time_ms) while( LJ.CheckInterval(7) ~= 1 )do end end function sf(a) return string.format("%0.06e", a) end MB.WA(61800, 0, 2, EFKey_CalValues) -- Set the key cal_data = {} for i=0, numCalValues-1, 1 do MB.W(61810, 1, EFAdd_CalValues+4*i) cal_data[i] = MB.R(61812, 3) --print(string.format("%0.06e", cal_data[i])) if LJ_Product_ID == 7 then if i <= 32 then -- AINs if (i % 4) == 0 and i > 0 then print(sf(cal_data[i-4]), sf(cal_data[i-3]), " ", sf(cal_data[i-2]), sf(cal_data[i-1])) end elseif i <= 36 then if (i % 2) == 0 then print(sf(cal_data[i-2]), sf(cal_data[i-1])) end else print(string.format("%0.06e", cal_data[i])) end elseif LJ_Product_ID == 4 then if i <= 8 then -- AINs if (i % 4) == 0 and i > 0 then print(sf(cal_data[i-4]), sf(cal_data[i-3]), " ", sf(cal_data[i-2]), sf(cal_data[i-1])) end elseif i <= 16 then if (i % 2) == 0 then print(sf(cal_data[i-2]), sf(cal_data[i-1])) end else print(string.format("%0.06e", cal_data[i])) end end end MB.W(61800, 1, 0) -- Clear the key MB.W(6000, 1, 0)