Python CAN / SLCAN 发送和接收示例

SLCAN 是一种通过串行通信操作的 CAN 接口协议。它通常用于通过 USB 连接到 CAN 接口。

以下示例使用 python-can 库通过 SLCAN 发送和接收 CAN 消息。

基本意图是拥有两个独立的 CAN 适配器(例如非常便宜的 FYSETC UCANslcan 固件

首先,安装 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