class UdsInterface(IsoTpProtocol): def __init__(self, can_dev): self.cq = CanQueue(can_dev) self.cq.start() def uds_request(self, ecu_id, service, payload, timeout=2): msg = IsoTpMessage(ecu_id) # first byte is service ID, rest of message is payload msg.data = [service] + payload # length is payload length plus 1 for service ID byte msg.length = len(payload) + 1 # generate a request request = self.generate_frames(msg) # send the request for f in request: self.cq.send(f) # get a response message with the request ID + 20 start_ts = time.time() result = None while result == None: if time.time() - start_ts > timeout: return None response = self.cq.recv() if response and response.id == ecu_id + 0x20: result = self.parse_frame(response) return result
from canard import can from canard.hw import cantact from canard.utils.queue import CanQueue import time import sys dev = cantact.CantactDev(sys.argv[1]) dev.set_bitrate(125000) cq = CanQueue(dev) cq.start() print cq.recv() req = can.Frame(0x6A5) req.dlc = 8 req.data = [0x10,0xFF, 0xFF] cq.send(req) print cq.recv(filter=0x625, timeout=10) cq.stop() #dev.stop() #dev.ser.write('S0\r') #dev.start() #while True: # frame = can.Frame(0x10) # frame.dlc=3
def get_door_status(): #CANable USB port. Find by calling ls /dev/tty* in the mac terminal usb_port = "/dev/tty.usbmodem146201" # init the Cantact with the usb port dev = cantact.CantactDev(usb_port) # set the bitrate to 500kbps dev.set_bitrate(500000) # wrap in a CanQueue cq = CanQueue(dev) # start scanning cq.start() # receive the packets, and filter for only the 947 (hex: 0x3B3) (door sensor) and print to console frame = cq.recv(filter=0x3B3, timeout=1) # stop searching cq.stop() # All of my analysis was done on the hexadecimal format, so we need to convert the ascii into hex for the algorithm byte_1 = format(frame.data[0], '02x') byte_8 = format(frame.data[7], '02x') # print statements just to verify what the 1st and 8th bytes are (the only bytes that will ever change) print("Byte 1:") print(byte_1) print("Byte 8:") print(byte_8) print("===============") # set up the door statuses (default = False) trunk_ajar = False driver_ajar = False passenger_ajar = False hood_ajar = False # Algorithm as described in my notebook under Methodology > Door Ajar Status Notes # Check the second digit of the first byte if byte_1[1] == "1": trunk_ajar = True # Check the first digit of the last byte if byte_8[0] == "0": driver_ajar = False passenger_ajar = False if byte_8[0] == "1": passenger_ajar = True if byte_8[0] == "2": driver_ajar = True if byte_8[0] == "3": driver_ajar = True passenger_ajar = True # Check the second digit of the last byte if byte_8[1] == "a": hood_ajar = True # print out the statuses to console (as testing) print("Trunk Ajar? " + str(trunk_ajar)) print("Driver Door Ajar? " + str(driver_ajar)) print("Passenger Door Ajar? " + str(passenger_ajar)) print("Hood Ajar? " + str(hood_ajar))