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 # frame.data = [1,2,3] # dev.send(frame) # time.sleep(0.5)
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))