├── .gitignore ├── README.md ├── logfile.asc ├── neovi_bus.py └── virtual_bus.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .vscode 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # talk-to-your-car 2 | An example of sending and receiving CAN messages using python-can library 3 | 4 | ## virtual_bus.py 5 | 6 | It uses the [python-can](https://python-can.readthedocs.io/en/stable/index.html) built-in virtual bus to simulate the sending and receiving. It doesn't require any CAN hardware. It will send random data with ID 0x7E0 on the virtual bus in a thread every 0.5 second, and the receiver nofitier will print the received messages to the console and write them to the asc log file using built-in can.Logger notifier 7 | 8 | ## neovi_bus.py 9 | 10 | It basically does the same thing as virtual_bus.py, except It requires a real CAN hardware (which is an Intrepid device, probably valueCAN3, or other) . It will send random data on the real bus, so the CAN hardware needs to connect to somewhere (which is another CAN hardware). It will also receive the messages from outside device and save them to asc file 11 | 12 | In order to use this file, additional library(python) and drivers(CAN hardward) need to be installed. The guide on how to install can be found on [python-ics](python-ics.readthedocs.io) 13 | 14 | ## other CAN hardware 15 | 16 | python-can supports lots of CAN hardware including Vector, Kvaser, PCAN, etc. details can be found in Its doc. 17 | 18 | -------------------------------------------------------------------------------- /logfile.asc: -------------------------------------------------------------------------------- 1 | date Wed Feb 02 09:18:11.487131 PM 2020 2 | base hex timestamps absolute 3 | internal events logged 4 | Begin Triggerblock Wed Feb 02 09:18:11.492 PM 2020 5 | 0.000000 Start of measurement 6 | 0.000000 1 7E0 Rx d 8 0D 00 04 0B 0D 0D 0D 06 7 | 0.500714 1 7E0 Rx d 8 0C 08 0C 0D 06 09 0B 0E 8 | 1.001370 1 7E0 Rx d 8 03 0F 0B 02 0E 0D 00 04 9 | 1.502141 1 7E0 Rx d 8 0C 00 0B 0F 0D 08 0B 04 10 | 2.003721 1 7E0 Rx d 8 06 0C 07 02 06 05 0F 06 11 | 2.504515 1 7E0 Rx d 8 0F 07 07 0A 0C 05 01 08 12 | 3.006048 1 7E0 Rx d 8 0C 0F 01 0A 0F 01 05 00 13 | 3.506780 1 7E0 Rx d 8 08 0E 09 02 07 07 0D 0B 14 | 4.008424 1 7E0 Rx d 8 0E 0A 06 0D 0F 0E 00 04 15 | 4.509129 1 7E0 Rx d 8 03 05 0A 06 01 09 0D 0D 16 | 5.009780 1 7E0 Rx d 8 0A 0F 0C 0C 0D 05 07 05 17 | 5.511508 1 7E0 Rx d 8 0E 02 0C 05 03 09 00 08 18 | 6.012130 1 7E0 Rx d 8 0B 09 0A 08 0D 0F 0F 0A 19 | 6.512819 1 7E0 Rx d 8 0C 05 06 01 0E 06 02 0A 20 | 7.014481 1 7E0 Rx d 8 0F 0C 0E 0C 06 07 09 0C 21 | 7.515281 1 7E0 Rx d 8 03 0C 0B 01 02 0C 01 03 22 | 8.016892 1 7E0 Rx d 8 0D 02 08 0E 00 0E 04 09 23 | 8.517648 1 7E0 Rx d 8 02 04 02 03 01 05 0B 05 24 | 9.018320 1 7E0 Rx d 8 07 03 02 0D 09 00 02 0B 25 | 9.519911 1 7E0 Rx d 8 06 0B 07 00 09 0E 08 02 26 | 10.020699 1 7E0 Rx d 8 0A 01 09 04 0A 0E 0A 0D 27 | End TriggerBlock 28 | -------------------------------------------------------------------------------- /neovi_bus.py: -------------------------------------------------------------------------------- 1 | import can 2 | import threading 3 | import time 4 | import random 5 | 6 | def print_message(msg): 7 | print(msg) 8 | 9 | class tx_thread_cl: 10 | 11 | def __init__(self, bus): 12 | self.bus = bus 13 | self.running = True 14 | self.thread = threading.Thread(target=self.tx_callback, args=(self.bus,)) 15 | self.finished = False 16 | 17 | def start(self): 18 | self.thread.start() 19 | 20 | def tx_callback(self, bus): 21 | while self.running: 22 | data = [random.randint(0,15) for i in range(0,8)] 23 | msg = can.Message(is_extended_id=False, arbitration_id=0x7E0, data=data) 24 | bus.send(msg) 25 | time.sleep(0.5) 26 | self.finished = True 27 | 28 | def stop(self): 29 | self.running = False 30 | 31 | if __name__ == "__main__": 32 | bus = can.interface.Bus(bustype='neovi', channel='1', bitrate=500000) 33 | # RX part 34 | 35 | logger = can.Logger("logfile.asc") # save log to asc file 36 | listeners = [ 37 | print_message, # Callback function, print the received messages 38 | logger, # save received messages to asc file 39 | ] 40 | notifier = can.Notifier(bus, listeners) 41 | 42 | # TX part 43 | tx_service = tx_thread_cl(bus) 44 | tx_service.start() 45 | 46 | running = True 47 | while running: 48 | input() 49 | running = False 50 | 51 | while not tx_service.finished: 52 | tx_service.stop() 53 | 54 | # It's important to stop the notifier in order to finish the writting of asc file 55 | notifier.stop() 56 | # stops the bus 57 | bus.shutdown() -------------------------------------------------------------------------------- /virtual_bus.py: -------------------------------------------------------------------------------- 1 | import can 2 | import threading 3 | import time 4 | import random 5 | 6 | def print_message(msg): 7 | print(msg) 8 | 9 | class tx_thread_cl: 10 | 11 | def __init__(self, bus): 12 | self.bus = bus 13 | self.running = True 14 | self.thread = threading.Thread(target=self.tx_callback, args=(self.bus,)) 15 | self.finished = False 16 | 17 | def start(self): 18 | self.thread.start() 19 | 20 | def tx_callback(self, bus): 21 | while self.running: 22 | data = [random.randint(0,15) for i in range(0,8)] 23 | msg = can.Message(is_extended_id=False, arbitration_id=0x7E0, data=data) 24 | bus.send(msg) 25 | time.sleep(0.5) 26 | self.finished = True 27 | 28 | def stop(self): 29 | self.running = False 30 | 31 | if __name__ == "__main__": 32 | # RX part 33 | bus_rx = can.interface.Bus('virtual_ch', bustype='virtual') 34 | logger = can.Logger("logfile.asc") # save log to asc file 35 | listeners = [ 36 | print_message, # Callback function, print the received messages 37 | logger, # save received messages to asc file 38 | ] 39 | notifier = can.Notifier(bus_rx, listeners) 40 | 41 | # TX part 42 | bus_tx = can.interface.Bus('virtual_ch', bustype='virtual') 43 | tx_service = tx_thread_cl(bus_tx) 44 | tx_service.start() 45 | 46 | running = True 47 | while running: 48 | input() 49 | running = False 50 | 51 | while not tx_service.finished: 52 | tx_service.stop() 53 | 54 | # It's important to stop the notifier in order to finish the writting of asc file 55 | notifier.stop() 56 | # stops the bus 57 | bus_tx.shutdown() 58 | bus_rx.shutdown() --------------------------------------------------------------------------------