diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index 0807c406d1..45091e4e6e 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -344,14 +344,14 @@ function efi:get_backend(instance) end ---@class CAN CAN = {} --- get a CAN bus device handler first scripting driver +-- get a CAN bus device handler first scripting driver, will return nil if no driver with protocol Scripting is configured ---@param buffer_len uint32_t_ud -- buffer length 1 to 25 ----@return ScriptingCANBuffer_ud +---@return ScriptingCANBuffer_ud|nil function CAN:get_device(buffer_len) end --- get a CAN bus device handler second scripting driver +-- get a CAN bus device handler second scripting driver, will return nil if no driver with protocol Scripting2 is configured ---@param buffer_len uint32_t_ud -- buffer length 1 to 25 ----@return ScriptingCANBuffer_ud +---@return ScriptingCANBuffer_ud|nil function CAN:get_device2(buffer_len) end -- Auto generated binding diff --git a/libraries/AP_Scripting/lua_bindings.cpp b/libraries/AP_Scripting/lua_bindings.cpp index b1f7baea56..c8013b813f 100644 --- a/libraries/AP_Scripting/lua_bindings.cpp +++ b/libraries/AP_Scripting/lua_bindings.cpp @@ -682,6 +682,12 @@ int lua_get_CAN_device(lua_State *L) { } } + if (!scripting->_CAN_dev->initialized()) { + // Driver not initialized, probably because there is no can driver set to scripting + // Return nil + return 0; + } + new_ScriptingCANBuffer(L); *((ScriptingCANBuffer**)luaL_checkudata(L, -1, "ScriptingCANBuffer")) = scripting->_CAN_dev->add_buffer(buffer_len); @@ -707,6 +713,12 @@ int lua_get_CAN_device2(lua_State *L) { } } + if (!scripting->_CAN_dev2->initialized()) { + // Driver not initialized, probably because there is no can driver set to scripting 2 + // Return nil + return 0; + } + new_ScriptingCANBuffer(L); *((ScriptingCANBuffer**)luaL_checkudata(L, -1, "ScriptingCANBuffer")) = scripting->_CAN_dev2->add_buffer(buffer_len);