Lua Script to read out UART-sensor

Hi there,
I have an external sensor for my drone project. I am trying to use a Lua Script to read out the Serial Port of my Pixhawk 4. To boot the sensor the Pixhawk has to send the command @4D001F via UART to the sensor, after that it should periodically read the data from the sensor and send it to the GCS.

I have written a script, but it doesen´t work and honestly I don´t really know what i am doing, this is all new to me.
It would be huge for me, if someone with experience at Lua Scripting could look through my script and give me clues what I have to change.

Currently my script reads:

local port = serial:find_serial(0)

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

if port:available() > 0 then
gcs:send_text(0, “@4D001F”)
end

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

return spit, 1000

Thank you in advance :slight_smile:

You are probably not using serial 0 and might want to fix that first.

Thank you, unfortunately it still does not work. Do you have a suggestion on how you would write that script specifically?

Be more specific. What autopilot? What sensor? Which serial port?

You aren’t sending the boot string to the serial port. You are sending it to the GCS. More specifically, you probably aren’t even doing that because there will probably not be serial data available to satisfy the if condition surrounding it.

You are sending the “read” string whether or not data was read. Remove the second GCS message.

You are sending the GCS messages at a critical error severity. That’s probably not necessary, either.

It is a magnetometer from FieldLine Inc that I have plugged in the UART&I2CB port on the Pixhawk4 and I am using ArduCopter V4.1.5 OFFICIAL as firmware.

Would you mind editing my code from above in a way you would consider more useful?

Looks like you’re using SERIAL4.

Data sheet for the magnetometer?

https://download.fieldlineinc.com/Scalar/

here you can find a manual

Your script will never work as written. The data sheet indicates that a new line must be sent after each command. You also need to turn on channel 23 or 35 to get meaningful output. The default data rate is 250Hz (data every 4ms), but you’re only sampling it once per second. You probably need to slow the data rate to something like 50Hz to allow time for your script to run and then sample the output again.

I’m afraid I probably don’t have time to rewrite your script into a useful format, nor could I promise useful results, since I do not have that hardware at my disposal.

You’re on the right track, but the logic is very flawed right now.

Alright thanks a lot already

Do you know by any chance how I can send a command to the sensor (is there a function or something I could use)? Or is this even possible?

Should be something like:

port:write('@4D001F\n')

And to slow to 50Hz, I think it would be:

port:write('@1701F4\n')

Alright I will keep trying
Thank you so much

You may also have to do some ASCII byte value conversion. I’m not sure if the Lua serial write binding accepts string values.

Hi Yuri. I’m trying to write a serial motor driver and have hit the wall on outputting the necessary data strings which are required in hexidecimal format (eg. AB 01 4E 82 with 82 being a checksum value). If port:write only outputs integers, is there a way to output this string using the ASCII byte conversion you suggested above?

See if string.byte(‘A’) helps you understand what you might need to do. I don’t fully understand the issue you’re having.