can_bus_monitor-6.ino

BPI-5202 Loongson 2K1000LAi Embedded Single Board Industrial Computer · File : can_bus_monitor-6.ino · Language : python · Use case : Lire et afficher les trames CAN 2.0 reçues en temps réel via les ports CAN intégrés.

Monitors the BPI-5202 CAN 2.0 channel to debug industrial communications.

Requires CAN drivers loaded (standard SocketCAN module under Loongnix/LxAMP). Ensure 'can0' interface is active via 'ip link set can0 up type can bitrate 500000'. The script uses python-can library to access the hardware bus via Linux kernel API.

can_bus_monitor-6.ino

#!/usr/bin/env python3
"""
BPI-5202 CAN Bus Monitor
Prérequis : pip install python-can
Interfaces CAN typiques sous Loongnix/LxAMP : can0, can1
"""
import can
import sys
import time

try:
    # Configuration de l'interface CAN intégrée (vérifier via 'ip link' si différents noms)
    # BPI-5202 possède 2 ports CAN2.0
    bus = can.interface.Bus(channel='can0', interface='socketcan', bitrate=500000)
    print("Écoute CAN sur can0... (Ctrl+C pour quitter)")
    
    while True:
        message = bus.recv(timeout=1.0)
        if message is not None:
            print(f"ID: 0x{message.arbitration_id:03X} | DLC: {message.dlc} | Data: {message.data.hex(' ').upper()}")

except KeyboardInterrupt:
    print("\nArrêt")
except Exception as e:
    print(f"Erreur: {e}")

← Back to the board