From 113cc308a15d11bea1d60688bd54a4cbad63b0d4 Mon Sep 17 00:00:00 2001 From: Milosz Lagan Date: Fri, 3 Jan 2025 10:40:19 +0000 Subject: [PATCH] Wait until a serial port has been connected --- .../husarion_ugv_crsf_teleop/crsf_teleop_node.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/husarion_ugv_crsf_teleop/husarion_ugv_crsf_teleop/crsf_teleop_node.py b/husarion_ugv_crsf_teleop/husarion_ugv_crsf_teleop/crsf_teleop_node.py index c843285..418610c 100644 --- a/husarion_ugv_crsf_teleop/husarion_ugv_crsf_teleop/crsf_teleop_node.py +++ b/husarion_ugv_crsf_teleop/husarion_ugv_crsf_teleop/crsf_teleop_node.py @@ -109,7 +109,13 @@ def __init__(self): if len(self.linear_speed_presets.value) != 3 or len(self.angular_speed_presets.value) != 3: raise ValueError("Speed presets must be a list of 3 values") - self.serial = serial.Serial(port.value, baud.value, timeout=2) + self.serial = None + while self.serial is None: + try: + self.serial = serial.Serial(port.value, baud.value, timeout=2) + except serial.SerialException as e: + self.get_logger().error(f"Failed to open serial port: {e}") + rclpy.spin_once(self, timeout_sec=2) self.parser = CRSFParser() self.parser.on_message = lambda msg: self.handle_message(msg)