tcl_factory_product.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # -*- coding: utf-8 -*-
  2. import sys,os,time
  3. reload(sys)
  4. sys.setdefaultencoding('utf-8')
  5. import struct,binascii
  6. import threading
  7. from utils.serialboard_manager import *
  8. from tcl_factory_transfer import *
  9. #用于进行命令传输层的命令格式化
  10. class TCLFactoryCommand():
  11. Encoding = "ascii"
  12. Packet_Head_High = 0xAF
  13. """
  14. 其中:包引导码为命令的开始,用于自调设备发送使用:
  15. 0xAA --- 调试用命令代码引导码
  16. 0xAC --- 软件配屏参数调整命令代码引导码
  17. 0xAE --- **保留命令发送类型引导码**
  18. 用于待调试电视返回命令使用:
  19. 0xAB --- 调试用命令返回引导码
  20. 0xAD --- 软件配屏命令返回引导码
  21. 0xAF --- **保留命令返回类型引导码**
  22. """
  23. Head_TV_Debug = 0xAA
  24. Head_TV_Return = 0xAB
  25. Head_TV_Panel_Debug = 0xAC
  26. Head_TV_Panel_Return = 0xAD
  27. Head_TV_Debug_Other = 0xAE
  28. Head_TV_Other_Return = 0xAF
  29. FE_LEN_FLAG = 0xFE
  30. Packet_Head_Offset = 0
  31. Packet_Len_Offset = 1
  32. Packet_uCommand_Offset = 2
  33. Packet_CRC_Len = 2
  34. Packet_Head_Len = 1
  35. Packet_Len_Len = 1
  36. Packet_uCommand_Len = 1
  37. Packet_Min_Len = Packet_CRC_Len + Packet_Head_Len + Packet_Len_Len + Packet_uCommand_Len
  38. def __init__(self):
  39. self.Packet_Head = TCLFactoryCommand.Packet_Head_High & TCLFactoryCommand.Head_TV_Debug
  40. # 包长度为应用层数据字节数的总合+1(包引导码) +1(包长度) +2( CRC16 校验码) =应用层数据字节数总合+4 字节
  41. self.Packet_Length = [Packet_Min_Len]
  42. self.Packet_Command = 0x00
  43. self.CRC1 = 0xFF
  44. self.CRC2 = 0xFF
  45. self.Data = None
  46. #data必须是bytearray
  47. def setCommandData(self, command, data):
  48. self.Packet_Command = command
  49. self.Data = data
  50. if (self.Packet_Length[0] == TCLFactoryCommand.FE_LEN_FLAG):
  51. len = self.Data.__len__() + self.Packet_Length.__len__() + TCLFactoryCommand.Packet_Head_Len\
  52. +TCLFactoryCommand.Packet_uCommand_Len + TCLFactoryCommand.Packet_CRC_Len
  53. self.Packet_Length[1] = len >> 8
  54. self.Packet_Length[2] = len & 0xFF
  55. else:
  56. self.Packet_Length[0] = self.Packet_Length[0] + self.Data.__len__()
  57. #print "[TCLFactoryCommand:setCommandData],Packet_Length=",self.Packet_Length
  58. def setCommandHead(self, head):
  59. self.Packet_Head = TCLFactoryCommand.Packet_Head_High & head
  60. def changeFELenType(self):
  61. self.Packet_Length = [TCLFactoryCommand.FE_LEN_FLAG,0x00,0x00]
  62. def getCommandByteArray(self):
  63. bytePre = [self.Packet_Head]+ self.Packet_Length + [self.Packet_Command]
  64. byteCRC = [self.CRC1, self.CRC2]
  65. return bytearray(bytePre) + self.Data + bytearray(byteCRC)
  66. class SerialReadThread(threading.Thread):
  67. def __init__(self,threadID, counter, factory, name="SerialReadThread"):
  68. threading.Thread.__init__(self)
  69. self.threadID = threadID
  70. self.name = name
  71. self.counter = counter
  72. self.factory = factory
  73. self.transfer = factory.factoryTransfer
  74. def printThreadInfo(self):
  75. print "ThreadID:", self.threadID, ";Thread Name:", self.name
  76. def run(self):
  77. global Command_Status,Command_Result_Data,Command_Result
  78. print "\nRun SerialReadThread,Current status=",Command_Status
  79. while(Factory_Status == Factory_Status_Open):
  80. while(Command_Status == Status_Command_Return_Wait):
  81. result, line = self.transfer.readResultLine()
  82. self.factory.parseCommandResult(line)
  83. if (line.__len__() >= 5):
  84. Command_Status = Status_Command_Send
  85. print "[SerialReadThread:run],Command_Result=0x%x" % Command_Result
  86. print "\nExit SerialReadThread"
  87. Status_Init = 0x00
  88. Status_Command_Send = 0x01
  89. Status_Command_Return_Wait = 0x02
  90. Command_Status = Status_Init
  91. Command_Result_Return_Pass = 0x0A
  92. Command_Result_Return_Fail = 0x0E
  93. Command_Result = 0x00
  94. #有些指令的回馈结果,此时回馈的数据有两条指令
  95. #第一条是端长度的指令,告诉指令是否被成功执行。第二条回馈指令,返回想要获取的数据
  96. #例如查看projectID。ab050adf4e ab0785012ea08d
  97. Command_Result_Data = None
  98. Command_Return_Command = None
  99. Factory_Status_Open = 0x01
  100. Factory_Status_Close = 0x00
  101. Factory_Status = Factory_Status_Close
  102. ThreadID_SerialRead = 0x01
  103. Thread_Counter = 1
  104. Try_Count_Max = 3
  105. class TCLFactory():
  106. def __init__(self):
  107. global Thread_Counter
  108. self.Factory_Model_ON = False
  109. self.Command_Excute_Counter = 0
  110. self.initTransfer()
  111. self.openFactory()
  112. def openTCLFactroyModel(self):
  113. print "Open Factory model"
  114. self.sendCommandToTV(0x10,bytearray([0x01]))
  115. def closeTCLFactoryModel(self):
  116. print "Close Factory model"
  117. self.sendCommandToTV(0x10, bytearray([0x00]))
  118. def initTransfer(self):
  119. self.factoryTransfer = TCLFactoryTransfer()
  120. def runReadThread(self):
  121. global Thread_Counter, Command_Status
  122. try:
  123. print ""
  124. Command_Status = Status_Init
  125. self.serialReadThread = SerialReadThread(ThreadID_SerialRead, Thread_Counter, self)
  126. self.serialReadThread.printThreadInfo()
  127. self.serialReadThread.start()
  128. Thread_Counter += 1
  129. except:
  130. print "Error:Could not start SerialReadThread"
  131. def packCommandData(self, command, data, FELen = False):
  132. print "[TCLFactory:packCommandData]command=0x%x"%command ,";data=", data
  133. factoryCommand = TCLFactoryCommand()
  134. if (FELen == True):
  135. factoryCommand.changeFELenType()
  136. factoryCommand.setCommandData(command, data)
  137. return factoryCommand
  138. #line:是返回命令的整个字节数组
  139. def parseCommandResult(self, result):
  140. global Command_Result,Command_Result_Data
  141. print "parseCommandResult,result:", binascii.hexlify(result)
  142. if (result.__len__() < TCLFactoryCommand.Packet_Min_Len):
  143. return
  144. packet_head = result[Packet_Head_Offset]
  145. if (TCLFactoryCommand.Head_TV_Return == packet_head):
  146. result_len = result[Packet_Len_Offset]
  147. #获取反馈回来的数据,例如需要获取的ProjectID
  148. if (result.__len__() > TCLFactoryCommand.Packet_Min_Len*2) and (result_len == TCLFactoryCommand.Packet_Min_Len):
  149. Command_Result = result[Packet_uCommand_Offset]
  150. second_offset_begin = TCLFactoryCommand.Packet_Min_Len
  151. second_offset_end = result.__len__()-1
  152. self.parseCommandResult(result[second_offset_begin:second_offset_end])
  153. elif (result_len > TCLFactoryCommand.Packet_Min_Len):
  154. Command_Result = Command_Result_Return_Pass
  155. data_offset_begin = TCLFactoryCommand.Packet_uCommand_Offset+1
  156. data_offset_end = data_offset_begin + (result_len - TCLFactoryCommand.Packet_Min_Len)
  157. Command_Result_Data = result[data_offset_begin:data_offset_end]
  158. else:
  159. Command_Result = result[Packet_uCommand_Offset]
  160. else:
  161. Command_Result = Command_Result_Return_Fail
  162. """
  163. 发送指令后,进入结果等待。出现3次错误回馈,返回Fail。反之,表示命令成功。
  164. """
  165. def sendCommandToTV(self, command, data, FELen = False):
  166. global Command_Status,Command_Result
  167. Command_Result = 0x00
  168. Command_Status = Status_Command_Return_Wait
  169. self.Command_Excute_Counter += 1
  170. print "Begin to wait to read.Command_Status=0x%x"%Command_Status, "; apicounter=", Command_Excute_Counter
  171. time.sleep(1)
  172. flag = FELen
  173. factoryCommand = self.packCommandData(command, data, FELen = flag)
  174. factoryCommand.setCommandHead(TCLFactoryCommand.Head_TV_Debug)
  175. commandByteArr = factoryCommand.getCommandByteArray()
  176. self.factoryTransfer.sendCommandToTV(commandByteArr)
  177. if (Command_Result == Command_Result_Return_Pass):
  178. print "Command Excute Pass"
  179. Command_Status = Status_Command_Send
  180. self.Command_Excute_Counter = 0
  181. return True
  182. if (Command_Result == Command_Result_Return_Fail):
  183. if (self.Command_Excute_Counter == Try_Count_Max):
  184. print "Command Excute Fail"
  185. Command_Status = Status_Command_Send
  186. self.Command_Excute_Counter = 0
  187. return False
  188. else:
  189. return self.sendCommandToTV(command,data)
  190. #没有回馈结果的情况
  191. Command_Status = Status_Command_Send
  192. self.Command_Excute_Counter = 0
  193. print "End to wait to read.Command_Status=", Command_Status, "; counter=", Command_Excute_Counter
  194. return False
  195. #返回前一条指令的回馈报文结果:ucCommand,ucData。
  196. def readCommandResult(self):
  197. print "[TCLFactory:readCommandResult]"
  198. if (Command_Result_Data <> None):
  199. print "[TCLFactory:readCommandResult],Command_Result=0x%x"%Command_Result, ";Command_Result_Data:", binascii.hexlify(Command_Result_Data)
  200. if (Command_Result == Command_Result_Return_Pass):
  201. result = "Pass"
  202. else:
  203. result = "Fail"
  204. return result, Command_Result_Data
  205. def closeFactory(self):
  206. global Factory_Status
  207. Factory_Status = Factory_Status_Close
  208. def openFactory(self):
  209. global Factory_Status
  210. Factory_Status = Factory_Status_Open
  211. self.runReadThread()
  212. if __name__ == "__main__":
  213. factory = TCLFactory()
  214. factory.openTCLFactroyModel()
  215. #factory.sendCommandToTV(0x25,0x02)
  216. factory.closeFactory()