当前位置:   article > 正文

python2.7/serial/test/ARM Linux_python2.7使用serial协议

python2.7使用serial协议
  1. #-*- coding:UTF-8 -*-
  2. # author:Lake Hu :2020/9
  3. # -*- coding: utf-8 -*-
  4. import time
  5. import serial
  6. import re
  7. import random
  8. import time
  9. localtime = time.asctime(time.localtime(time.time()))
  10. reboot_timeout_default = 30
  11. read_timeout_default = 5
  12. ser = serial.Serial("COM1", 115200, timeout=read_timeout_default)
  13. print "open success:%s bps %d" %(ser.name,ser.baudrate)
  14. def print_cmd_out_serial(fail_keyword_match=""):
  15. while True:
  16. # read line
  17. data = ser.readlines()
  18. # check if line is not empty
  19. if not data:
  20. if fail_keyword_match!="":
  21. continue
  22. else:
  23. break
  24. else:
  25. for line in data:
  26. line = line.strip()
  27. print "%s" % (line)
  28. if fail_keyword_match!="" and re.match(fail_keyword_match,line):
  29. return False
  30. break
  31. return True
  32. def mount_check():
  33. print("send mount now")
  34. sendcmd="mount\n"
  35. ser.write(sendcmd)
  36. return print_cmd_out_serial("/dev/mmcblk0p1 on /flash")
  37. def mount_flash():
  38. print("send echo 3 > /proc/sys/vm/drop_caches now")
  39. sendcmd="echo 3 > /proc/sys/vm/drop_caches\n"
  40. ser.write(sendcmd)
  41. print("send mount /dev/mmcblk0p1 /flash now")
  42. sendcmd="mount /dev/mmcblk0p1 /flash\n"
  43. ser.write(sendcmd)
  44. return print_cmd_out_serial("failed")
  45. def dd_test(bs,count,skip,readtimeout,isbackgroud=False):
  46. #sendcmd = "rm -f /tmp/data \n"
  47. #print("send %s "%(sendcmd))
  48. #ser.write(sendcmd)
  49. sendcmd = "dd if=/dev/mmcblk0p1 bs=%s count=%d skip=%d of=/tmp/data \n" %(bs,count,skip)
  50. print("send readtimeout=%d : %s"%(readtimeout,sendcmd))
  51. ser.write(sendcmd)
  52. ser.timeout = readtimeout
  53. print_cmd_out_serial()
  54. ser.timeout = read_timeout_default ### reset
  55. if(isbackgroud==False):
  56. sendcmd = "dd if=/tmp/data of=/flash/data ;sync \n"
  57. else:
  58. sendcmd = "dd if=/tmp/data of=/flash/data%d_%d_%d ;sync &\n" %(bs,count,skip)
  59. print("send %s"%(sendcmd))
  60. ser.write(sendcmd)
  61. print_cmd_out_serial()
  62. return True
  63. def reboot():
  64. print("send reboot now")
  65. sendcmd="reboot\n"
  66. ser.write(sendcmd)
  67. def kreatv_dd_test():
  68. bs = 1
  69. count = 2*1024*1024
  70. skip = 0x438
  71. readtimeout = read_timeout_default
  72. for i in range(1,20):
  73. bs = random.randint(1,100)
  74. count = 2*1024*1024/bs +random.randint(1,500)
  75. skip = i*random.randint(1,100) + 1024*bs
  76. ##readtimeout = (bs*count/1024/1024%300) * 2 ### max wait for 5min *2
  77. ##if(readtimeout == 0):
  78. ## readtimeout = read_timeout_default
  79. print time.asctime(time.localtime(time.time())), "dd test No. ", i
  80. dd_test(bs,count,skip,read_timeout_default,random.choice([True, False]))
  81. def kreattv():
  82. starttime = time.asctime(time.localtime(time.time()))
  83. while ser.isOpen():
  84. print starttime, "Start Time"
  85. reboot()
  86. print time.asctime(time.localtime(time.time())), "Reboot ===> Sleep"
  87. time.sleep(reboot_timeout_default + random.randint(1,10))
  88. while True:
  89. # read line
  90. data = ser.readlines()
  91. # check if line is not empty
  92. if not data:
  93. break
  94. else:
  95. for line in data:
  96. line = line.strip()
  97. print "%s" % (line)
  98. sendcmd="uptime\n"
  99. print("send %s" %sendcmd)
  100. ser.write(sendcmd)
  101. print_cmd_out_serial("min")
  102. if(mount_flash()==False):
  103. print time.asctime(time.localtime(time.time())), "!!! Got corrupt, stop now!! "
  104. ser.close()
  105. os._exit(1)
  106. kreatv_dd_test()
  107. print time.asctime(time.localtime(time.time())), "One loop is done"
  108. print time.asctime(time.localtime(time.time())), "Completely Stop Close"
  109. ser.close()
  110. if __name__ == "__main__":
  111. kreattv()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/180792?site
推荐阅读
相关标签
  

闽ICP备14008679号