debug_serial.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def excuteCmdlog(self, command):
  24. self.serialTool.sendCommand(command)
  25. ret = self.serialTool.readData(1024)
  26. print "excuteCmd, return:",ret
  27. return ret
  28. '''
  29. 另起一个线程,在终端显示日志。
  30. '''
  31. def showLogThread(self):
  32. thread.start_new_thread(self.showLog,())
  33. def showLog(self):
  34. self.showLogTag = True
  35. while self.showLogTag is True:
  36. ret = self.serialTool.readLine()
  37. if ret.__len__() > 0:
  38. print ret
  39. '''
  40. 关闭日志,无论终端显示,或者保存到文件
  41. '''
  42. def closeLog(self):
  43. self.showLogTag = False
  44. '''
  45. 另起一个线程,保存日志到指定文件
  46. :param filePath: 日志文件路径
  47. '''
  48. def saveLogToFileThread(self, filePath):
  49. thread.start_new_thread(self.saveLogToFile,(filePath,))
  50. def saveLogToFile(self, filePath):
  51. with open(filePath, "w") as logFile:
  52. while self.showLogTag is True:
  53. retLine = self.serialTool.readLine()
  54. logFile.write(retLine)
  55. logFile.flush()
  56. logFile.close()
  57. if __name__ == "__main__":
  58. serial = DebugSerial()
  59. serial.showLogThread()