emv_ctl.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @文件 :bms_pcs_ctl.py
  5. @时间 :2022/02/21 16:56:26
  6. @作者 :None
  7. @版本 :1.0
  8. @说明 :主页面 CTL
  9. '''
  10. from sys import argv
  11. from utils.globalvar import SD, EmvState
  12. from utils.qt import QApplication, QThread
  13. from controller.emv_status_ctl import EmvStatusControll
  14. from widget.emv_home import Win_Emv_Home
  15. from worker.emv_work import EmvCanReceived, EmvComWork
  16. from utils.utils import calculate_crc_8, a_bit
  17. import serial.tools.list_ports
  18. class EmvControll:
  19. def __init__(self):
  20. self._app = QApplication(argv)
  21. self._view = Win_Emv_Home()
  22. self._model = EmvState()
  23. self.init()
  24. def init(self):
  25. self.port_check()
  26. self.emv_status = EmvStatusControll()
  27. self._view.setStatusBar(self.emv_status._view.statusbar)
  28. self._view.interface_signal.connect(self.interface_chose)
  29. self._view.cb_bms.currentIndexChanged.connect(self._bms_id)
  30. self._view.connect_signal.connect(self.emv_connect)
  31. self._view.disconnect_signal.connect(self.emv_disconnect)
  32. self._view.emv_on_signal.connect(self.emv_on)
  33. self._view.emv_off_signal.connect(self.emv_off)
  34. def _bms_id(self):
  35. SD.EMV_ID = int(self._view.cb_bms.currentText())
  36. def emv_on(self):
  37. if SD.CAN_ON_OFF:
  38. data = [0, 0, 0, 0, 0, 0, 0, 0xAA]
  39. if self._view.cb_cls.currentIndex() == 0:
  40. data[1] = 0xff
  41. data[2] = 0x01
  42. elif self._view.cb_cls.currentIndex() <= 8:
  43. data[1] = 1 << self._view.cb_cls.currentIndex() - 1
  44. else:
  45. data[2] = 1 << (self._view.cb_cls.currentIndex() - 9)
  46. self._model.emv_1_state = a_bit(data[1], 1)
  47. self._model.emv_2_state = a_bit(data[1], 2)
  48. self._model.emv_3_state = a_bit(data[1], 3)
  49. self._model.emv_4_state = a_bit(data[1], 4)
  50. self._model.emv_5_state = a_bit(data[1], 5)
  51. self._model.emv_6_state = a_bit(data[1], 6)
  52. self._model.emv_7_state = a_bit(data[1], 7)
  53. self._model.emv_8_state = a_bit(data[1], 8)
  54. self._model.emv_9_state = a_bit(data[2], 1)
  55. if self._view.cb_interface.currentIndex() == 0:
  56. data[0] = calculate_crc_8(0x18CF0000 | SD.EMV_ID, data[1:])
  57. SD.CAN_CONTROL.send(0x18CF0000 | SD.EMV_ID, data, extern_flag=True)
  58. elif self._view.cb_interface.currentIndex() == 1:
  59. SD.COM_CONTROL.send_16h(0x01, 0x0064, 0x0003, 3, [data[1], data[2], data[7]])
  60. self._emv_ce_signal(data, 1)
  61. else:
  62. return
  63. def emv_off(self):
  64. if SD.CAN_ON_OFF:
  65. data = [0, 0xFF, 1, 0, 0, 0, 0, 0xFF]
  66. if self._view.cb_cls.currentIndex() == 0:
  67. data[1] = 0
  68. data[2] = 0
  69. elif self._view.cb_cls.currentIndex() <= 8:
  70. data[1] = (~ (1 << self._view.cb_cls.currentIndex() - 1)) & 0xFF
  71. else:
  72. data[2] = 0
  73. self._model.emv_1_state = a_bit(data[1], 1)
  74. self._model.emv_2_state = a_bit(data[1], 2)
  75. self._model.emv_3_state = a_bit(data[1], 3)
  76. self._model.emv_4_state = a_bit(data[1], 4)
  77. self._model.emv_5_state = a_bit(data[1], 5)
  78. self._model.emv_6_state = a_bit(data[1], 6)
  79. self._model.emv_7_state = a_bit(data[1], 7)
  80. self._model.emv_8_state = a_bit(data[1], 8)
  81. self._model.emv_9_state = a_bit(data[2], 1)
  82. if self._view.cb_interface.currentIndex() == 0:
  83. data[0] = calculate_crc_8(0x18CF0000 | SD.EMV_ID, data[1:])
  84. SD.CAN_CONTROL.send(0x18CF0000 | SD.EMV_ID, data, extern_flag=True)
  85. elif self._view.cb_interface.currentIndex() == 1:
  86. SD.COM_CONTROL.send_16h(0x01, 0x0064, 0x0003, 3, [data[1], data[2], data[7]])
  87. self._emv_ce_signal(data, 1)
  88. else:
  89. return
  90. def port_check(self):
  91. com_port = {}
  92. port_list = list(serial.tools.list_ports.comports())
  93. self._view.cb_com_interface_port.clear()
  94. for port in port_list:
  95. com_port["%s" % port_list[0]] = "%s" % port[1]
  96. self._view.cb_com_interface_port.addItem(port[0])
  97. def emv_connect(self):
  98. if self._view.cb_interface.currentIndex() == 0:
  99. self.emv_can_connect()
  100. elif self._view.cb_interface.currentIndex() == 1:
  101. self.emv_com_connect()
  102. else:
  103. return
  104. def emv_com_connect(self):
  105. self._port = self._view.cb_com_interface_port.currentText()
  106. self._baudrate = int(self._view.cb_com_interface_baudrate.currentText())
  107. self._bytesize = int(self._view.cb_com_interface_word_length.currentText())
  108. self._stopbits = int(self._view.cb_com_interface_stop.currentText())
  109. if self._view.cb_com_interface_parity.currentIndex() == 0:
  110. self._parity = 'N'
  111. elif self._view.cb_com_interface_parity.currentIndex() == 1:
  112. self._parity = 'E'
  113. elif self._view.cb_com_interface_parity.currentIndex() == 2:
  114. self._parity = 'O'
  115. else:
  116. self._parity = 'M'
  117. SD.COM_CONTROL.set_com(self._port, self._baudrate, self._bytesize, self._parity, self._stopbits)
  118. SD.CAN_ON_OFF = SD.COM_CONTROL.open_device()
  119. if SD.CAN_ON_OFF:
  120. self.emv_com_connected()
  121. self._view.groupbox_com_interface.setDisabled(True)
  122. self._view.groupbox_interface.setDisabled(True)
  123. else:
  124. self._view.no_com_device()
  125. def emv_com_connected(self):
  126. # Com线程类
  127. self.emv_com_thread = QThread()
  128. self.emv_com_work = EmvComWork()
  129. self.emv_com_work.moveToThread(self.emv_com_thread)
  130. self.emv_com_thread.finished.connect(self.emv_com_work.deleteLater)
  131. self.emv_com_thread.started.connect(self.emv_com_work.work)
  132. self.emv_com_work.show_com_signal.connect(self.emv_com_signal)
  133. # 启动线程
  134. self.emv_com_thread.start()
  135. self.emv_status.emv_connect()
  136. def emv_com_signal(self, v1, v2, v3):
  137. self._view.led_emv_total.set_status(1)
  138. ver = "v" + str(v1) + "." + str(v2) + "." + str(v3)
  139. self._view.edt_emv_version.setText(ver)
  140. def emv_can_connect(self):
  141. self._can_index_var = self._view.cb_can_interface_index.currentText()
  142. self._can_chanel_var = self._view.cb_can_interface_channel.currentText()
  143. self._can_baudrate_var = self._view.cb_can_interface_baudrate.currentText()
  144. can_index = int(self._can_index_var)
  145. can_channel = int(self._can_chanel_var)
  146. can_baudrate = self._can_baudrate_var
  147. SD.CAN_CONTROL.set_can_board(can_index, can_channel, can_baudrate)
  148. try:
  149. if SD.CAN_CONTROL.open_device():
  150. SD.CAN_ON_OFF = SD.CAN_CONTROL.init_can(0x00000000, 0xFFFFFFFF)
  151. if SD.CAN_ON_OFF:
  152. self.emv_can_connected()
  153. self._view.groupbox_can_interface.setDisabled(True)
  154. self._view.groupbox_interface.setDisabled(True)
  155. else:
  156. self._view.can_connect_error()
  157. except AttributeError:
  158. self._view.no_can_device()
  159. def emv_disconnect(self):
  160. if self._view.cb_interface.currentIndex() == 0:
  161. self.emv_can_disconnect()
  162. elif self._view.cb_interface.currentIndex() == 1:
  163. self.emv_com_disconnect()
  164. else:
  165. return
  166. def emv_com_disconnect(self):
  167. if SD.CAN_ON_OFF:
  168. SD.CAN_ON_OFF = False
  169. if self.emv_com_thread.isRunning():
  170. self.emv_com_thread.quit()
  171. self.emv_com_thread.wait()
  172. if self.emv_com_thread.isFinished():
  173. del self.emv_com_work
  174. del self.emv_com_thread
  175. self._view.groupbox_com_interface.setDisabled(False)
  176. self._view.groupbox_interface.setDisabled(False)
  177. self.emv_data_recovery()
  178. self.emv_status.emv_disconnect()
  179. def emv_can_disconnect(self):
  180. if SD.CAN_ON_OFF:
  181. SD.CAN_ON_OFF = False
  182. SD.CAN_CONTROL.clear_buffer()
  183. SD.CAN_CONTROL.close_can()
  184. if self.emv_can_received_thread.isRunning():
  185. self.emv_can_received_thread.quit()
  186. self.emv_can_received_thread.wait()
  187. if self.emv_can_received_thread.isFinished():
  188. del self.emv_can_received
  189. del self.emv_can_received_thread
  190. self._view.groupbox_can_interface.setDisabled(False)
  191. self._view.groupbox_interface.setDisabled(False)
  192. self.emv_status.emv_disconnect()
  193. self.emv_data_recovery()
  194. def emv_can_connected(self):
  195. # CAN数据接收类
  196. self.emv_can_received_thread = QThread()
  197. self.emv_can_received = EmvCanReceived()
  198. self.emv_can_received.moveToThread(self.emv_can_received_thread)
  199. self.emv_can_received_thread.finished.connect(self.emv_can_received.deleteLater)
  200. self.emv_can_received_thread.started.connect(self.emv_can_received.received)
  201. self.emv_can_received.show_emv_ce_signal.connect(self._emv_ce_signal)
  202. self.emv_can_received.show_emv_cf_signal.connect(self._emv_cf_signal)
  203. # 启动线程
  204. self.emv_can_received_thread.start()
  205. self.emv_status.emv_connect()
  206. def _emv_ce_signal(self, data, con_type):
  207. if (data[0] == 0xaa and con_type == 0) or (data[7] == 0xaa and con_type == 1):
  208. if self._view.cb_cls.currentIndex() == 0:
  209. self._view.led_emv_1.set_status(self._model.emv_1_state)
  210. self._view.led_emv_2.set_status(self._model.emv_2_state)
  211. self._view.led_emv_3.set_status(self._model.emv_3_state)
  212. self._view.led_emv_4.set_status(self._model.emv_4_state)
  213. self._view.led_emv_5.set_status(self._model.emv_5_state)
  214. self._view.led_emv_6.set_status(self._model.emv_6_state)
  215. self._view.led_emv_7.set_status(self._model.emv_7_state)
  216. self._view.led_emv_8.set_status(self._model.emv_8_state)
  217. self._view.led_emv_9.set_status(self._model.emv_9_state)
  218. elif self._view.cb_cls.currentIndex() == 1:
  219. self._view.led_emv_1.set_status(self._model.emv_1_state)
  220. elif self._view.cb_cls.currentIndex() == 2:
  221. self._view.led_emv_2.set_status(self._model.emv_2_state)
  222. elif self._view.cb_cls.currentIndex() == 3:
  223. self._view.led_emv_3.set_status(self._model.emv_3_state)
  224. elif self._view.cb_cls.currentIndex() == 4:
  225. self._view.led_emv_4.set_status(self._model.emv_4_state)
  226. elif self._view.cb_cls.currentIndex() == 5:
  227. self._view.led_emv_5.set_status(self._model.emv_5_state)
  228. elif self._view.cb_cls.currentIndex() == 6:
  229. self._view.led_emv_6.set_status(self._model.emv_6_state)
  230. elif self._view.cb_cls.currentIndex() == 7:
  231. self._view.led_emv_7.set_status(self._model.emv_7_state)
  232. elif self._view.cb_cls.currentIndex() == 8:
  233. self._view.led_emv_8.set_status(self._model.emv_8_state)
  234. elif self._view.cb_cls.currentIndex() == 9:
  235. self._view.led_emv_9.set_status(self._model.emv_9_state)
  236. else:
  237. return
  238. elif (data[0] == 0xff and con_type == 0) or (data[7] == 0xff and con_type == 1):
  239. if self._view.cb_cls.currentIndex() == 0:
  240. self._view.led_emv_1.set_status(self._model.emv_1_state)
  241. self._view.led_emv_2.set_status(self._model.emv_2_state)
  242. self._view.led_emv_3.set_status(self._model.emv_3_state)
  243. self._view.led_emv_4.set_status(self._model.emv_4_state)
  244. self._view.led_emv_5.set_status(self._model.emv_5_state)
  245. self._view.led_emv_6.set_status(self._model.emv_6_state)
  246. self._view.led_emv_7.set_status(self._model.emv_7_state)
  247. self._view.led_emv_8.set_status(self._model.emv_8_state)
  248. self._view.led_emv_9.set_status(self._model.emv_9_state)
  249. elif self._view.cb_cls.currentIndex() == 1:
  250. self._view.led_emv_1.set_status(self._model.emv_1_state)
  251. elif self._view.cb_cls.currentIndex() == 2:
  252. self._view.led_emv_2.set_status(self._model.emv_2_state)
  253. elif self._view.cb_cls.currentIndex() == 3:
  254. self._view.led_emv_3.set_status(self._model.emv_3_state)
  255. elif self._view.cb_cls.currentIndex() == 4:
  256. self._view.led_emv_4.set_status(self._model.emv_4_state)
  257. elif self._view.cb_cls.currentIndex() == 5:
  258. self._view.led_emv_5.set_status(self._model.emv_5_state)
  259. elif self._view.cb_cls.currentIndex() == 6:
  260. self._view.led_emv_6.set_status(self._model.emv_6_state)
  261. elif self._view.cb_cls.currentIndex() == 7:
  262. self._view.led_emv_7.set_status(self._model.emv_7_state)
  263. elif self._view.cb_cls.currentIndex() == 8:
  264. self._view.led_emv_8.set_status(self._model.emv_8_state)
  265. elif self._view.cb_cls.currentIndex() == 9:
  266. self._view.led_emv_9.set_status(self._model.emv_9_state)
  267. else:
  268. return
  269. else:
  270. return
  271. def _emv_cf_signal(self, data):
  272. self._view.led_emv_total.set_status(1)
  273. emv_ver = "v" + str(a_bit(data[0], 1)) + "." + str(a_bit(data[0], 2)) + "." + str(a_bit(data[0], 3))
  274. self._view.edt_emv_version.setText(emv_ver)
  275. def run(self):
  276. self._view.show()
  277. return self._app.exec_()
  278. def interface_chose(self, current_interface):
  279. if current_interface == 1:
  280. self._view.groupbox_can_interface.hide()
  281. self._view.groupbox_com_interface.show()
  282. else:
  283. self._view.groupbox_com_interface.hide()
  284. self._view.groupbox_can_interface.show()
  285. def emv_data_recovery(self):
  286. self._view.cb_bms.setCurrentIndex(0)
  287. self._view.led_emv_total.set_status(0)
  288. self._view.led_emv_1.set_status(0)
  289. self._view.led_emv_2.set_status(0)
  290. self._view.led_emv_3.set_status(0)
  291. self._view.led_emv_4.set_status(0)
  292. self._view.led_emv_5.set_status(0)
  293. self._view.led_emv_6.set_status(0)
  294. self._view.led_emv_7.set_status(0)
  295. self._view.led_emv_8.set_status(0)
  296. self._view.led_emv_9.set_status(0)
  297. self._view.led_emv_10.set_status(0)
  298. SD.EMV_ID = 1