How to read brodcast messages in python using multithread

I want to read the messages in my python script that are printed in QGCS (Screenshot attached) in the red.


I have connected my PC with the Pixhawk using ttl2usb convertor. I assume these are broadcasting messages, am I right here?
How should I code my program to read it?

Those are STATUSTEXT type messages but they are not send by default when you boot the autopilot. You have to request them first, in Python it would look something like this:

def request_messages(self):
    """ Requesting MAVLink messages"""
    self.vehicle.mav.command_long_send(
        self.vehicle.target_system,
        self.vehicle.target_component,
        mavutil.mavlink.MAV_CMD_REQUEST_MESSAGE,
        253,
        0, 0, 0, 0, 0, 0, 0
    )

where 253 refers to the STATUSTEXT message.

These messages also contain a severity field and the red ones in your screenshot are marked as critical (2).

Here is my code:

from pymavlink import mavutil

def request_messages(a):
    """ Requesting MAVLink messages"""
    a.mav.command_long_send(
        a.target_system,
        a.target_component,
        mavutil.mavlink.MAV_CMD_REQUEST_MESSAGE,
        253,
        0, 0, 0, 0, 0, 0, 0
    )
    print(a.recv_match(type='COMMAND_ACK', blocking=True, timeout=2))
    while True:
        print(a.recv_match(type='STATUSTEXT', blocking=True, timeout=10))

connection = mavutil.mavlink_connection('/dev/ttyACM2')
print(connection.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_ONBOARD_CONTROLLER, mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0))
connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (connection.target_system, connection.target_component))
request_messages(connection)

Output:

None
Heartbeat from system (system 1 component 0)
COMMAND_ACK {command : 512, result : 0, progress : 0, result_param2 : 0, target_system : 255, target_component : 0}
None
None
None
STATUSTEXT {severity : 2, text : PreArm: Hardware safety switch, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: AHRS: EKF3 Yaw inconsistent 130 deg. Wait , id : 7, chunk_seq : 0}
STATUSTEXT {severity : 2, text : or reboot, id : 7, chunk_seq : 1}
STATUSTEXT {severity : 2, text : PreArm: GPS 1: Bad fix, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: Battery 1 unhealthy, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: Waiting for RC, id : 0, chunk_seq : 0}
None
None

I don’t think we can ask for “STATUSTEXT” message as after the request message it is unable to print it. Even in my code inside the def method I tried the while loop and within which only receiving request and STATUSTEXT revcmatch. Then also no luck in getting these messages
Sample Code:

def request_messages(a):
    """ Requesting MAVLink messages"""
    while True:
        a.mav.command_long_send(
            a.target_system,
            a.target_component,
            mavutil.mavlink.MAV_CMD_REQUEST_MESSAGE,
            253,
            0, 0, 0, 0, 0, 0, 0
        )
        print(a.recv_match(type='STATUSTEXT', blocking=True, timeout=2))

Output:

None
Heartbeat from system (system 1 component 0)
None
None
None
None
None
STATUSTEXT {severity : 2, text : PreArm: Hardware safety switch, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: AHRS: EKF3 Yaw inconsistent 131 deg. Wait , id : 20, chunk_seq : 0}
STATUSTEXT {severity : 2, text : or reboot, id : 20, chunk_seq : 1}
STATUSTEXT {severity : 2, text : PreArm: GPS 1: Bad fix, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: Battery 1 unhealthy, id : 0, chunk_seq : 0}
STATUSTEXT {severity : 2, text : PreArm: Waiting for RC, id : 0, chunk_seq : 0}
None
None

You should just request the message once, thereafter it will be sent regularly. Think of it more like enabling those type of messages.

I think you might have missed out on my previous reply that there are two code I have run
The first one is the one I think you are referring to because there is a while loop only for reading STATUSTEXT which is the last command to execute inside that program that too continuously inside the while loop.

The frequency of these messages in 0.0 Hz NOt I can change the frequency of this message type.

I think these messages are only sent when certain events are triggered on the autopilot so changing the rate would probably not help.

Yes and as I am checking this message in a sequence after and before getting messages like gps, airspeed, imu etc. I miss these messages when triggered.
Suppose the reading sequence is:
while True:
read: HEARTBEAT → GPS_RAW_INT → GPS2_RAW → AIRSPEED1 → STATUSTEXT →
AIRSPEED2 → RAW_IMU
here the it will read these one after the other inside a thread. There are instances when I switch off the RC and at the particular moment it is reading either GPS_RAW_INT or AIRSPEED2 and these messages are triggered so because of these being in a sequence I miss these out STATUSTEXT Messages.