• python serial 加Phidgets 测试boottime


    公司要测试boottime,就自己写了个脚本,虽然写的有点丑,但是基本功能是能实现了。

    import os,sys
    if os.path.isdir('C:Python36libsite-packages'):
        sys.path.append('C:Python36libsite-packages')
    
    from serial import Serial
    from Phidgets.Devices.InterfaceKit import InterfaceKit
    import time,re
    import threading
    
    class mythread(threading.Thread):
    
        def __init__(self):
            super(mythread,self).__init__()
            self.__running = threading.Event()
            self.__running.set()
    
        def run(self):
            i = 0
            while self.__running.isSet():
                print("{0} --time: {1}".format("
    "*40,i),end='')
                i +=1
                time.sleep(1)
    
        def stop(self):
            self.__running.clear()
    
    class relay(object):
        def __init__(self,powerbutton=0):
            self.powerbutton = powerbutton
            self.device = InterfaceKit()
            self.device.openPhidget()
            self.device.waitForAttach(1000)
    
        def reset_system(self):
            print ('start to reset system')
            self.device.setOutputState(self.powerbutton,True)
            time.sleep(8)
            self.device.setOutputState(self.powerbutton,False)
            time.sleep(5)
            self.device.setOutputState(self.powerbutton,True)
            time.sleep(1)
            self.device.setOutputState(self.powerbutton,False)
            print ('finished to reset system')
    
    class BIOSCMD(object):
        # BIOS MENU UI
        BIOS_SETUP_MENU_ENTRY_PAT = r'Press[wW]+[F2]'
        BIOS_BOOTOPTON_SELECT_PAT = r'Press[wW]+[F7]'
        BIOS_LOGIN_PAT = r'localhost login:'
    
        BIOS_CMD_KEY_ENTER = '
    '
        BIOS_CMD_KEY_ESC = '33'+' '
        BIOS_CMD_KEY_UP = '33'+'[A'
        BIOS_CMD_KEY_DOWN = '33'+'[B'
        BIOS_CMD_KEY_RIGHT = '33'+'[C'
        BIOS_CMD_KEY_LEFT = '33'+'[D'
        BIOS_CMD_KEY_PAGEUP = '33'+'?'
        BIOS_CMD_KEY_PAGEDOWN = '33'+'/'
        BIOS_CMD_KEY_F1 = '33'+'1'
        BIOS_CMD_KEY_F2 = '33'+'2'
        BIOS_CMD_KEY_F3 = '33'+'3'
        BIOS_CMD_KEY_F4 = '33'+'4'
        BIOS_CMD_KEY_F5 = '33'+'5'
        BIOS_CMD_KEY_F6 = '33'+'6'
        BIOS_CMD_KEY_F7 = '33'+'7'
        BIOS_CMD_KEY_F8 = '33'+'8'
        BIOS_CMD_KEY_F9 = '33'+'9'
        BIOS_CMD_KEY_F10 = '33'+'0'
        BIOS_CMD_KEY_F11 = '33'+'!'
        BIOS_CMD_KEY_F12 = '33'+'@'
        BIOS_CMD_KEY_Y = 'y'
        BIOS_CMD_KEY_N = 'n'
        BIOS_CMD_KEY_DELETE = '33'+'-'
        BIOS_CMD_KEY_HOME = '33'+'h'
        BIOS_CMD_KEY_END = '33' + 'k'
        BIOS_CMD_KEY_INSERT = '33' + '+'
        BIOS_CMD_KEY_SPACE = ' '
        BIOS_CMD_KEY_BACKSPACE = '10'
        BIOS_CMD_KEY_CTRL_ALT_DELETE = '33R33r33R'
        BIOS_CMD_KEY_CTRL = '21'
        BIOS_CMD_KEY_ALT = '22'
        BIOS_CMD_KEY_SHIFT = '20'
        BIOS_CMD_KEY_PLUS = '20' + '+'
        BIOS_CMD_KEY_MINUS = '20' + '-'
    
    class TestBootTime():
        def __init__(self,Port,Bps,Timeout=None):
            self.Port = Port
            self.Bps = Bps
            self.Timeout = Timeout
            try:
                self.con=Serial(self.Port,self.Bps,timeout=self.Timeout)
            except:
                print('Open Serial Port Error')
                sys.exit(1)
    
        def read(self):
            #read form in_waiting
            #py3 date is bytes type,py2 data is str type
            try:
                lenth = self.con.in_waiting
                if lenth > 0:
                    data = self.con.read(lenth)
                    return data
    
            except Exception as e:
                print('read error:{}'.format(e))
    
        def write(self,cmd):
            self.con.flushOutput()
            try:
                self.con.write(bytes(cmd,encoding='utf-8'))
            except Exception as e:
                print("write error:{}".format(e))
    
        def enter_into_UEFI(self,timeout=900):
            serial_data = b''
            s_time=time.time()
            is_PressF7 = False
            is_IntoUEFI = False
            pattern_f7=re.compile(bytes(BIOSCMD.BIOS_BOOTOPTON_SELECT_PAT,encoding='utf-8'))
            while True:
                if time.time() - s_time > timeout:
                    print ('into F7 Timeout')
                    self.con.close()
                    sys.exit()
    
                data = self.read()
                if not data:
                    continue
    
                serial_data += data
    
                if is_PressF7:
                    if not is_IntoUEFI:
                        if serial_data.find(b'Please select boot device') != -1:
                            self.write(BIOSCMD.BIOS_CMD_KEY_DOWN)
                            self.write(BIOSCMD.BIOS_CMD_KEY_DOWN)
                            self.write(BIOSCMD.BIOS_CMD_KEY_DOWN)
                            self.write(BIOSCMD.BIOS_CMD_KEY_ENTER)
                            is_IntoUEFI = True
                    else:
                        if serial_data.find(b'Shell>') != -1:
                            return True
    
                else:
                    for serial_data_line in serial_data.split(b'
    '):
    
                        if not serial_data_line:
                            continue
    
                        if pattern_f7.search(serial_data_line):
                            self.write(BIOSCMD.BIOS_CMD_KEY_F7)
                            is_PressF7 = True
                            break
                        else:
                            if serial_data_line.endswith(b'
    '):
                                serial_data.replace(serial_data_line+b'
    ',b'')
    
    
        def enter_into_f2(self,timeout=900):
            serial_data = b''
            s_time = time.time()
            is_PressF2 = False
            pattern_f2 = re.compile(bytes(BIOSCMD.BIOS_SETUP_MENU_ENTRY_PAT,encoding='utf-8'))
            while True:
                if time.time() - s_time > timeout:
                    print ('into F2 Timeout')
                    self.con.close()
                    sys.exit()
    
                data = self.read()
                if not data:
                    continue
    
                serial_data += data
    
                if is_PressF2:
                    if serial_data.find(b'EDKII Menu') != -1:
                        return True
    
                else:
                    for serial_data_line in serial_data.split(b'
    '):
    
                        if not serial_data_line:
                            continue
    
                        if pattern_f2.search(serial_data_line):
                            self.write(BIOSCMD.BIOS_CMD_KEY_F2)
                            is_PressF2 = True
                            break
                        else:
                            if serial_data_line.endswith(b'
    '):
                                serial_data.replace(serial_data_line+b'
    ',b'')
    
        def test_F2_time(self):
            print ("Test Boot to F2 time ...")
            M = mythread()
            M.start()
            Start_time_F2 = time.time()
            if self.enter_into_f2():
                M.stop()
                print ("==>Boot to F2 Setup time={}".format(time.time() -  Start_time_F2))
    
        def test_UEFI_time(self):
            print ("Test Boot to UEFI time ...")
            M = mythread()
            M.start()
            Start_time_UEFI = time.time()
            if self.enter_into_UEFI():
                M.stop()
                print ("==>Boot to UEFI time={}".format(time.time() -  Start_time_UEFI))
    
        def test_os_time_by_ping(self,sut='192.168.137.5',timeout=900):
            print ("Test Boot to OS by ping time ...")
            M = mythread()
            M.start()
            Start_time_OS_ping = time.time()
            while True:
                if time.time() - Start_time_OS_ping > timeout:
                    print ('Into OS timeout')
                    self.con.close()
                    sys.exit()
                p = os.popen('ping {} -n 1 -w 1000 '.format(sut))
                output = p.read()
                if not output:
                    continue
    
                if output.find('Reply from {}'.format(sut))!= -1 and output.find('Lost = 0 (0% loss)') != -1:
                    M.stop()
                    print ("==> Boot to OS time={}".format(time.time() -  Start_time_OS_ping))
                    break
    
        def test_os_time_by_serial(self,timeout=900):
            print('Test Boot to OS by Serial time ...')
            serial_data = b''
            M = mythread()
            M.start()
            Start_time_OS_serial = time.time()
            pattern_login = re.compile(bytes(BIOSCMD.BIOS_LOGIN_PAT,encoding='utf-8'))
            while True:
                if time.time() - Start_time_OS_serial > timeout:
                    print ('into OS Timeout')
                    self.con.close()
                    sys.exit()
    
                data = self.read()
                if not data:
                    continue
    
                serial_data += data
    
                for serial_data_line in serial_data.split(b'
    '):
                    if not serial_data_line:
                            continue
    
                    if pattern_login.search(serial_data_line):
                        print ("==> Boot to OS time={}".format(time.time() -  Start_time_OS_serial))
                        M.stop()
                        return True
                    else:
                        if serial_data_line.endswith(b'
    '):
                            serial_data.replace(serial_data_line+b'
    ',b'')
    
    if __name__ == '__main__':
        T = TestBootTime(Port='COM3',Bps='115200')
        R = relay(powerbutton=0)
    
        print ('[Test Start]')
        R.reset_system()
        T.test_F2_time()
    
        R.reset_system()
        T.test_UEFI_time()
    
        R.reset_system()
        #T.test_os_time_by_ping(sut='192.168.137.5')
        T.test_os_time_by_serial()
    
        print ('Test Finished')
        
    
         
  • 相关阅读:
    mybatis入参错误:There is no getter for property named ‘status‘ in ‘class java.lang.Integer‘
    JAVA程序员面试笔试题(一)
    Java8新特性LocalDateTime获取周几
    linux常用命令记录 screen
    ubuntu 19.04 + lenovo-xiaoxin-I2000 触摸板右键单击无法使用
    华为交换路由常用命令
    centos7常用软件
    一般网络延迟高的原因
    华为防火墙进程&简单配置
    私网互联(本质是三层路由)
  • 原文地址:https://www.cnblogs.com/xia-dong/p/12807271.html
Copyright © 2020-2023  润新知