[PYTHON] Hello, world! With virtual CAN communication

Overview

Let's simulate CAN communication used for in-vehicle embedded applications in a PC. You can read more about CAN in the article below. https://qiita.com/fattolys/items/e8f8081d3cb42d7da0f6

The following is the network configuration of the system created this time. Throw a CAN packet from Nord2 to Nord1. vcan.jpg

environment

Create virtual interface

Create a virtual CAN interface with the following command.

 $ modprobe vcan
 $ sudo ip link add dev vcan0 type vcan
 $ sudo ip link set up vcan0

If you hit ifconfig, you can see that vcan0 is added as shown below.

$ ifconfig
vcan0: flags=193<UP,RUNNING,NOARP>  mtu 72
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 1000  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Nord 1 code creation

Nord 1 will display the packets coming to vcan 0 sequentially.

vcan_nord_1.py


# ref: https://elinux.org/Python_Can
import socket
import struct

# CAN frame packing/unpacking (see `struct can_frame` in <linux/can.h>)
can_frame_fmt = "=IB3x8s"

def build_can_frame(can_id, data):
    can_dlc = len(data)
    data = data.ljust(8, b'\x00')
    return struct.pack(can_frame_fmt, can_id, can_dlc, data)

def dissect_can_frame(frame):
    can_id, can_dlc, data = struct.unpack(can_frame_fmt, frame)
    return (can_id, can_dlc, data[:can_dlc])

# create a raw socket and bind it to the given CAN interface
s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
s.bind(("vcan0",))

while True:
    cf, addr = s.recvfrom(16)
    print('Received: can_id=%x, can_dlc=%x, data=%s' % dissect_can_frame(cf))

Nord 2 code creation

Nord2 throws a "hellowld" packet to vcan0.

vcan_nord_2.py


# ref: https://elinux.org/Python_Can
import socket
import struct

# CAN frame packing/unpacking (see `struct can_frame` in <linux/can.h>)
can_frame_fmt = "=IB3x8s"

def build_can_frame(can_id, data):
    can_dlc = len(data)
    data = data.ljust(8, b'\x00')
    return struct.pack(can_frame_fmt, can_id, can_dlc, data)

# create a raw socket and bind it to the given CAN interface
s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
s.bind(("vcan0",))

try:
    s.send(build_can_frame(0x01, b'hellowld'))
except socket.error:
    print('Error sending CAN frame')

Operation check

Start vcan_nord_1.py in the terminal and execute vcan_nord_2.py in another terminal. As shown below, the terminal on the vcan_nord_1.py side will show that the packet has arrived. Peek 2020-02-16 16-35.gif

from now on

It might be interesting to make a car that runs within a 3DCG platform such as Unity using vcan communication.

Reference site

https://elinux.org/Bringing_CAN_interface_up https://elinux.org/Python_Can

Recommended Posts

Hello, world! With virtual CAN communication
hello world with ctypes
Hello, World with Docker
Hello world with flask
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Python starting with Hello world!
[Note] Hello world output with python
Hello world
Hello World! By QPython with Braincrash
Hello World and face detection with opencv-python 4.2
Hello World with Raspberry Pi + Minecraft Pi Edition
Hello World! By QPython with Brainfu * k
Pymacs hello world
cython hello world
Hello World and face detection with OpenCV 4.3 + Python
Hello World with gRPC / go in Docker environment
Say hello to the world with Python with IntelliJ
Hello World with nginx + uwsgi + python on EC2
Create a one-file hello world application with django
First python ① Environment construction with pythonbrew & Hello World !!
Create a "Hello World" (HTTP) server with Tornado
Serial communication with Python
Socket communication with Python
web2py memo: Hello World
Serial communication with python
RabbitMQ Tutorial 1 ("Hello World!")
HTTP communication with Python
Hello World on Django
Django's first Hello World
Virtual environment with Python 3.6
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Modeling-
Hello World with Google App Engine (Java 8) + Spring Boot + Gradle
Hello World with Google App Engine (Java 8) + Servlet API 3.1 + Gradle
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Prediction / Evaluation-
Hello World with Google App Engine (Java 11) + Spring Boot + Gradle
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Programming language in "Hello World"
View Python communication with Fiddler
Hello World in GO language
Hello World (beginners) on Django
Switch virtual environment with jupyter
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Start with Windows, not so scary Nim ① It's like hello world.