From e99498dc8d6857157e979d946e2569495cc018df Mon Sep 17 00:00:00 2001 From: noahredon Date: Tue, 21 Apr 2026 12:31:30 +0100 Subject: [PATCH 1/5] feat: Add set_mavlink_version function in mavutil.py --- mavutil.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mavutil.py b/mavutil.py index ecdcf4402..38a5b006b 100644 --- a/mavutil.py +++ b/mavutil.py @@ -139,6 +139,24 @@ def set_dialect(dialect: str, with_type_annotations: bool | None = None) -> None current_dialect = dialect mavlink = mod +def set_mavlink_version(version): + '''set the MAVLink version to work with. + For example, set_mavlink_version(2.0) + ''' + v = float(version) + if v == 2.0: + os.environ.pop('MAVLINK09', None) + os.environ['MAVLINK20'] = '1' + elif v == 1.0: + os.environ.pop('MAVLINK09', None) + os.environ.pop('MAVLINK20', None) + elif v == 0.9: + os.environ.pop('MAVLINK20', None) + os.environ['MAVLINK09'] = '1' + else: + raise ValueError("Wrong MAVLink version") + set_dialect(current_dialect) + # Set the default dialect. This is done here as it needs to be after the function declaration set_dialect(os.environ['MAVLINK_DIALECT']) From 1adc8320d157f9861d799a2260c436b06cbe5a3b Mon Sep 17 00:00:00 2001 From: noahredon Date: Tue, 21 Apr 2026 13:07:00 +0100 Subject: [PATCH 2/5] mavutil.py: set_mavlink_version used in mavfile.auto_mavlink_version --- mavutil.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mavutil.py b/mavutil.py index 38a5b006b..87d92b309 100644 --- a/mavutil.py +++ b/mavutil.py @@ -312,8 +312,7 @@ def auto_mavlink_version(self, buf): self.first_byte = False if self.WIRE_PROTOCOL_VERSION != "2.0" and magic == 253: self.WIRE_PROTOCOL_VERSION = "2.0" - os.environ['MAVLINK20'] = '1' - set_dialect(current_dialect) + set_mavlink_version(2.0) else: return # switch protocol From 14f0f202ad38261061cc6d35842efdbd8529072f Mon Sep 17 00:00:00 2001 From: noahredon Date: Thu, 30 Apr 2026 14:45:14 +0100 Subject: [PATCH 3/5] mavutil: warning added to set_mavlink_version docstring --- mavutil.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mavutil.py b/mavutil.py index 87d92b309..1c1916055 100644 --- a/mavutil.py +++ b/mavutil.py @@ -142,6 +142,11 @@ def set_dialect(dialect: str, with_type_annotations: bool | None = None) -> None def set_mavlink_version(version): '''set the MAVLink version to work with. For example, set_mavlink_version(2.0) + + Warning: If the currently loaded dialect is specific to a single MAVLink + version (e.g., a v2-only dialect) and you switch to an incompatible version, + this function will raise a FileNotFoundError because the underlying + set_dialect() call will fail to find the corresponding XML file ''' v = float(version) if v == 2.0: From 3b92ab13cd57c47bff6686e96db6e05a767393de Mon Sep 17 00:00:00 2001 From: noahredon Date: Sun, 30 Aug 2026 16:53:35 +0100 Subject: [PATCH 4/5] mavutil: set_mavlink_version: rollback to old environment variables state if exception in set_dialect --- mavutil.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mavutil.py b/mavutil.py index 1c1916055..7d3859be9 100644 --- a/mavutil.py +++ b/mavutil.py @@ -149,6 +149,11 @@ def set_mavlink_version(version): set_dialect() call will fail to find the corresponding XML file ''' v = float(version) + + old_m09 = os.environ.get('MAVLINK09', None) + old_m20 = os.environ.get('MAVLINK20', None) + + # Apply new environment variables if v == 2.0: os.environ.pop('MAVLINK09', None) os.environ['MAVLINK20'] = '1' @@ -160,7 +165,21 @@ def set_mavlink_version(version): os.environ['MAVLINK09'] = '1' else: raise ValueError("Wrong MAVLink version") - set_dialect(current_dialect) + + # Try to set the dialect, rollback if it fails + try: + set_dialect(current_dialect) + except Exception as e: + if old_m09 is not None: + os.environ['MAVLINK09'] = old_m09 + else: + os.environ.pop('MAVLINK09', None) + if old_m20 is not None: + os.environ['MAVLINK20'] = old_m20 + else: + os.environ.pop('MAVLINK20', None) + + raise e # Set the default dialect. This is done here as it needs to be after the function declaration set_dialect(os.environ['MAVLINK_DIALECT']) From feae683ea418135fbd745229dffd30674bfb6de5 Mon Sep 17 00:00:00 2001 From: noahredon Date: Wed, 2 Sep 2026 14:27:58 +0100 Subject: [PATCH 5/5] mavutil: set_mavlink_version: Remove support for the MAVLink 0.9 wire protocol --- mavutil.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/mavutil.py b/mavutil.py index 7d3859be9..b910adf4d 100644 --- a/mavutil.py +++ b/mavutil.py @@ -147,22 +147,18 @@ def set_mavlink_version(version): version (e.g., a v2-only dialect) and you switch to an incompatible version, this function will raise a FileNotFoundError because the underlying set_dialect() call will fail to find the corresponding XML file + + Deprecated: 0.9 is no longer supported ''' v = float(version) - old_m09 = os.environ.get('MAVLINK09', None) old_m20 = os.environ.get('MAVLINK20', None) # Apply new environment variables if v == 2.0: - os.environ.pop('MAVLINK09', None) os.environ['MAVLINK20'] = '1' elif v == 1.0: - os.environ.pop('MAVLINK09', None) - os.environ.pop('MAVLINK20', None) - elif v == 0.9: os.environ.pop('MAVLINK20', None) - os.environ['MAVLINK09'] = '1' else: raise ValueError("Wrong MAVLink version") @@ -170,10 +166,6 @@ def set_mavlink_version(version): try: set_dialect(current_dialect) except Exception as e: - if old_m09 is not None: - os.environ['MAVLINK09'] = old_m09 - else: - os.environ.pop('MAVLINK09', None) if old_m20 is not None: os.environ['MAVLINK20'] = old_m20 else: