Lua script failing to run when readstring() is called

So, im trying to read the data from a usb device into my pixhawk4 using a pico as host (this part works fine), the part thats giving me some trouble is in the Lua script. Its supposed to read from a serial port and write to gcs so i can see it (and later on be used inside the code), but on mission planner i get the error “attempt to call a nil value (method readstring)”
my code is currently looking like this:

local port = serial:find_serial(0)

port:begin(115200)
port:set_flow_control(0)

function spit ()
local avail = port:available():toint()
if avail > 0 then
gcs:send_text(0, “Hello from serial_test.lua #bytes:” .. avail)
local read = port:readstring(avail)
gcs:send_text(0, "RX: " .. read)
end
return spit, 1000
end

return spit, 1000

Tbh i have literally no clue on what im doing wrong, so thanks in advance for any help!

Edit: I’m using serial port 4, which is already configured as scripting in mission planner

What version of Ardupilot are you using? IIRC readstring is a fairly new addition to lua API.

I’m using a custom version of ardupilot with a few modifications, but the core version is arduplane 4.5

IIRC it is a part of 4.6 release.

I just tested it with a fresh deployment using the ArduPlane 4.6 release, and it worked perfectly, thanks for the help! By the way, do you know if there’s a way to achieve similar results with the 4.5 release?

You need to bring appropriate commits into your branch.