乐趣区

关于python:python串口自动测试

装置应用 pip install pyserial

关上和退出信号


com = serial.Serial("com15", 115200, timeout=0.5)

def sig_handler(sig, frame):
    com.close()
    exit(0)

signal.signal(signal.SIGTERM, sig_handler)
signal.signal(signal.SIGINT, sig_handler)

try:
    com.open()
except:
    com.close()
    com.open()

读取一行输入

    try:
        readline = str(com.readline(), encoding = "utf8")
    except:
        continue

读取指定信息时候发送指定命令

    try:
        if re.findall(r'xxx readline, flags=0):
            com.write(bytes("\r\nreboot\r\n", encoding = "utf8"))
    except:
        print("some errr....")
退出移动版