led_protocol.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import socket
  2. import datetime
  3. import time
  4. font_library = {
  5. 'CN32':'\\FO000', # 32X32中文字体
  6. 'CN48':'\\FO001',# 48X48中文字体
  7. 'EN32': '\\FE000',# 16X32英文字体
  8. 'EN48': '\\FE001'# 24X48英文字体
  9. }
  10. string_color = {
  11. "UNKNOWN": '', #
  12. "RED": '\\C1', # 红色, 默认
  13. "GREEN": '\\C2', # 绿色
  14. "YELLOW": '\\C3', # 黄色
  15. "BLUE": '\\C4', # 蓝色
  16. "CYAN": '\\C5', # 青色
  17. "PURPLE": '\\C6', # 紫色
  18. "WHITE": '\\C7' # 白色
  19. }
  20. class Area:
  21. def __init__(self,area_index,x,y,width,height):
  22. self.area_index = area_index
  23. self.x = x
  24. self.y = y
  25. self.width = width
  26. self.height = height
  27. class LedProtocol:
  28. def __init__(self,area_list):
  29. self.area_dict = {}
  30. for area in area_list.keys():
  31. self.area_dict[area] = Area(area,area_list[area][0],area_list[area][1],area_list[area][2],area_list[area][3])
  32. def string2bytes(self,area_index,input,en_font,cn_font):
  33. input = en_font+cn_font+input
  34. # area_index = 0 #显示区域编号
  35. # width = 170 # 显示区域宽,单位像素
  36. # height = 64 # 显示区域高,单位像素
  37. # x = area_index * width + area_index # 显示区域左上角x,单位像素
  38. # y = 0 # 显示区域左上角y,单位像素
  39. # msg = bytearray(input, encoding='utf-8')
  40. msg = bytearray(input, encoding='gbk')
  41. m_frame_head = bytearray(8) # 帧头, 默认8个0xA5
  42. m_frame_head[0:8] = b'\xA5\xA5\xA5\xA5\xA5\xA5\xA5\xA5'
  43. m_frame_configuration = bytearray(14) # 包头, 配置参数, 14个byte
  44. m_frame_configuration[0:12] = b'\xfe\xff\x00\x80\x00\x00\x00\x00\x00\x00\x63\x02'
  45. # m_frame_configuration 最后2个是 数据域长度,后面在写
  46. m_cmd_head = bytearray(7) # 指令头, 默认7个byte
  47. m_cmd_head[0:7] = b'\xA3\x06\x01\x00\x00\x00\x01'
  48. m_area_head = bytearray(29) # 区域头, 默认29个byte
  49. # m_area_head 前面2个是 区域的数据长度, 后面在写
  50. m_area_head[2] = 0
  51. t_area_x = 0x8000 + self.area_dict[area_index].x # 区域 X 坐标,默认以字节(8 个像素点)为单位, 高字节最高位为 1 时,表示以像素点为单位
  52. m_area_head[3] = t_area_x % 256
  53. m_area_head[4] = int(t_area_x / 256)
  54. t_area_y = self.area_dict[area_index].y # 区域 Y 坐标,以像素点为单位
  55. m_area_head[5] = t_area_y % 256
  56. m_area_head[6] = int(t_area_y / 256)
  57. t_area_width = 0x8000 + self.area_dict[area_index].width # 区域宽度,默认以字节(8 个像素点)为单位, 高字节最高位为 1 时,表示以像素点为单位
  58. m_area_head[7] = t_area_width % 256
  59. m_area_head[8] = int(t_area_width / 256)
  60. t_area_height = self.area_dict[area_index].height # 区域高度,以像素点为单位
  61. m_area_head[9] = t_area_height % 256
  62. m_area_head[10] = int(t_area_height / 256)
  63. m_area_head[11] = self.area_dict[area_index].area_index
  64. m_area_head[12:25] = b'\x00\x00\x02\x00\x00\x00\x00\x02\x01\x01\x00\x00\x0a'
  65. m_area_head[18] = 8 #居中对齐
  66. m_area_head[19] = 2 #多行显示
  67. m_area_head[20] = 1 #手动换行
  68. m_area_head[21] = 1 #0x01——静止显示
  69. # m_area_head[21] = 5 # 0x05——向上移动
  70. # m_area_head后面4个是 显示字符串 的长度, 后续在写.
  71. # 长度整合
  72. t_show_string_len = len(msg)
  73. m_area_head[25] = t_show_string_len % 256
  74. m_area_head[26] = (int(t_show_string_len / 256)) % 256
  75. m_area_head[27] = (int(t_show_string_len / 256 / 256)) % 256
  76. m_area_head[28] = (int(t_show_string_len / 256 / 256 / 256)) % 256
  77. t_area_data_len = 29 - 2 + len(msg)
  78. m_area_head[0] = t_area_data_len % 256
  79. m_area_head[1] = int(t_area_data_len / 256)
  80. t_data_len = 7 + 29 + len(msg)
  81. m_frame_configuration[12] = t_data_len % 256
  82. m_frame_configuration[13] = int(t_data_len / 256)
  83. # crc校验码
  84. t_result_src = m_frame_configuration + m_cmd_head + m_area_head + msg
  85. crc = self.crc16(t_result_src)
  86. [a1, a2] = self.dec2hex(crc)
  87. m_frame_crc = bytearray(2) # crc校验码, 2个byte, 包校验为包头数据和数据域的校验值
  88. m_frame_crc[0] = a1
  89. m_frame_crc[1] = a2
  90. p_src = t_result_src + m_frame_crc
  91. p_dst = bytearray(1024)
  92. j = 0
  93. for i in range(len(p_src)):
  94. if p_src[i] == 0xA5:
  95. p_dst[j] = 0xA6
  96. p_dst[j + 1] = 0x02
  97. j += 2
  98. elif p_src[i] == 0xA6:
  99. p_dst[j] = 0xA6
  100. p_dst[j + 1] = 0x01
  101. j += 2
  102. elif p_src[i] == 0x5A:
  103. p_dst[j] = 0x5B
  104. p_dst[j + 1] = 0x02
  105. j += 2
  106. elif p_src[i] == 0x5B:
  107. p_dst[j] = 0x5B
  108. p_dst[j + 1] = 0x01
  109. j += 2
  110. else:
  111. p_dst[j] = p_src[i]
  112. j = j + 1
  113. m_frame_tail = bytearray(1)
  114. m_frame_tail[0] = 90 # 就是0x5A
  115. output = m_frame_head + p_dst[0:j] + m_frame_tail
  116. # print('--------------------------------')
  117. # print(output)
  118. # print(len(output))
  119. return output
  120. def crc16(self, bytes):
  121. crc = 0
  122. for byte in bytes:
  123. crc = byte ^ crc
  124. for i in range(8):
  125. if crc & 0x01 == 1:
  126. crc = (crc >> 1) ^ 0xA001
  127. else:
  128. crc = crc >> 1
  129. return crc
  130. def dec2hex(self, num):
  131. a = int(num / 256)
  132. b = int(num % 256)
  133. return [b, a]
  134. # AREA_LIST =[
  135. # [0,0,0,170,64],#区域编号,区域左上角X坐标,区域左上角Y坐标,区域宽度,区域高度
  136. # [1,175,0,193,64],#区域编号,区域左上角X坐标,区域左上角Y坐标,区域宽度,区域高度
  137. # [2,371,0,205,64]#区域编号,区域左上角X坐标,区域左上角Y坐标,区域宽度,区域高度
  138. # ]
  139. #
  140. # if __name__=="__main__":
  141. # led = LedControl("192.168.1.100",5005,AREA_LIST)
  142. # led.sendLedMsg(0,string_color["WHITE"]+"A1入口",font_library["EN48"],font_library["CN48"])
  143. # led.sendLedMsg(1,"%s空闲 可进入\\n%s限高: 2.00"%(string_color["GREEN"],string_color["BLUE"]))
  144. # led.sendLedMsg(2,"%s剩大车位:%d\\n%s剩小车位:%d"%(string_color["RED"],0,string_color["GREEN"],100))
  145. # sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  146. # sock.settimeout(1)
  147. # sock.connect(("192.168.1.163",5005))
  148. #
  149. # print("connected")
  150. #
  151. #
  152. # time.sleep(1)
  153. # bytes=string2bytes(0, '楚天车库', string_color["RED"])
  154. # sock.send(bytes)
  155. # time.sleep(1)
  156. # bytes=string2bytes(1, '停车入口', string_color["GREEN"])
  157. # sock.send(bytes)
  158. # time.sleep(1)
  159. # bytes=string2bytes(2, '暂停服务', string_color["YELLOW"])
  160. # sock.send(bytes)
  161. # #bytes=string2bytes(3, '\\C1暂停\\C2服务', string_color["UNKNOWN"])
  162. # #sock.send(bytes)
  163. #
  164. #
  165. # sock.close()
  166. # print("end")