Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clear screen #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 60 additions & 8 deletions terminal_s/terminal.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Terminal for serial port

Requirement:

+ pyserial
+ colorama
+ py-getch
Expand All @@ -20,8 +18,18 @@
import colorama
import click
import serial
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:
Expand All @@ -35,9 +43,16 @@ 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.---'.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()
def read_input():
nonlocal timestamp_en
nonlocal timestamp_input_en
if os.name == 'nt':
from msvcrt import getch
else:
Expand All @@ -53,15 +68,37 @@ 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'\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
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)
Expand All @@ -73,12 +110,27 @@ 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)
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
Expand Down Expand Up @@ -113,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: ')
Expand All @@ -127,4 +179,4 @@ def main(port, baudrate, parity, stopbits, l):
pass

if __name__ == "__main__":
main()
main()