Autonomous vehicles are no longer a dream! We now have self-driving cars that can navigate by themselves and let the passengers sit back and relax. While self-driving cars still have some bridges to the gap, the current state is still fascinating. The key to developing such a mesmerizing technology is reading and manipulating the data available from the vehicle. It is necessary to process the car data to make the self-driving algorithm work. [In reality, there are deep neural networks, along with PID controllers and hard-coded programming that control the vehicle, but to keep it simple, we’ll call it a “self-driving algorithm”]. This is where we segway into the topic of CAN-Bus.
In the world of programming, the Control Area Network (CAN) bus has emerged as a crucial communication protocol. It plays a pivotal role in various industries, with one of its most prominent applications being in the industry of self-driving cars. Developed by Bosch, it was designed to facilitate reliable communication between electronic control units (ECUs) within vehicles. A CAN bus consists of two or more twisted pair wires, where messages are broadcast to all nodes on the network. This means that every ECU can both send and receive data, making it a versatile and efficient communication system.
There are various tools and software to read data from a vehicle via CAN bus. This blog focuses on reading data via a simple Python program.

Basic Information about reading CAN data
Vehicle data can be read by connecting a USB-to-CAN adapter to the OBD port found on the driver’s side, underneath the steering wheel. Before connecting to the OBD port, the adapter needs to be set at a specific baud rate. Baud rate is the rate at which communication takes place via a channel. Here, we are talking about communication between the laptop and the vehicle via the “CAN channel”. As a standard, most vehicles have a baud rate of 500 kB/s. However, it is always necessary to check this before connecting to the OBD port as a false baud rate can cause the CAN bus to malfunction and your vehicle could go berserk.
Below is an example of a USB-to-CAN adapter. This innovative device, available on Amazon, allows you to use your PC and a hardware interface to get the information from your car’s computer. Please note that the provided link is an affiliate link. This comes at no extra cost to you and helps support the maintenance of this blog. Your support is greatly appreciated!
Reading data using Python
Below is the Python code to establish a connection with the CAN bus and continuously receive messages that come up in the bus.
import can
channel = 'socketcan'
interface = 'can0'
bus = can.interface.Bus(channel=channel, channel=interface, bitrate=500000)
def read_can_data():
try:
while True:
message = bus.recv() #Receive a message from the CAN bus
print(f"ID: {message.arbitration_id}, Data: {message.data}")
except KeyboardInterrupt:
print("\nExiting CAN bus reader.")
if __name__ == "__main__":
read_can_data()
Let’s break down the code.
import can
First, we import the can
module, which is a Python library for interacting with the CAN bus. You can install this library using pip3 install python-can
.
def read_can_data():
# Set up the CAN interface (replace 'socketcan' with your interface type)
channel = 'socketcan'
interface = 'can0' # Replace with your specific interface name
bus = can.interface.Bus(channel=channel, channel=interface, bitrate=500000)
Here, the function named read_can_data
is defined. Inside the function:
channel
is set to ‘socketcan’, which is a type of CAN interface. You may need to replace it with the appropriate interface type based on your setup.interface
is set to ‘can0’, but you should replace it with the specific name of your CAN interface.bitrate
is set to 500,000 bits per second, which is a common bitrate for CAN bus communication.
The can.interface.Bus
class is then used to create a connection to the CAN bus with the specified parameters.
try:
while True:
message = bus.recv() # Receive a message from the CAN bus
print(f"ID: {message.arbitration_id}, Data: {message.data}")
except KeyboardInterrupt:
print("\nExiting CAN bus reader.")
The bus.recv()
method receives a message from the bus, and the identifier (ID) and data of the received message are printed.
Safety and Legal Considerations
Before embarking on any exploration of vehicle systems, it is crucial to emphasize safety and legal compliance. Respect privacy, adhere to ethical standards, and ensure that your activities align with legal regulations. Unauthorized access to certain vehicle functions may have legal implications, and it is essential to use the acquired knowledge responsibly.
Conclusion
Reading vehicle data via the CAN bus with Python is a gateway to a world of possibilities. Whether you are an automotive enthusiast, a researcher, or a developer, tapping into the CAN bus provides unparalleled insights into the intricacies of vehicle systems. The Python code presented in this blog serves as a starting point for your journey into automotive data analysis and a stepping stone to the self-driving industry. Remember to exercise caution, prioritize safety, and adhere to legal and ethical standards while exploring the capabilities of the CAN bus. As you delve deeper into the automotive symphony, may your coding adventures be both enlightening and rewarding. Happy exploring and make sure to follow me on social media 🙂
Pingback: LiDAR vs. Cameras: Which One Won’t Crash Your Car? - Machine Learning Site