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.
This commit is contained in:
Julian Oes 2018-06-29 02:18:22 +02:00 committed by Daniel Agar
parent 96f47d4772
commit 4e43cc3518

View File

@ -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)