debug_serial.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding:utf-8 -*-
  2. import os, sys, time
  3. import thread
  4. '''
  5. 工程师调试的串口客户端
  6. '''
  7. from ssat_sdk.utils.serialboard_manager import TCLSerial
  8. class DebugSerial():
  9. def __init__(self):
  10. self.serialTool = TCLSerial()
  11. self.showLogTag = True
  12. def __del__(self):
  13. self.serialTool.close()
  14. '''
  15. 在电视端执行串口命令
  16. :param command:指令
  17. :return 无
  18. '''
  19. def excuteCmd(self, command):
  20. self.serialTool.sendCommand(command)
  21. ret = self.serialTool.readData(1024)
  22. print "excuteCmd, return:",ret
  23. '''
  24. 另起一个线程,在终端显示日志。
  25. '''
  26. def showLogThread(self):
  27. thread.start_new_thread(self.showLog,())
  28. def showLog(self):
  29. self.showLogTag = True
  30. while self.showLogTag is True:
  31. ret = self.serialTool.readLine()
  32. if ret.__len__() > 0:
  33. print ret
  34. '''
  35. 关闭日志,无论终端显示,或者保存到文件
  36. '''
  37. def closeLog(self):
  38. self.showLogTag = False
  39. '''
  40. 另起一个线程,保存日志到指定文件
  41. :param filePath: 日志文件路径
  42. '''
  43. def saveLogToFileThread(self, filePath):
  44. thread.start_new_thread(self.saveLogToFile,(filePath,))
  45. def saveLogToFile(self, filePath):
  46. with open(filePath, "w") as logFile:
  47. while self.showLogTag is True:
  48. retLine = self.serialTool.readLine()
  49. logFile.write(retLine)
  50. logFile.flush()
  51. logFile.close()
  52. if __name__ == "__main__":
  53. serial = DebugSerial()
  54. serial.showLogThread()