I wanted to read two types of data at the same time, but failed

I wanted to read two types of data at the same time, “Alt” in “VFR_HUD” and “xACC” in “RAW_IMU”, so I wrote the following code:

from pymavlink import mavutil
def DO_Filter():
    master = mavutil.mavlink_connection('udpin:0.0.0.0:14550')
    while True:
        msg = master.recv_match()
        # print(msg)
        if not msg:
            continue
        if msg.get_type()=='VFR_HUD' or msg.get_type()=='RAW_IMU':
            print(msg.alt)
            print(msg.xacc)

if __name__=='__main__':
    DO_Filter()

However, the following error message appears on the terminal:

PS C:\Users\32343\Desktop\mytest3.6.5> & D:/Programme/Python3.8.0/python.exe c:/Users/32343/Desktop/mytest3.6.5/test/test27.py
Traceback (most recent call last):
  File "c:/Users/32343/Desktop/mytest3.6.5/test/test27.py", line 58, in <module>
    DO_Filter()
  File "c:/Users/32343/Desktop/mytest3.6.5/test/test27.py", line 54, in DO_Filter
    print(msg.alt)
AttributeError: 'MAVLink_raw_imu_message' object has no attribute 'alt'

Thanks for your reply!

Hi @robotmark,

You can’t treat every message as the same - VFR_HUD messages don’t have an xacc property, and RAW_IMU messages don’t have an alt property.

There are a few different approaches to solve this problem discussed here :slight_smile:

1 Like

Thank you. The problem has been solved successfully.

1 Like