Files
Enrik Berkhan 4030508105 Fix 'solo monitor' reconnect.
On my Linux system (and maybe others as well), /dev/ttyACM0 is only
created after the Solo key is inserted. If you try to 'monitor' before
inserting, it will fail because the device node does not yet exist. Of
course one can use debug-2 to stall the Solo key until monitor opens the
serial device. However, this has the side effect of making the Solo key
not work without a running monitor.

Second, if you disconnect the Solo key, 'monitor' tries to reconnect,
but does not close the serial device before. The device node might be
deleted, but the minor number is still in use. Therefore, on next
connect e.g. /dev/ttyACM1 will be used and the reconnection will fail.

This change

1. enters reconnect mode also when the device node does not exist
initially

2. closes the device before entering reconnect mode if it was opened
before

(This has been tested on Linux, namely Ubuntu 20.04, and Windows 10. On
Windows it worked fine also without the change.)
2020-10-25 23:36:36 +01:00

50 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright 2019 SoloKeys Developers
#
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.
import sys
import time
import click
import serial
@click.command()
@click.argument("serial_port")
def monitor(serial_port):
"""Reads Solo Hacker serial output from USB serial port SERIAL_PORT.
SERIAL-PORT is something like /dev/ttyACM0 or COM10.
Automatically reconnects. Baud rate is 115200.
"""
ser = None
def reconnect():
while True:
time.sleep(0.02)
try:
ser = serial.Serial(serial_port, 115200, timeout=0.05)
return ser
except serial.SerialException:
pass
while True:
try:
if ser is None:
ser = serial.Serial(serial_port, 115200, timeout=0.05)
data = ser.read(1)
sys.stdout.buffer.write(data)
sys.stdout.flush()
except serial.SerialException:
if ser is not None:
ser.close()
print(f"reconnecting {serial_port}...")
ser = reconnect()
print("done")