Skip to content

Commit

Permalink
add reconnecting option
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Jun 21, 2020
1 parent 72ac946 commit 8bda5e5
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions terminal_s/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 8bda5e5

Please sign in to comment.