Skip to content

RECEIVE_CAN_MESSAGE

Download Flojoy Studio to try this app
Read data from a CAN Bus connection. This block is used to read data from a CAN Bus connection. A connection to the CAN device must be established using the CAN_CONNECT block. Params: PCAN_address : str The CAN device address to connect to. timeout : int The timeout in seconds to wait for a message. Default is 10. raise_error : bool If True, an exception will be raised if no data is received. Default is True. Returns: Stateful : can.message.Message Return a can bus message
Python Code
from flojoy import flojoy, DataContainer, DeviceConnectionManager, Stateful
from typing import Optional
import can


@flojoy(deps={"python-can": "4.3.1"})
def RECEIVE_CAN_MESSAGE(
    CAN_address: str,
    timeout: int = 10,
    raise_error: bool = True,
    default: Optional[DataContainer] = None,
) -> Stateful:
    """Read data from a CAN Bus connection.

    This block is used to read data from a CAN Bus connection. A connection to the CAN device must be established using the CAN_CONNECT block.

    Parameters
    ----------
    PCAN_address : str
        The CAN device address to connect to.
    timeout : int
        The timeout in seconds to wait for a message. Default is 10.
    raise_error : bool
        If True, an exception will be raised if no data is received. Default is True.

    Returns
    -------
    Stateful : can.message.Message
        Return a can bus message
    """

    connection: can.interface.Bus = DeviceConnectionManager.get_connection(
        CAN_address
    ).get_handle()

    msg = connection.recv(timeout)

    if msg is None:
        if raise_error:
            raise Exception("No data received")
        else:
            return Stateful([])

    return Stateful([msg])

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 application demonstrates how to use multiple CAN blocks to connect to a PEAK-USB device and read messages from it. The PEAK-USB device is a USB-to-CAN interface that enables you to connect a computer to a CAN network. This device is used in this case to capture the messages of a particular sensor by filtering them directly at the kernel level, thus reducing the load on the CPU, and save those messages to a log file locally.

Once the app is done, the generated logs are exported to an S3 bucket to keep a record of the sensor’s data for further analysis.

To replicate this application, you must connect the PEAK-USB to your computer and install the required driver (refer to the PEAK_CONNECT blocks for more information on the necessary driver for your platform). Then, simply specify the PEAK-USB channel in the required blocks, and this Flojoy application will log the messages received by the device!

Detecting channels A valuable block is the PEAK_DETECT_AVAILABLE_DEVICE. This block allows you to display the devices using the PCAN interface that are connected to your computer.