From 4e43cc3518e1e4bef698530bf017838325c23de9 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Fri, 29 Jun 2018 02:18:22 +0200 Subject: [PATCH] px_uploader.py fix flashing issue (#9792) This fixes a problem where the pyserial write call gets stuck. It happens on a specific Fedora 28 system with internal USB ports as well as USB hubs. It is not clear why the problem is resolved but it is clearly reproducible that with a timeout of 0, the write can get stuck and with a timeout > 0 it works every time. The exception added as part of this commit makes sense but has never been triggered in my testing. --- Tools/px_uploader.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Tools/px_uploader.py b/Tools/px_uploader.py index c1c260706d..c9d05f8697 100755 --- a/Tools/px_uploader.py +++ b/Tools/px_uploader.py @@ -191,8 +191,11 @@ class uploader(object): MAVLINK_REBOOT_ID0 = bytearray(b'\xfe\x21\x45\xff\x00\x4c\x00\x00\x40\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf6\x00\x00\x00\x00\xcc\x37') def __init__(self, portname, baudrate_bootloader, baudrate_flightstack): - # open the port, keep the default timeout short so we can poll quickly - self.port = serial.Serial(portname, baudrate_bootloader, timeout=0.5) + # Open the port, keep the default timeout short so we can poll quickly. + # On some systems writes can suddenly get stuck without having a + # write_timeout > 0 set. + self.port = serial.Serial(portname, baudrate_bootloader, timeout=0.5, write_timeout=0.5) + # self.port.write_timeout = 0.5 self.otp = b'' self.sn = b'' self.baudrate_bootloader = baudrate_bootloader @@ -230,7 +233,14 @@ class uploader(object): def __send(self, c): # print("send " + binascii.hexlify(c)) - self.port.write(c) + while True: + try: + self.port.write(c) + break + except serial.SerialTimeoutException as e: + print("Write timeout (%s), trying again" % e) + time.sleep(0.04) + continue def __recv(self, count=1): c = self.port.read(count)