SLCAN é um protocolo de interface CAN que opera via comunicação serial. É frequentemente usado para conectar a interfaces CAN via USB.
O exemplo a seguir usa a biblioteca python-can para enviar e receber mensagens CAN via SLCAN.
A intenção básica é ter dois adaptadores CAN separados (como o FYSETC UCAN muito barato com o firmware slcan)
Primeiro, instale a biblioteca python-can:
install_python_can.sh
pip install python-canExemplo de remetente
Este código se conecta a /dev/ttyACM0 e envia repetidamente uma única mensagem 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}")Saída de exemplo
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 08Exemplo de receptor
Este código se conecta a /dev/ttyACM1 e escuta por mensagens CAN recebidas (e as imprime na linha de comando):
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}")Saída de exemplo
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 08If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow