Skip to content

CANABLE_CONNECT

Download Flojoy Studio to try this app
Connect to a slcan-compatible USB-to-CAN adapter. Params: device : SerialDevice The serial device to connect to. CAN_address : str The `CAN_address` to use in other blocks to refer to this connection. Note: This is not the physical address of the device, but rather a unique identifier for the connection. Needed since not all CAN bus connection can be retreive with a SerialDevice. bitrate : int The bitrate to use for the CAN bus. Returns: out : Optional[DataContainer] None
Python Code
from flojoy import (
    DataContainer,
    SerialDevice,
    flojoy,
    DeviceConnectionManager,
    HardwareDevice,
)
from typing import Optional
import can


@flojoy(deps={"python-can": "4.3.1"})
def CANABLE_CONNECT(
    device: SerialDevice,
    CAN_address: str,
    bitrate: int = 500000,
    default: Optional[DataContainer] = None,
) -> Optional[DataContainer]:
    """Connect to a slcan-compatible USB-to-CAN adapter.

    Parameters
    ----------
    device : SerialDevice
        The serial device to connect to.
    CAN_address : str
        The `CAN_address` to use in other blocks to refer to this connection.
        Note: This is not the physical address of the device, but rather a unique identifier for the connection. Needed since not all CAN bus connection can be retreive with a SerialDevice.
    bitrate : int
        The bitrate to use for the CAN bus.

    Returns
    -------
    Optional[DataContainer]
        None
    """

    session = can.interface.Bus(
        interface="slcan", channel=device.get_port(), bitrate=bitrate
    )

    DeviceConnectionManager.register_connection(
        HardwareDevice(CAN_address), session, lambda slcan_bus: slcan_bus.shutdown()
    )

    return None

Find this Flojoy Block on GitHub

Example App

Having problems with this example app? Join our Discord community and we will help you out!
React Flow mini map

This example shows how to send a frame to a CAN bus using the CANable USB-to-CAN adapter. The application sends a first frame intended to indicate the start of a test on a system. Subsequently, the application sends a burst of messages and then sends a frame to indicate the end of the test.

This application uses an CANABLE_CONNECT block to establish a connection to the CAN bus, but this connection could be replaced by any other connection. For example, the PEAK_CONNECT block could be used instead.