串口消息通过颜色区分

This commit is contained in:
ranchuan
2025-02-19 18:33:52 +08:00
parent 80d6120246
commit faa480ed9f
2 changed files with 12 additions and 8 deletions

View File

@@ -5,6 +5,11 @@ import sys
# 同一个进程中所有调用这个文件的 .py 文件都使用这个变量
_log_fp=None
STR_RED="\033[0;31m"
STR_BLUE="\033[0;34m"
STR_YELLOW="\033[0;33m"
STR_END="\033[0m"
def _time():
return '['+time.strftime("%Y-%m-%d %H:%M:%S")+']'
@@ -30,7 +35,7 @@ def mywrite(data:str):
txt=txt.replace('\n\n','\n')
_log_fp.write(txt)
_log_fp.flush()
sys.stdout.write(data)
sys.stdout.write(f"{STR_YELLOW}{data}{STR_END}")
sys.stdout.flush()
def log_init(file_name:str):

View File

@@ -119,13 +119,12 @@ def download_callback(total_packets, success_count, error_count):
# 打印串口收到的字符
def print_device_str(data:bytearray):
data:list[bytearray]=data.split(b"\r\n")
for item in data:
try:
d=item.decode('utf-8')
myprint("DEVICE:",d.strip())
except Exception as e:
myprint("DEVICE:",item)
data=data.replace(b"\r\n",b"\n")
try:
d=data.decode('utf-8')
mywrite(d)
except Exception as e:
mywrite(f"{data}\n")