python读取BIOS串口信息

#!-*-coding:utf8-*-
import serial  # 导入模块


####Get avilable port
import serial.tools.list_ports

port_list = list(serial.tools.list_ports.comports())
print(port_list)
if len(port_list) == 0:
    print('无可用串口')
else:
    for i in range(0, len(port_list)):
        print(port_list[i])
# COM9 - Prolific USB-to-Serial Comm Port (COM9)
# COM3 - Intel(R) Active Management Technology - SOL (COM3)
# COM6 - USB Serial Port (COM6)
# COM1 - Communications Port (COM1)
# COM4 - USB Serial Port (COM4)
# COM5 - USB Serial Port (COM5)


####Read data from serial port
import serial  # 导入模块
import sys

# 端口,GNU / Linux上的/ dev / ttyUSB0 等 或 Windows上的 COM3 等
portx = "COM100"
# 波特率,标准值之一:50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200
bps = 115200
# 超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)
timex = 5
# 打开串口,并得到串口对象
ser = serial.Serial(portx, bps, timeout=timex)

ret = ser.write(b"Hello ")
print "ret is :___>>>", ret,ser.isOpen()
if ser.isOpen():
    print "port has been opened"
else:
    ser.open()
    print "port not  opened,open it"
# 写数据
f=open("serial.log","a")

while True:

    s=ser.read(1024)
    ser.flush()
    print s
    f.write(str(s))
    f.flush()
    print "res===========",s
    if "All Cpu Information" in s:
        print "All Cpu Information in s ,exit"
        ser.close()  # 关闭串口
        sys.exit(0)


# except Exception as e:
#     print("---异常---:", e)

 

posted @ 2020-11-04 17:07  腹肌猿  阅读(714)  评论(0编辑  收藏  举报