diff --git a/terminal_s/terminal.py b/terminal_s/terminal.py index 2cb5105..8e38b91 100644 --- a/terminal_s/terminal.py +++ b/terminal_s/terminal.py @@ -23,35 +23,7 @@ from serial.tools import list_ports -CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) - -@click.command(context_settings=CONTEXT_SETTINGS) -@click.option('-p', '--port', default=None, help='serial port name') -@click.option('-b', '--baudrate', default=115200, help='set baud reate') -@click.option('--parity', default='N', type=click.Choice(['N', 'E', 'O', 'S', 'M']), help='set parity') -@click.option('-s', '--stopbits', default=1, help='set stop bits') -@click.option('-l', is_flag=True, help='list serial ports') -def main(port, baudrate, parity, stopbits, l): - if port is None: - ports = list_ports.comports() - if not ports: - print('--- No serial port available ---') - return - if len(ports) == 1: - port = ports[0][0] - else: - print('--- Available Ports ----') - for i, v in enumerate(ports): - print('--- {}: {}'.format(i, v)) - - if l: - return - raw = input('--- Select port index: ') - try: - n = int(raw) - port = ports[n][0] - except: - return +def run(port, baudrate, parity='N', stopbits=1): try: device = serial.Serial(port=port, baudrate=baudrate, @@ -61,12 +33,10 @@ def main(port, baudrate, parity, stopbits, l): timeout=0.1) except: print('--- Failed to open {} ---'.format(port)) - return - - print('--- Press Ctrl+] to quit ---') + return 0 + print('--- {} is connected. Press Ctrl+] to quit ---'.format(port)) queue = deque() - def read_input(): if os.name == 'nt': from msvcrt import getch @@ -110,11 +80,51 @@ def read_input(): if line: print(line.decode(errors='replace'), end='', flush=True) except IOError: - print('Device is disconnected') + print('--- {} is disconnected ---'.format(port)) break device.close() + if thread.is_alive(): + print('--- Press R to reconnect the device, or press Enter to exit ---') + thread.join() + if queue and queue[0] in (b'r', b'R'): + return 1 + return 0 + + + + +CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) + +@click.command(context_settings=CONTEXT_SETTINGS) +@click.option('-p', '--port', default=None, help='serial port name') +@click.option('-b', '--baudrate', default=115200, help='set baud reate') +@click.option('--parity', default='N', type=click.Choice(['N', 'E', 'O', 'S', 'M']), help='set parity') +@click.option('-s', '--stopbits', default=1, help='set stop bits') +@click.option('-l', is_flag=True, help='list serial ports') +def main(port, baudrate, parity, stopbits, l): + if port is None: + ports = list_ports.comports() + if not ports: + print('--- No serial port available ---') + return + if len(ports) == 1: + port = ports[0][0] + else: + print('--- Available Ports ----') + for i, v in enumerate(ports): + print('--- {}: {} {}'.format(i, v[0], v[2])) + if l: + return + raw = input('--- Select port index: ') + try: + n = int(raw) + port = ports[n][0] + except: + return + while run(port, baudrate, parity, stopbits): + pass if __name__ == "__main__": main()