Send message using Lua between Pixhawk4 and sensor


I am trying to use a external magnetometer (Index of /Scalar) with my Pixhawk4. I have connected it to the UART&I2CB port and I am working on a Lua Script to boot up the sensor and read out the data. So far I have the following script:

local port = serial:find_serial(0)



function spit ()
if port:available() > 0 then
read = port:read()
gcs:send_text(0, read)
gcs:send_text(0, “test”)
return spit, 1000

return spit, 1000

The command @4D001F should boot up the sensor and the second one should set it to a certain baudrate, but that doesent work.

Mission Planner gives me the following messages:
02.05.2022 12:19:49 : x a nil value (local ‘port’)
02.05.2022 12:19:49 : Lua: ./scripts/serial_test3.lua:5: attempt to inde

If anyone has experience with Lua scripting in connection to MissionPlanner I would be grateful for any input you could provide :slight_smile:

Why are you double posting this? This is a duplicate of Lua Script to read out UART-sensor - #10 by Yuri_Rage

Because I edited some of it and thought someone might stumble over a fresh post.