From 529200dfd5149e9b6a8e8086b735ae62124d518b Mon Sep 17 00:00:00 2001 From: Kong Fanming Date: Thu, 15 Apr 2021 11:26:43 +0800 Subject: [PATCH 1/3] add timestamp function. --- terminal_s/terminal.py | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/terminal_s/terminal.py b/terminal_s/terminal.py index 8e38b91..a711c23 100644 --- a/terminal_s/terminal.py +++ b/terminal_s/terminal.py @@ -20,6 +20,7 @@ import colorama import click import serial +import datetime from serial.tools import list_ports @@ -35,9 +36,13 @@ def run(port, baudrate, parity='N', stopbits=1): print('--- Failed to open {} ---'.format(port)) return 0 - print('--- {} is connected. Press Ctrl+] to quit ---'.format(port)) + print('--- {} is connected. Press Ctrl+] to quit, Press Ctrl+t to show or hide timestamp---'.format(port)) queue = deque() + timestamp_en = bool() + timestamp_input_en = bool() def read_input(): + nonlocal timestamp_en + nonlocal timestamp_input_en if os.name == 'nt': from msvcrt import getch else: @@ -53,15 +58,35 @@ def read_input(): # print(ch) if ch == b'\x1d': # 'ctrl + ]' to quit break - if ch == b'\x00' or ch == b'\xe0': # arrow keys' escape sequences + if ch == b'\x14': # 'ctrl + t' to change timestamp status + timestamp_en = bool(1-timestamp_en) + if ch == b'\x00' or ch == b'\xe0': # arrow keys' escape sequences for windows ch2 = getch() esc_dict = { b'H': b'A', b'P': b'B', b'M': b'C', b'K': b'D', b'G': b'H', b'O': b'F' } if ch2 in esc_dict: queue.append(b'\x1b[' + esc_dict[ch2]) else: queue.append(ch + ch2) - else: + timestamp_input_en = False + else: queue.append(ch) + if ch == b' ' or ch == b'\n' or ch == b'\r': + timestamp_input_en = False + elif ch == b'\x1b': # arrow keys' escape sequences for linux + ch2 = getch() + if ch2 == b'[': + ch2 = getch() + esc_dict = { b'A': b'A', b'B': b'B', b'C': b'C', b'D': b'D', b'H': b'H', b'F': b'F' } + if ch2 in esc_dict: + queue.append(b'[' + esc_dict[ch2]) + else: + queue.append(b'[' + ch2) + else: + queue.append(ch2) + timestamp_input_en = False + else: + timestamp_input_en = True + # print(queue) if os.name != 'nt': termios.tcsetattr(stdin_fd, termios.TCSADRAIN, tty_attr) @@ -73,12 +98,19 @@ def read_input(): while thread.is_alive(): try: length = len(queue) + queue_bak = queue.copy() if length > 0: device.write(b''.join(queue.popleft() for _ in range(length))) line = device.readline() if line: - print(line.decode(errors='replace'), end='', flush=True) + if (line in queue_bak and len(line) == len(queue_bak)) or (timestamp_en == False or timestamp_input_en == False) or (line == b'\r' or line == b'\r\n'): + print(line.decode(errors='replace'), end='', flush=True) + if timestamp_en == True: + timestamp_input_en = True + else: + time_now = datetime.datetime.now().strftime('%H:%M:%S.%f') + print('\033[1;35m ' + time_now + '\033[0m ' + line.decode(errors='replace'), end='', flush=True) except IOError: print('--- {} is disconnected ---'.format(port)) break From 78c792f91d6dadef334ec11595cabc61b4bc2310 Mon Sep 17 00:00:00 2001 From: Kong Fanming Date: Mon, 31 May 2021 16:18:19 +0800 Subject: [PATCH 2/3] add clear screen Signed-off-by: Kong Fanming --- terminal_s/terminal.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/terminal_s/terminal.py b/terminal_s/terminal.py index a711c23..b481646 100644 --- a/terminal_s/terminal.py +++ b/terminal_s/terminal.py @@ -23,6 +23,15 @@ import datetime from serial.tools import list_ports +# define our clear function +def screen_clear(): + # for windows + if os.name == 'nt': + os.system('cls') + # for mac and linux(here, os.name is 'posix') + else: + os.system('clear') + def run(port, baudrate, parity='N', stopbits=1): try: @@ -36,7 +45,10 @@ def run(port, baudrate, parity='N', stopbits=1): print('--- Failed to open {} ---'.format(port)) return 0 - print('--- {} is connected. Press Ctrl+] to quit, Press Ctrl+t to show or hide timestamp---'.format(port)) + print('--- {} is connected.---'.format(port)) + print('---Press Ctrl+] to quit---') + print('---Press Ctrl+t to show or hide timestamp---') + print('---Press Ctrl+l clear screen---') queue = deque() timestamp_en = bool() timestamp_input_en = bool() @@ -58,6 +70,8 @@ def read_input(): # print(ch) if ch == b'\x1d': # 'ctrl + ]' to quit break + if ch == b'\x0c': + screen_clear() # 'ctrl + l' to clear screen if ch == b'\x14': # 'ctrl + t' to change timestamp status timestamp_en = bool(1-timestamp_en) if ch == b'\x00' or ch == b'\xe0': # arrow keys' escape sequences for windows @@ -111,6 +125,14 @@ def read_input(): else: time_now = datetime.datetime.now().strftime('%H:%M:%S.%f') print('\033[1;35m ' + time_now + '\033[0m ' + line.decode(errors='replace'), end='', flush=True) + if (b'login:' in line): + usrname = b'root\r\n'; + #print(usrname) + device.write(usrname) + if (b'Password:' in line): + password = b'CherryYoudao\r\n'; + #print(password) + device.write(password) except IOError: print('--- {} is disconnected ---'.format(port)) break From 80bf1f9022a0988a8c905741f5d8a34aa5c51268 Mon Sep 17 00:00:00 2001 From: Fanming Kong Date: Thu, 2 Sep 2021 17:00:18 +0800 Subject: [PATCH 3/3] fix usb device name show Signed-off-by: Fanming Kong --- terminal_s/terminal.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/terminal_s/terminal.py b/terminal_s/terminal.py index b481646..2018dd3 100644 --- a/terminal_s/terminal.py +++ b/terminal_s/terminal.py @@ -1,8 +1,6 @@ """ Terminal for serial port - Requirement: - + pyserial + colorama + py-getch @@ -23,12 +21,12 @@ import datetime from serial.tools import list_ports -# define our clear function +# define our clear function def screen_clear(): - # for windows + # for windows if os.name == 'nt': os.system('cls') - # for mac and linux(here, os.name is 'posix') + # for mac and linux(here, os.name is 'posix') else: os.system('clear') @@ -167,7 +165,7 @@ def main(port, baudrate, parity, stopbits, l): else: print('--- Available Ports ----') for i, v in enumerate(ports): - print('--- {}: {} {}'.format(i, v[0], v[2])) + print('--- {}: {} {}'.format(i, v[0], v[1])) if l: return raw = input('--- Select port index: ') @@ -181,4 +179,4 @@ def main(port, baudrate, parity, stopbits, l): pass if __name__ == "__main__": - main() + main() \ No newline at end of file