我正在尝试使用this Python script在Debian上对Pixhawk进行编程,但是它总是会卡住。在装有Ubuntu的另一台计算机上,它运行良好。我确定我是 dialout 组的成员。我成功地使用了Debian上的QGroundControl应用程序来连接设备。
我确定该错误发生在serial.Serial()
:
class uploader(object):
'''Uploads a firmware file to the PX FMU bootloader'''
def __init__(self, portname, baudrate_bootloader, baudrate_flightstack):
print("DEBUG: init")
print("DEBUG: init port")
print("DEBUG:",portname)
print("DEBUG:",baudrate_bootloader)
try:
self.port = serial.Serial(portname, baudrate_bootloader, timeout=2, write_timeout=2)
except:
print("Who knows")
控制台输出:
DEBUG: init
DEBUG: init port
DEBUG: /dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00
DEBUG: 115200
Who knows
如果我尝试通过以下方式捕获异常:
except (OSError, serial.SerialException, serial.SerialTimeoutException):
这行似乎被忽略了。
这是怎么回事?我该如何解决呢?
$ ll /dev/ttyACM0 /dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00
lrwxrwxrwx 1 root root 13 Feb 12 23:04 /dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00 -> ../../ttyACM0
crw-rw---- 1 root dialout 166, 0 Feb 12 23:04 /dev/ttyACM0
作为说明:
原始脚本陷入无休止的循环,通过发出print
调用,我发现这段代码被删除可能导致以下情况:
class uploader(object):
...
def __init__(self, portname, baudrate_bootloader, baudrate_flightstack):
# 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.
# chartime 8n1 * bit rate is us
self.chartime = 10 * (1.0 / baudrate_bootloader)
# we use a window approche to SYNC,<result> gathring
self.window = 0
self.window_max = 256
self.window_per = 2 # Sync,<result>
self.ackWindowedMode = False # Assume Non Widowed mode for all USB CDC
self.port = serial.Serial(portname, baudrate_bootloader, timeout=0.5, write_timeout=0.5)
self.otp = b''
self.sn = b''
self.baudrate_bootloader = baudrate_bootloader
self.baudrate_flightstack = baudrate_flightstack
self.baudrate_flightstack_idx = -1
我发现,self.port = serial.Serial(...)
后面不会出现打印。也没有错误消息或崩溃。不会发生崩溃,因为上传者的调用在try内:
try:
if "linux" in _platform:
...
up = uploader(port, args.baud_bootloader, baud_flightstack)
elif ...
except Exception:
# open failed, rate-limit our attempts
time.sleep(0.05)
# and loop to the next port
continue
我不确定,但这似乎是Python pyserial的问题,仅在Debian上发生。我无法在其他操作系统上重现此内容。但是在其他Debian上。