bms_balance_ctl.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. from utils.globalvar import SD
  4. from widget.bms_balance import Win_Balance
  5. class BmsBalanceControll:
  6. def __init__(self):
  7. self._view = Win_Balance()
  8. self.init()
  9. def init(self):
  10. self._view.balance_control_signal.connect(self._cell_balance_ctl)
  11. def _cell_balance_ctl(self, cmu_id, cell_id, state):
  12. print(cmu_id, cell_id, state)
  13. if (cell_id <= 40):
  14. try:
  15. data = []
  16. if len(data) < 8:
  17. data += (8 - len(data)) * [0]
  18. data[0] = 0x02
  19. data[1] = 0xC0
  20. data[2] = cmu_id
  21. data[3] = ((1 if (state > 0) else 0) << (cell_id - 1)) & 0xFF
  22. data[4] = (((1 if (state > 0) else 0) << (cell_id - 1)) & 0xFF00) >> 8
  23. data[5] = (((1 if (state > 0) else 0) << (cell_id - 1)) & 0xFF0000) >> 16
  24. data[6] = (((1 if (state > 0) else 0) << (cell_id - 1)) & 0xFF000000) >> 24
  25. data[7] = (((1 if (state > 0) else 0) << (cell_id - 1)) & 0xFF00000000) >> 32
  26. msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
  27. except:
  28. self._view.can_connect_error()
  29. else:
  30. try:
  31. data = []
  32. if len(data) < 8:
  33. data += (8 - len(data)) * [0]
  34. data[0] = 0x03
  35. data[1] = 0xC0
  36. data[2] = cmu_id
  37. data[3] = ((1 if (state > 0) else 0) << (cell_id - 41)) & 0xFF
  38. data[4] = (((1 if (state > 0) else 0) << (cell_id - 41)) & 0xFF00) >> 8
  39. data[5] = (((1 if (state > 0) else 0) << (cell_id - 41)) & 0xFF0000) >> 16
  40. data[6] = (((1 if (state > 0) else 0) << (cell_id - 41)) & 0xFF000000) >> 24
  41. data[7] = (((1 if (state > 0) else 0) << (cell_id - 41)) & 0xFF00000000) >> 32
  42. msg = SD.CAN_CONTROL.send((0x18 << 24) | (SD.BCU_ID << 16) | 0x1804, data)
  43. except:
  44. self._view.can_connect_error()