pcs_ctl.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  12. from utils.qt import QApplication, QThread, QTimer
  13. from controller.pcs_status_ctl import PcsStatusControll
  14. from widget.pcs_home import Win_Pcs_Home
  15. from worker.pcs_work import PcsCanReceived, PcsComWork
  16. import serial.tools.list_ports
  17. import utils.modbus.defines as cst
  18. class PcsControll:
  19. def __init__(self):
  20. self._app = QApplication(argv)
  21. self._view = Win_Pcs_Home()
  22. self.init()
  23. def init(self):
  24. self.port_check()
  25. self.pcs_status = PcsStatusControll()
  26. self._view.setStatusBar(self.pcs_status._view.statusbar)
  27. self._view.interface_signal.connect(self.interface_chose)
  28. self._view.connect_signal.connect(self.pcs_connect)
  29. self._view.disconnect_signal.connect(self.pcs_disconnect)
  30. self._view.btn_all_on.clicked.connect(self._relay_all_on)
  31. self._view.btn_all_off.clicked.connect(self._relay_all_off)
  32. self._view.btn_cls_on.clicked.connect(self._relay_on)
  33. self._view.btn_cls_off.clicked.connect(self._relay_off)
  34. # self.timer = QTimer()
  35. # self.timer.timeout.connect(self.com_work)
  36. def _relay_all_on(self):
  37. if SD.CAN_ON_OFF:
  38. if self._view.cb_interface.currentIndex() == 0:
  39. SD.CAN_CONTROL.send(0x182B003F, [1, 0, 0, 0, 0, 0, 0, 0], extern_flag=True)
  40. elif self._view.cb_interface.currentIndex() == 1:
  41. SD.COM_CONTROL.send(1, cst.WRITE_MULTIPLE_REGISTERS, 0x1900, 0x01)
  42. else:
  43. return
  44. def _relay_all_off(self):
  45. if SD.CAN_ON_OFF:
  46. if self._view.cb_interface.currentIndex() == 0:
  47. SD.CAN_CONTROL.send(0x182B003F, [0, 0, 0, 0, 0, 0, 0, 0], extern_flag=True)
  48. elif self._view.cb_interface.currentIndex() == 1:
  49. SD.COM_CONTROL.send(1, cst.WRITE_MULTIPLE_REGISTERS, 0x1900, 0x00)
  50. else:
  51. return
  52. def _relay_on(self):
  53. if SD.CAN_ON_OFF:
  54. if self._view.cb_interface.currentIndex() == 0:
  55. SD.CAN_CONTROL.send(0x182B003F, [0xff, 1, self._view.cb_cls.currentIndex() + 1, 0, 0, 0, 0, 0], extern_flag=True)
  56. elif self._view.cb_interface.currentIndex() == 1:
  57. SD.COM_CONTROL.send(1, cst.WRITE_MULTIPLE_REGISTERS, 0x1901, 1 << self._view.cb_cls.currentIndex())
  58. else:
  59. return
  60. def _relay_off(self):
  61. if SD.CAN_ON_OFF:
  62. if self._view.cb_interface.currentIndex() == 0:
  63. SD.CAN_CONTROL.send(0x182B003F, [0xff, 0, self._view.cb_cls.currentIndex() + 1, 0, 0, 0, 0, 0], extern_flag=True)
  64. elif self._view.cb_interface.currentIndex() == 1:
  65. SD.COM_CONTROL.send(1, cst.WRITE_MULTIPLE_REGISTERS, 0x1901, (~ (1 << self._view.cb_cls.currentIndex())) & 0xFFFF)
  66. else:
  67. return
  68. def com_work(self):
  69. ret = SD.COM_CONTROL.send(1, cst.READ_HOLDING_REGISTERS, 0x0100, 16)
  70. print(list(ret))
  71. def port_check(self):
  72. com_port = {}
  73. port_list = list(serial.tools.list_ports.comports())
  74. self._view.cb_com_interface_port.clear()
  75. for port in port_list:
  76. com_port["%s" % port_list[0]] = "%s" % port[1]
  77. self._view.cb_com_interface_port.addItem(port[0])
  78. def pcs_connect(self):
  79. if self._view.cb_interface.currentIndex() == 0:
  80. self.pcs_can_connect()
  81. elif self._view.cb_interface.currentIndex() == 1:
  82. self.pcs_com_connect()
  83. else:
  84. return
  85. def pcs_disconnect(self):
  86. if self._view.cb_interface.currentIndex() == 0:
  87. self.pcs_can_disconnect()
  88. elif self._view.cb_interface.currentIndex() == 1:
  89. self.pcs_com_disconnect()
  90. else:
  91. return
  92. def pcs_can_connect(self):
  93. self._can_index_var = self._view.cb_can_interface_index.currentText()
  94. self._can_chanel_var = self._view.cb_can_interface_channel.currentText()
  95. self._can_baudrate_var = self._view.cb_can_interface_baudrate.currentText()
  96. can_index = int(self._can_index_var)
  97. can_channel = int(self._can_chanel_var)
  98. can_baudrate = self._can_baudrate_var
  99. SD.CAN_CONTROL.set_can_board(can_index, can_channel, can_baudrate)
  100. try:
  101. if SD.CAN_CONTROL.open_device():
  102. SD.CAN_ON_OFF = SD.CAN_CONTROL.init_can(0x00000000, 0xFFFFFFFF)
  103. if SD.CAN_ON_OFF:
  104. self.pcs_can_connected()
  105. self._view.groupbox_can_interface.setDisabled(True)
  106. self._view.groupbox_interface.setDisabled(True)
  107. else:
  108. self._view.can_connect_error()
  109. except AttributeError:
  110. self._view.no_can_device()
  111. def pcs_can_disconnect(self):
  112. if SD.CAN_ON_OFF:
  113. SD.CAN_ON_OFF = False
  114. SD.CAN_CONTROL.clear_buffer()
  115. SD.CAN_CONTROL.close_can()
  116. if self.pcs_can_received_thread.isRunning():
  117. self.pcs_can_received_thread.quit()
  118. self.pcs_can_received_thread.wait()
  119. if self.pcs_can_received_thread.isFinished():
  120. del self.pcs_can_received
  121. del self.pcs_can_received_thread
  122. self._view.groupbox_can_interface.setDisabled(False)
  123. self._view.groupbox_interface.setDisabled(False)
  124. self.pcs_data_recovery()
  125. self.pcs_status.pcs_disconnect()
  126. def pcs_com_disconnect(self):
  127. if SD.CAN_ON_OFF:
  128. SD.CAN_ON_OFF = False
  129. if self.pcs_com_thread.isRunning():
  130. self.pcs_com_thread.quit()
  131. self.pcs_com_thread.wait()
  132. if self.pcs_com_thread.isFinished():
  133. del self.pcs_com_work
  134. del self.pcs_com_thread
  135. # self.timer.stop()
  136. self._view.groupbox_com_interface.setDisabled(False)
  137. self._view.groupbox_interface.setDisabled(False)
  138. self.pcs_data_recovery()
  139. self.pcs_status.pcs_disconnect()
  140. def pcs_com_connect(self):
  141. self._port = self._view.cb_com_interface_port.currentText()
  142. self._baudrate = int(self._view.cb_com_interface_baudrate.currentText())
  143. self._bytesize = int(self._view.cb_com_interface_word_length.currentText())
  144. self._stopbits = int(self._view.cb_com_interface_stop.currentText())
  145. if self._view.cb_com_interface_parity.currentIndex() == 0:
  146. self._parity = 'N'
  147. elif self._view.cb_com_interface_parity.currentIndex() == 1:
  148. self._parity = 'E'
  149. elif self._view.cb_com_interface_parity.currentIndex() == 2:
  150. self._parity = 'O'
  151. else:
  152. self._parity = 'M'
  153. SD.COM_CONTROL.set_com(self._port, self._baudrate, self._bytesize, self._parity, self._stopbits)
  154. SD.CAN_ON_OFF = SD.COM_CONTROL.open_device()
  155. if SD.CAN_ON_OFF:
  156. self.pcs_com_connected()
  157. # self.timer.start(1000)
  158. self._view.groupbox_com_interface.setDisabled(True)
  159. self._view.groupbox_interface.setDisabled(True)
  160. else:
  161. self._view.no_com_device()
  162. def pcs_com_connected(self):
  163. # Com线程类
  164. self.pcs_com_thread = QThread()
  165. self.pcs_com_work = PcsComWork()
  166. self.pcs_com_work.moveToThread(self.pcs_com_thread)
  167. self.pcs_com_thread.finished.connect(self.pcs_com_work.deleteLater)
  168. self.pcs_com_thread.started.connect(self.pcs_com_work.work)
  169. self.pcs_com_work.show_00_com_signal.connect(self._pcs_00_com_signal)
  170. self.pcs_com_work.show_01_com_signal.connect(self._pcs_01_com_signal)
  171. self.pcs_com_work.show_02_com_signal.connect(self._pcs_02_com_signal)
  172. self.pcs_com_work.show_03_com_signal.connect(self._pcs_03_com_signal)
  173. self.pcs_com_work.show_04_com_signal.connect(self._pcs_04_com_signal)
  174. self.pcs_com_work.show_05_com_signal.connect(self._pcs_05_com_signal)
  175. self.pcs_com_work.show_06_com_signal.connect(self._pcs_06_com_signal)
  176. self.pcs_com_work.show_07_com_signal.connect(self._pcs_07_com_signal)
  177. self.pcs_com_work.show_08_com_signal.connect(self._pcs_08_com_signal)
  178. self.pcs_com_work.show_09_com_signal.connect(self._pcs_09_com_signal)
  179. self.pcs_com_work.show_0A_com_signal.connect(self._pcs_0A_com_signal)
  180. self.pcs_com_work.show_0B_com_signal.connect(self._pcs_0B_com_signal)
  181. self.pcs_com_work.show_0C_com_signal.connect(self._pcs_0C_com_signal)
  182. self.pcs_com_work.show_0D_com_signal.connect(self._pcs_0D_com_signal)
  183. self.pcs_com_work.show_0E_com_signal.connect(self._pcs_0E_com_signal)
  184. self.pcs_com_work.show_0F_com_signal.connect(self._pcs_0F_com_signal)
  185. self.pcs_status.pcs_connect()
  186. # 启动线程
  187. self.pcs_com_thread.start()
  188. def _pcs_00_com_signal(self, data):
  189. print(hex(data))
  190. def _pcs_01_com_signal(self, data):
  191. print(hex(data))
  192. def _pcs_02_com_signal(self, data):
  193. self._view.edt_bms_volt.setText(str(data * 0.1))
  194. def _pcs_03_com_signal(self, data):
  195. self._view.edt_bms_cur.setText(str(data * 0.1 - 2000))
  196. def _pcs_04_com_signal(self, data):
  197. self._view.edt_soc.setText(str(data * 0.1))
  198. def _pcs_05_com_signal(self, data):
  199. self._view.edt_bms_soh.setText(str(data * 0.1))
  200. def _pcs_06_com_signal(self, data):
  201. self._view.edt_bms_chg_cur_max.setText(str(data * 0.1))
  202. def _pcs_07_com_signal(self, data):
  203. self._view.edt_bms_dischg_cur_max.setText(str(data * 0.1))
  204. def _pcs_08_com_signal(self, data):
  205. self._view.edt_bms_chg_volt_high.setText(str(data * 0.1))
  206. def _pcs_09_com_signal(self, data):
  207. self._view.edt_bms_dischg_volt_high.setText(str(data * 0.1))
  208. def _pcs_0A_com_signal(self, data):
  209. self._view.edt_bms_chg_power_real.setText(str(data * 0.1))
  210. def _pcs_0B_com_signal(self, data):
  211. self._view.edt_bms_dischg_power_real.setText(str(data * 0.1))
  212. def _pcs_0C_com_signal(self, data):
  213. self._view.edt_bms_high_volt.setText(str(data * 0.1))
  214. def _pcs_0D_com_signal(self, data):
  215. self._view.edt_bms_low_volt.setText(str(data * 0.1))
  216. def _pcs_0E_com_signal(self, data):
  217. self._view.edt_bms_dischg_power_max.setText(str(data * 0.1))
  218. def _pcs_0F_com_signal(self, data):
  219. self._view.edt_bms_chg_power_max.setText(str(data * 0.1))
  220. def pcs_can_connected(self):
  221. # CAN数据接收类
  222. self.pcs_can_received_thread = QThread()
  223. self.pcs_can_received = PcsCanReceived()
  224. self.pcs_can_received.moveToThread(self.pcs_can_received_thread)
  225. self.pcs_can_received_thread.finished.connect(self.pcs_can_received.deleteLater)
  226. self.pcs_can_received_thread.started.connect(self.pcs_can_received.received)
  227. self.pcs_can_received.show_90_signal.connect(self._90_signal)
  228. self.pcs_can_received.show_91_signal.connect(self._91_signal)
  229. self.pcs_can_received.show_92_signal.connect(self._92_signal)
  230. self.pcs_can_received.show_93_signal.connect(self._93_signal)
  231. self.pcs_status.pcs_connect()
  232. # 启动线程
  233. self.pcs_can_received_thread.start()
  234. def _90_signal(self, data):
  235. self._view.edt_soc.setText(str(round(data[0] * 0.4, 1)))
  236. self._view.led_bms_ready.set_status(data[1])
  237. if data[2] == 0:
  238. self._view.edt_bms_chg_enabled.setText("允充允放")
  239. elif data[2] == 1:
  240. self._view.edt_bms_chg_enabled.setText("允充禁放")
  241. elif data[2] == 2:
  242. self._view.edt_bms_chg_enabled.setText("禁充允放")
  243. elif data[2] == 3:
  244. self._view.edt_bms_chg_enabled.setText("禁充禁放")
  245. self._view.edt_bms_stop.setText("")
  246. self._view.led_bms_fault.set_status(2 if data[4] == 1 else 0)
  247. self._view.edt_bms_soh.setText(str(round(data[5] * 0.4, 1)))
  248. self._view.edt_bms_high_temp.setText(str(data[6] - 40))
  249. self._view.edt_bms_low_temp.setText(str(data[7] - 40))
  250. def _91_signal(self, data):
  251. self._view.edt_bms_dischg_power_max.setText(str(round((data[1] << 8 | data[0]) * 0.1, 1)))
  252. self._view.edt_bms_chg_power_max.setText(str(round((data[3] << 8 | data[2]) * 0.1, 1)))
  253. self._view.edt_bms_chg_cur_max.setText(str(round((data[5] << 8 | data[4]) * 0.1 - 3200, 1)))
  254. self._view.edt_bms_dischg_cur_max.setText(str(round((data[7] << 8 | data[6]) * 0.1 - 3200, 1)))
  255. def _92_signal(self, data):
  256. self._view.edt_bms_high_volt.setText(str(round((data[1] << 8 | data[0]) * 0.001, 3)))
  257. self._view.edt_bms_low_volt.setText(str(round((data[3] << 8 | data[2]) * 0.001, 3)))
  258. self._view.edt_bms_chg_volt_high.setText(str(round((data[5] << 8 | data[4]) * 0.1, 1)))
  259. self._view.edt_bms_dischg_volt_high.setText(str(round((data[7] << 8 | data[6]) * 0.1, 1)))
  260. def _93_signal(self, data):
  261. self._view.edt_bms_volt.setText(str(round((data[1] << 8 | data[0]) * 0.1, 2)))
  262. self._view.edt_bms_cur.setText(str(round((data[3] << 8 | data[2]) * 0.1 - 3200, 2)))
  263. self._view.edt_bms_chg_power_real.setText(str(round((data[5] << 8 | data[4]) * 0.1, 1)))
  264. self._view.edt_bms_dischg_power_real.setText(str(round((data[7] << 8 | data[6]) * 0.1, 1)))
  265. def run(self):
  266. self._view.show()
  267. return self._app.exec_()
  268. def interface_chose(self, current_interface):
  269. if current_interface == 1:
  270. self._view.groupbox_can_interface.hide()
  271. self._view.groupbox_com_interface.show()
  272. else:
  273. self._view.groupbox_com_interface.hide()
  274. self._view.groupbox_can_interface.show()
  275. def pcs_data_recovery(self):
  276. self._view.edt_soc.setText("")
  277. self._view.led_bms_ready.set_status(0)
  278. self._view.edt_bms_chg_enabled.setText("")
  279. self._view.edt_bms_stop.setText("")
  280. self._view.led_bms_fault.set_status(0)
  281. self._view.edt_bms_soh.setText("")
  282. self._view.edt_bms_high_temp.setText("")
  283. self._view.edt_bms_low_temp.setText("")
  284. self._view.edt_bms_dischg_power_max.setText("")
  285. self._view.edt_bms_chg_power_max.setText("")
  286. self._view.edt_bms_chg_cur_max.setText("")
  287. self._view.edt_bms_dischg_cur_max.setText("")
  288. self._view.edt_bms_high_volt.setText("")
  289. self._view.edt_bms_low_volt.setText("")
  290. self._view.edt_bms_chg_volt_high.setText("")
  291. self._view.edt_bms_dischg_volt_high.setText("")
  292. self._view.edt_bms_volt.setText("")
  293. self._view.edt_bms_cur.setText("")
  294. self._view.edt_bms_chg_power_real.setText("")
  295. self._view.edt_bms_dischg_power_real.setText("")