赞
踩
- #-*- coding:UTF-8 -*-
- # author:Lake Hu :2020/9
- # -*- coding: utf-8 -*-
-
-
-
- import time
- import serial
- import re
- import random
- import time
-
- localtime = time.asctime(time.localtime(time.time()))
-
- reboot_timeout_default = 30
- read_timeout_default = 5
-
- ser = serial.Serial("COM1", 115200, timeout=read_timeout_default)
- print "open success:%s bps %d" %(ser.name,ser.baudrate)
-
-
- def print_cmd_out_serial(fail_keyword_match=""):
- while True:
- # read line
- data = ser.readlines()
- # check if line is not empty
- if not data:
- if fail_keyword_match!="":
- continue
- else:
- break
- else:
- for line in data:
- line = line.strip()
- print "%s" % (line)
-
- if fail_keyword_match!="" and re.match(fail_keyword_match,line):
- return False
- break
- return True
-
-
-
- def mount_check():
- print("send mount now")
- sendcmd="mount\n"
- ser.write(sendcmd)
-
- return print_cmd_out_serial("/dev/mmcblk0p1 on /flash")
-
-
- def mount_flash():
- print("send echo 3 > /proc/sys/vm/drop_caches now")
- sendcmd="echo 3 > /proc/sys/vm/drop_caches\n"
- ser.write(sendcmd)
-
- print("send mount /dev/mmcblk0p1 /flash now")
- sendcmd="mount /dev/mmcblk0p1 /flash\n"
- ser.write(sendcmd)
-
- return print_cmd_out_serial("failed")
-
-
-
- def dd_test(bs,count,skip,readtimeout,isbackgroud=False):
- #sendcmd = "rm -f /tmp/data \n"
- #print("send %s "%(sendcmd))
- #ser.write(sendcmd)
- sendcmd = "dd if=/dev/mmcblk0p1 bs=%s count=%d skip=%d of=/tmp/data \n" %(bs,count,skip)
- print("send readtimeout=%d : %s"%(readtimeout,sendcmd))
- ser.write(sendcmd)
- ser.timeout = readtimeout
- print_cmd_out_serial()
-
- ser.timeout = read_timeout_default ### reset
- if(isbackgroud==False):
- sendcmd = "dd if=/tmp/data of=/flash/data ;sync \n"
- else:
- sendcmd = "dd if=/tmp/data of=/flash/data%d_%d_%d ;sync &\n" %(bs,count,skip)
- print("send %s"%(sendcmd))
- ser.write(sendcmd)
- print_cmd_out_serial()
- return True
-
-
- def reboot():
- print("send reboot now")
- sendcmd="reboot\n"
- ser.write(sendcmd)
-
-
- def kreatv_dd_test():
- bs = 1
- count = 2*1024*1024
- skip = 0x438
- readtimeout = read_timeout_default
-
- for i in range(1,20):
- bs = random.randint(1,100)
- count = 2*1024*1024/bs +random.randint(1,500)
- skip = i*random.randint(1,100) + 1024*bs
- ##readtimeout = (bs*count/1024/1024%300) * 2 ### max wait for 5min *2
- ##if(readtimeout == 0):
- ## readtimeout = read_timeout_default
-
- print time.asctime(time.localtime(time.time())), "dd test No. ", i
- dd_test(bs,count,skip,read_timeout_default,random.choice([True, False]))
-
-
-
- def kreattv():
- starttime = time.asctime(time.localtime(time.time()))
- while ser.isOpen():
- print starttime, "Start Time"
- reboot()
- print time.asctime(time.localtime(time.time())), "Reboot ===> Sleep"
- time.sleep(reboot_timeout_default + random.randint(1,10))
- while True:
- # read line
- data = ser.readlines()
- # check if line is not empty
- if not data:
- break
- else:
- for line in data:
- line = line.strip()
- print "%s" % (line)
-
- sendcmd="uptime\n"
- print("send %s" %sendcmd)
- ser.write(sendcmd)
- print_cmd_out_serial("min")
-
- if(mount_flash()==False):
- print time.asctime(time.localtime(time.time())), "!!! Got corrupt, stop now!! "
- ser.close()
- os._exit(1)
-
- kreatv_dd_test()
- print time.asctime(time.localtime(time.time())), "One loop is done"
- print time.asctime(time.localtime(time.time())), "Completely Stop Close"
- ser.close()
-
- if __name__ == "__main__":
- kreattv()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。