Пример отправки и приёма Python CAN / SLCAN

SLCAN — протокол CAN-интерфейса, работающий через последовательную связь. Он часто используется для подключения к CAN-интерфейсам через USB.

Следующий пример использует библиотеку python-can для отправки и приёма CAN-сообщений через SLCAN.

Основная идея — иметь два отдельных CAN-адаптера (например, очень дешёвый FYSETC UCAN с slcan-прошивкой)

Сначала установите библиотеку python-can:

install_python_can.sh
pip install python-can

Пример отправителя

Этот код подключается к /dev/ttyACM0 и многократно отправляет одно CAN-сообщение:

slcan_sender.py
#!/usr/bin/env python3
import can
import time

# Configuration for the SLCAN device
slcan_device = '/dev/ttyACM0'
baud_rate = 500000  # Set the appropriate baud rate for your setup

# Create a CAN bus instance using the SLCAN interface
bus = can.interface.Bus(interface='slcan', channel=slcan_device, bitrate=baud_rate)

# Define a simple CAN message
can_id = 0x123  # CAN ID
data = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]  # 8 bytes of data

# Create a CAN message
message = can.Message(arbitration_id=can_id, data=data, is_extended_id=False)

try:
    while True:
        # Send the CAN message
        bus.send(message)
        print(f"Sent: {message}")

        # Wait for a second before sending the next message
        time.sleep(1)

except KeyboardInterrupt:
    print("Stopped by user")

except can.CanError as e:
    print(f"CAN error: {e}")

Пример вывода

slcan_sender_output.txt
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Sent: Timestamp:        0.000000    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08

Пример приёмника

Этот код подключается к /dev/ttyACM1 и слушает входящие CAN-сообщения (и выводит их в командной строке):

slcan_receiver.py
#!/usr/bin/env python3
import can

# Configuration for the SLCAN device
slcan_device = '/dev/ttyACM1'
baud_rate = 500000  # Set the appropriate baud rate for your setup

# Create a CAN bus instance using the SLCAN interface
bus = can.interface.Bus(interface='slcan', channel=slcan_device, bitrate=baud_rate)

try:
    print("Listening for CAN messages on", slcan_device)
    while True:
        # Read a message from the CAN bus
        message = bus.recv()

        if message is not None:
            print(f"Received: {message}")

except KeyboardInterrupt:
    print("Stopped by user")

except can.CanError as e:
    print(f"CAN error: {e}")

Пример вывода

slcan_receiver_output.txt
Listening for CAN messages on /dev/ttyACM1
Received: Timestamp: 1722300573.156834    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300573.157226    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300573.157592    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300573.157937    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300573.979205    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300574.979508    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300575.979660    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300576.979969    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08
Received: Timestamp: 1722300577.980201    ID:      123    S Rx                DL:  8    01 02 03 04 05 06 07 08

Check out similar posts by category: Python CAN