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....")

你可能感兴趣的:(python,串口)