Ive been working on getting the first CAN system integrated but keep getting a nil value error when trying to read the data, the first system is just thermocouples 8 of them, this uses standard CAN messages which are LSB. I based my scripting on the Link G4 CAN Script
the error is...
My full code is...
Code: Select all
--Configured for IMC Thermocouple Unit
--how frequently we poll for CAN messages
tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0
--1 for Big Endian (MSB) mode; 0 for Little Endian mode (LSB)
be_mode = 0
--add your virtual channels here
--params: <channel name>,<sample rate>, <logging precision>, <min value>, <max value>, <units label>
t1Id = addChannel("t1Exh1", 10, 1, -271, 1371, "degC")
t2Id = addChannel("t2Exh2", 10, 1, -271, 1371, "degC")
t3Id = addChannel("t3Exh3", 10, 1, -271, 1371, "degC")
t4Id = addChannel("t4Exh4", 10, 1, -271, 1371, "degC")
t5Id = addChannel("t5Exh5", 10, 1, -271, 1371, "degC")
t6Id = addChannel("t6Exh6", 10, 1, -271, 1371, "degC")
t7Id = addChannel("t7Fuel", 10, 1, -271, 1371, "degC")
t8Id = addChannel("t8Air", 10, 1, -271, 1371, "degC")
----------------------------------------
--customize here for CAN channel mapping
--format is:
--[CAN Id] = function(data) map_chan(<chan_id>, data, <CAN offset>, <CAN length>, <multiplier>,
-- <adder>, [filter])
----------------------------------------
CAN_map = {
[102] = function(data) map_chan(t1Id, data, 0, 16, 0.0251, 550)
map_chan(t2Id, data, 16, 16, 0.0251, 550)
map_chan(t3Id, data, 32, 16, 0.0251, 550)
map_chan(t4Id, data, 48, 16, 0.0251, 550) end,
[103] = function(data) map_chan(t5Id, data, 0, 16, 0.0251, 550)
map_chan(t6Id, data, 16, 16, 0.0251, 550)
map_chan(t7Id, data, 32, 16, 0.0251, 550)
map_chan(t8Id, data, 48, 16, 0.0251, 550) end
}
function onTick()
processCAN(CAN_chan)
end
--===========do not edit below===========
function processCAN(chan)
repeat
local id, e, data = rxCAN(chan)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
until id == nil
end
--Map CAN channel, little endian format
function map_chan_le(cid, data, offset, len, mult, add, filter)
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
--Map CAN channel, big endian format
function map_chan_be(cid, data, offset, len, mult, add, filter)
offset = offset + 1
local value = 0
while len > 0 do
value = (value * 256) + data[offset]
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
map_chan = (be_mode == 1) and map_chan_be or map_chan_le
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)