Skip to content

Instantly share code, notes, and snippets.

@qoopooh
Last active October 2, 2017 09:46
Show Gist options
  • Save qoopooh/55cb8b2887c96eae9523713fdeac169f to your computer and use it in GitHub Desktop.
Save qoopooh/55cb8b2887c96eae9523713fdeac169f to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
from __future__ import print_function
import can
import argparse
CAN_ID = '0x011'
MOTOR_INFO_PACKAGE = ('|', 0, '?')
COMM_CHECK_PACKAGE = ('B', 0, 'X')
parser = argparse.ArgumentParser(description='Send can message to C2000')
parser.add_argument('-i', '--can_id',
default=CAN_ID,
help='Set CAN ID (e.g. 0x012)')
def send_one(can_id):
"""Send CAN message to the bus
Args:
can_id (integer): CAN ID
"""
bus = can.interface.Bus(channel='can0', bustype='socketcan_ctypes')
msg = can.Message(arbitration_id=can_id,
data=COMM_CHECK_PACKAGE, extended_id=False)
try:
bus.send(msg)
print("Message sent on {}: {}".format(bus.channel_info, msg))
resp = bus.recv(2)
if resp:
print("Response 1: {}".format(resp))
resp = bus.recv(2)
if resp:
print("Response 2: {}".format(resp))
else:
print("No response")
except can.CanError:
print("Message NOT sent")
if __name__ == "__main__":
args = parser.parse_args()
if '0x' in args.can_id:
can_id = int(args.can_id, 16)
else:
can_id = int(args.can_id)
send_one(can_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment