SEND_PERIODIC_CAN_MESSAGE
Download Flojoy Studio to try this app
Send a periodic message to a CAN bus. Send a message to a CAN device periodically. This block should be compatible with all devices that support the CAN interface.
`STOP_SEND_PERIODIC_CAN_MESSAGE` can be used to stop sending the messages.
A connection to the device is required. Use a CAN_CONNECT block to connect to a CAN device. Params: CAN_address : str The CAN device address to connect to. message : Stateful A list of messages to send to the CAN device. period : float The period in seconds between messages. duration : Optional float Approximate duration in seconds to continue sending messages. If no duration is provided, the task will continue indefinitely. Returns: out : DataContainer Optional: None
Python Code
from typing import Optional
from flojoy import flojoy, DeviceConnectionManager, Stateful, DataContainer
import can
@flojoy(deps={"python-can": "4.3.1"})
def SEND_PERIODIC_CAN_MESSAGE(
CAN_address: str,
message: Stateful,
period: float,
duration: Optional[float] = None,
default: Optional[DataContainer] = None,
) -> Optional[DataContainer]:
"""Send a periodic message to a CAN bus.
Send a message to a CAN device periodically. This block should be compatible with all devices that support the CAN interface.
`STOP_SEND_PERIODIC_CAN_MESSAGE` can be used to stop sending the messages.
A connection to the device is required. Use a CAN_CONNECT block to connect to a CAN device.
Parameters
----------
CAN_address : str
The CAN device address to connect to.
message : Stateful
A list of messages to send to the CAN device.
period: float
The period in seconds between messages.
duration: Optional float
Approximate duration in seconds to continue sending messages. If no duration is provided, the task will continue indefinitely.
Returns
-------
DataContainer
Optional: None
"""
connection: can.interface.Bus = DeviceConnectionManager.get_connection(
CAN_address
).get_handle()
connection.send_periodic(message.obj, period, duration=duration)
return None
Example App
Having problems with this example app? Join our Discord community and we will help you out!
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.