返回教程正文

配套源码

test_protocol.py

can-gateway/tests/test_protocol.py
zdt_x57s_can Brick:把一台 ZDT X57S 封装成可复用对象can-gateway/tests/test_protocol.py
Python63 行
  1. import unittest
  2. from zdt_x57s_protocol import (
  3. ZdtProtocolError,
  4. arbitration_id,
  5. build_enable_command,
  6. build_speed_command,
  7. build_speed_query,
  8. build_stop_command,
  9. parse_ack,
  10. parse_speed_reply,
  11. )
  12. class ProtocolTests(unittest.TestCase):
  13. """
  14. @description : 验证第二代FW_Emm固定CAN报文字节和解析边界
  15. @param : unittest自动创建
  16. @return : 无
  17. """
  18. def test_command_bytes(self):
  19. """
  20. @description : 校验地址映射、速度查询、使能、速度和停止报文
  21. @param : 无
  22. @return : 无
  23. """
  24. self.assertEqual(arbitration_id(1), 0x100)
  25. self.assertEqual(arbitration_id(4), 0x400)
  26. self.assertEqual(build_speed_query(), bytes.fromhex("35 6B"))
  27. self.assertEqual(
  28. build_enable_command(True), bytes.fromhex("F3 AB 01 00 6B")
  29. )
  30. self.assertEqual(
  31. build_speed_command(-20, 10),
  32. bytes.fromhex("F6 01 00 14 0A 00 6B"),
  33. )
  34. self.assertEqual(
  35. build_stop_command(), bytes.fromhex("FE 98 00 6B")
  36. )
  37. def test_speed_reply(self):
  38. """
  39. @description : 校验正反方向速度应答解析
  40. @param : 无
  41. @return : 无
  42. """
  43. self.assertEqual(parse_speed_reply(bytes.fromhex("35 00 00 3C 6B")), 60)
  44. self.assertEqual(parse_speed_reply(bytes.fromhex("35 01 00 14 6B")), -20)
  45. def test_rejected_ack(self):
  46. """
  47. @description : 校验E2拒绝状态会抛出协议异常
  48. @param : 无
  49. @return : 无
  50. """
  51. with self.assertRaises(ZdtProtocolError):
  52. parse_ack(bytes.fromhex("F3 E2 6B"), 0xF3)
  53. if __name__ == "__main__":
  54. unittest.main()