[SOLVED] Issue with Reading from CAN

I want to read in sensor data through a CAN channel and I’m trying to use lua scripting to do so but it’s not working. Lua scripts themselves are theoretically fine and do the other things that I want it to do but it never reads the CAN message. More specifically, the frame is always null.

Any advice? or ideas?

Note that CAN is the only one I have available so please don’t suggest I change to a different protocol - it’s not an option.

Thank you

Need a lot more info in order to help. What devices? Copy of the script (or at least the relevant calls)?

I have a few different devices that I’ve been trying to communicate with - including an Arduino and a CANmod.temp (from CSS electronics). They communicate with other things (unrelated to Ardupilot) but not with the Pixhawk Orange Cube.

As for the script, I’ve tried a few different things but to keep things simpler, the example script given CAN_read.lua doesn’t work. For some reason, I can’t attach a file but I’ve just copied it below. The outcome of the script will show a ‘Hello World!’ only.

– This script is an example of reading from the CAN bus
– Load CAN driver, using the scripting protocol and with a buffer size of 5
local driver = CAN.get_device(5)

function update()

– Read a message from the buffer
frame = driver:read_frame()
gcs:send_text(6, ‘Hello World!’)

if frame then
– note that we have to be careful to keep the ID as a uint32_t userdata to retain precision
gcs:send_text(6, ‘Hello World2!’)
gcs:send_text(0,string.format("CAN msg from " … tostring(frame:id()) … “: %i, %i, %i, %i, %i, %i, %i, %i”, frame:data(0), frame:data(1), frame:data(2), frame:data(3), frame:data(4), frame:data(5), frame:data(6), frame:data(7)))

return update, 100


return update()

Do you have the CAN parameters and bitrate set correctly?

I’ve found the issue - there were two bitrates and I needed to make them the same for it to work. Thank you so much!

1 Like