Hi! I’m trying read of canbus port while script Lua. I configured parameters respect of CAN:
- CAN_D1_PROTOCOL = 10 – Scripting
- CAN_P1_DRIVER = 1 – First Driver
I have 2 scripts: Read and Write.
Script write
-- Load CAN driver, using the scripting protocol and with a buffer size of 5
local driver = CAN:get_device(5)
if driver then
local message = string.format("ID: %d se pudo obtener el dispositivo CAN para escribir",5)
print(message)
return
end
function update()
local msg = CANFrame()
msg:id( (uint32_t(1) << 31) | (uint32_t(30) << 24) | (uint32_t(1081) << 8) | uint32_t(11) )
msg:data(0,0) -- set light_id = 0
-- sending 4 bytes of data
msg:dlc(4)
-- write the frame with a 10000us timeout
driver:write_frame(msg, 10000)
return update,100
end
return update
And script read:
-- This script is an example of reading from the CAN bus
-- Load CAN driver1. The first will attach to a protocol of 10
local driver1 = CAN:get_device(5)
function show_frame(dnum, frame)
local message = string.format("CAN[%u] msg from " .. tostring(frame:id()) .. ": %i, %i, %i, %i, %i, %i, %i, %i", dnum, frame:data(0), frame:data(1), frame:data(2), frame:data(3), frame:data(4), frame:data(5), frame:data(6), frame:data(7))
print(message)
end
function update()
-- see if we got any frames
if driver1 then
frame = driver1:read_frame()
show_frame(0,frame)
end
return update, 10
end
return update()
And frame variable of script read is nil. Can someone help me? Also, I have tried examples ardupilot/libraries/AP_Scripting/examples/CAN_read.lua at master · ArduPilot/ardupilot · GitHub , But the same thing happens to me.
Thanks!!
CAN_read.lua (637 Bytes)
CAN_write.lua (590 Bytes)