No Description

canfixer.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. from __future__ import absolute_import, print_function
  4. import sys
  5. import argparse
  6. import socket
  7. from datetime import datetime
  8. import can
  9. from can import Bus, BusState, Logger
  10. def decon(msg_id):
  11. msg_prio=msg_id>>26
  12. msg_type=(msg_id>>24) & 0x03
  13. msg_dst=(msg_id>>16) & 0xFF
  14. msg_src=(msg_id>>8) & 0xFF
  15. msg_cmd=msg_id & 0xFF
  16. print("prio: ", hex(msg_prio),"type: ", hex(msg_type)," dst: ", hex(msg_dst)," src: ", hex(msg_src)," cmd: ",hex(msg_cmd))
  17. return [msg_prio, msg_type, msg_dst, msg_src, msg_cmd]
  18. def main():
  19. verbosity = 2
  20. logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
  21. can.set_logging_level(logging_level_name)
  22. can_filters = []
  23. config = {"can_filters": can_filters, "single_handle": True}
  24. config["interface"] = "socketcan"
  25. config["bitrate"] = 125000
  26. bus = Bus("can1", **config)
  27. print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))
  28. print('Can Logger (Started on {})\n'.format(datetime.now()))
  29. try:
  30. while True:
  31. msg = bus.recv(1)
  32. if msg is not None:
  33. de=decon(msg.arbitration_id)
  34. m= { "prio": hex(de[0]), "type": hex(de[1]), "dst": hex(de[2]), "src": hex(de[3]), "cmd": hex(de[4]), "target": hex(msg.data[0]) }
  35. if de[2] is 3 and msg.data[0] is 3 and de[1] is 0 and de[3] is 3 and de[4] is 3 :
  36. print(m)
  37. m = can.Message(arbitration_id=0x0C030900,
  38. data=[3],
  39. extended_id=True)
  40. try:
  41. bus.send(m)
  42. except BaseException as e:
  43. logging.error("Error sending can message {%s}: %s" % (m, e))
  44. except KeyboardInterrupt:
  45. pass
  46. finally:
  47. bus.shutdown()
  48. if __name__ == "__main__":
  49. main()