led.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import socket
  2. import time
  3. string_color = {
  4. "UNKNOWN":'', #
  5. "RED":'\\C1', #红色, 默认
  6. "GREEN":'\\C2', #绿色
  7. "YELLOW":'\\C3', #黄色
  8. "BLUE":'\\C4', #蓝色
  9. "CYAN":'\\C5', #青色
  10. "PURPLE":'\\C6', #紫色
  11. "WHITE":'\\C7' #白色
  12. }
  13. class Led31():
  14. def __init__(self,ip,port):
  15. self.ip=ip
  16. self.port=port
  17. def display(self,area_index, input, color):
  18. self.sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. self.sock.settimeout(1)
  20. try:
  21. self.sock.connect((self.ip,self.port))
  22. self.sock.send(self.generate_bytes(area_index, input, color))
  23. self.sock.close()
  24. except:
  25. pass
  26. time.sleep(0.01)
  27. def generate_bytes(self,area_index, input, color):
  28. input = color + input
  29. # area_index = 0 #显示区域编号
  30. x = 0 #显示区域左上角x,单位像素
  31. y = area_index*17 #显示区域左上角y,单位像素
  32. width = 79 #显示区域宽,单位像素
  33. heigth = 16 #显示区域高,单位像素
  34. #msg = bytearray(input, encoding='utf-8')
  35. msg = bytearray(input, encoding='gbk')
  36. m_frame_head = bytearray(8) #帧头, 默认8个0xA5
  37. m_frame_head[0:8] = b'\xA5\xA5\xA5\xA5\xA5\xA5\xA5\xA5'
  38. m_frame_configuration = bytearray(14) #包头, 配置参数, 14个byte
  39. m_frame_configuration[0:12]=b'\xfe\xff\x00\x80\x00\x00\x00\x00\x00\x00\x63\x02'
  40. # m_frame_configuration 最后2个是 数据域长度,后面在写
  41. m_cmd_head = bytearray(7) #指令头, 默认7个byte
  42. m_cmd_head[0:7] = b'\xA3\x06\x01\x00\x00\x00\x01'
  43. m_area_head = bytearray(29) #区域头, 默认29个byte
  44. # m_area_head 前面2个是 区域的数据长度, 后面在写
  45. m_area_head[2] = 0
  46. t_area_x = 0x8000+x # 区域 X 坐标,默认以字节(8 个像素点)为单位, 高字节最高位为 1 时,表示以像素点为单位
  47. m_area_head[3]=t_area_x%256
  48. m_area_head[4]=int(t_area_x/256)
  49. t_area_y = y #区域 Y 坐标,以像素点为单位
  50. m_area_head[5]=t_area_y%256
  51. m_area_head[6]=int(t_area_y/256)
  52. t_area_width = 0x8000+width #区域宽度,默认以字节(8 个像素点)为单位, 高字节最高位为 1 时,表示以像素点为单位
  53. m_area_head[7]=t_area_width%256
  54. m_area_head[8]=int(t_area_width/256)
  55. t_area_height = heigth #区域高度,以像素点为单位
  56. m_area_head[9]=t_area_height%256
  57. m_area_head[10]=int(t_area_height/256)
  58. m_area_head[11] = area_index
  59. m_area_head[12:25] = b'\x00\x00\x02\x00\x00\x00\x00\x02\x01\x01\x00\x00\x0a'
  60. #m_area_head[21] = 1 #0x01——静止显示
  61. #m_area_head[21] = 5 #0x05——向上移动
  62. #m_area_head后面4个是 显示字符串 的长度, 后续在写.
  63. #长度整合
  64. t_show_string_len = len(msg)
  65. m_area_head[25] = t_show_string_len%256
  66. m_area_head[26]=(int(t_show_string_len/256))%256
  67. m_area_head[27]=(int(t_show_string_len/256/256))%256
  68. m_area_head[28]=(int(t_show_string_len/256/256/256))%256
  69. t_area_data_len = 29-2+ len(msg)
  70. m_area_head[0]=t_area_data_len%256
  71. m_area_head[1]=int(t_area_data_len/256)
  72. t_data_len = 7 + 29 + len(msg)
  73. m_frame_configuration[12] = t_data_len%256
  74. m_frame_configuration[13] = int(t_data_len/256)
  75. #crc校验码
  76. t_result_src = m_frame_configuration + m_cmd_head + m_area_head +msg
  77. crc=self.crc16(t_result_src)
  78. [a1,a2]=self.dec2hex(crc)
  79. m_frame_crc = bytearray(2) #crc校验码, 2个byte, 包校验为包头数据和数据域的校验值
  80. m_frame_crc[0]=a1
  81. m_frame_crc[1]=a2
  82. p_src = t_result_src + m_frame_crc
  83. p_dst = bytearray(1024)
  84. i=0
  85. j=0
  86. for i in range(len(p_src)):
  87. if p_src[i] == 0xA5:
  88. p_dst[j] = 0xA6
  89. p_dst[j+1] = 0x02
  90. j+=2
  91. elif p_src[i] == 0xA6:
  92. p_dst[j] = 0xA6
  93. p_dst[j+1] = 0x01
  94. j+=2
  95. elif p_src[i] == 0x5A:
  96. p_dst[j] = 0x5B
  97. p_dst[j+1] = 0x02
  98. j+=2
  99. elif p_src[i] == 0x5B:
  100. p_dst[j] = 0x5B
  101. p_dst[j+1] = 0x01
  102. j+=2
  103. else:
  104. p_dst[j] = p_src[i]
  105. j = j+1
  106. m_frame_tail = bytearray(1)
  107. m_frame_tail[0] = 90 #就是0x5A
  108. output = m_frame_head + p_dst[0:j] + m_frame_tail
  109. return output
  110. def dec2hex(self,num):
  111. a=int(num/256)
  112. b=int(num%256)
  113. return [b,a]
  114. def crc16(self,bytes):
  115. crc=0
  116. for byte in bytes:
  117. crc=byte^crc
  118. for i in range(8):
  119. if crc&0x01==1:
  120. crc=(crc>>1)^0xA001
  121. else:
  122. crc=crc>>1
  123. return crc
  124. class EntranceLED(Led31):
  125. def __init__(self,ip,port):
  126. Led31.__init__(self,ip,port)
  127. self.last_suv={"color":string_color["RED"],"string":"剩SUV 0"}
  128. self.last_small={"color":string_color["RED"],"string":"剩轿车 0"}
  129. self.last_idle={"color":string_color["GREEN"],"string":"初始化"}
  130. def fresh_led(self,suv,small,entrance_busy):
  131. suv_info={}
  132. small_info={}
  133. idle_info={}
  134. if suv<=0:
  135. suv_info["color"]=string_color["RED"]
  136. elif entrance_busy==True:
  137. suv_info["color"]=string_color["YELLOW"]
  138. else:
  139. suv_info["color"]=string_color["GREEN"]
  140. if small<=0:
  141. small_info["color"]=string_color["RED"]
  142. elif entrance_busy==True:
  143. small_info["color"]=string_color["YELLOW"]
  144. else:
  145. small_info["color"]=string_color["GREEN"]
  146. suv_info["string"]='剩SUV %02d'%suv
  147. small_info["string"]='剩轿车 %02d'%small
  148. if not self.last_suv["color"]==suv_info["color"] or not self.last_suv["string"]==suv_info["string"]:
  149. self.display(0,suv_info["string"],suv_info["color"])
  150. self.last_suv=suv_info
  151. if not self.last_small["color"]==small_info["color"] or not self.last_small["string"]==small_info["string"]:
  152. self.display(1,small_info["string"],small_info["color"])
  153. self.last_small=small_info
  154. if suv<=0 and small<=0:
  155. idle_info["color"]=string_color["RED"]
  156. idle_info["string"]="车位已满"
  157. elif entrance_busy==True:
  158. idle_info["color"]=string_color["YELLOW"]
  159. idle_info["string"]="有车请等待"
  160. else:
  161. idle_info["color"]=string_color["GREEN"]
  162. idle_info["string"]="空闲可进入"
  163. if not self.last_idle["color"]==idle_info["color"] or not self.last_idle["string"]==idle_info["string"]:
  164. self.display(2,idle_info["string"],idle_info["color"])
  165. self.last_idle=idle_info