can_frame_read()
Description
Reads the buffered frame from the interface’s receive buffer into a can_frame_header structure and data array. Intended to be called from the script’s event callback when the event matches the CAN interface identifier (i.e. a frame has arrived).
Syntax
can_frame_read(can_interface, frame_header, frame_data);
Parameters
Parameter | Array structure | Description |
|
| CAN interface to read from – CAN1 or CAN2. |
|
| CAN data ID for the received frame. Example: 0x7D8. |
| Bitwise flags of CAN data type for the received frame. Possible options:
| |
| Length of data for the received frame. | |
| Command | |
|
| Array that receives up to frame_header.length data bytes from the received frame. |
Returns
Return value | Return explanation |
SCRIPT_OPERATION_SUCCESS | The operation completed successfully. |
SCRIPT_OPERATION_FAILED | The operation could not be completed (invalid state, hardware error, etc.). |
SCRIPT_NO_ACCESS | The CAN interface has not been initialized (call can_fd_init first). |
SCRIPT_PARAM_INVALID | One or more passed parameters are invalid. |
Function call example
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <can>
#define CAN_BUS CAN1
new can_rx_frame_header[.can_id, .type, .length, .is_packed];
new can_rx_frame_data{64};
new can_rx_buffer{4096};
forward public callback (event);
public callback (event)
{
switch(event)
{
case CAN_BUS:
{
can_frame_read(CAN_BUS, can_rx_frame_header, can_rx_frame_data);
if ((can_rx_frame_header.type & CAN_TYPE_ID_TYPE_MASK) == CAN_TYPE_ID_EXTENDED)
{
// Process Extended CAN id type frame
if ((can_rx_frame_header.type & CAN_TYPE_FRAME_FD) == CAN_TYPE_FRAME_FD)
{
// Process FD type frame
}
}
}
}
return 0;
}
main()
{
can_rx_frame_header.is_packed = true;
can_fd_init(CAN_BUS, CAN_CTRL_MODE_NORMAL | CAN_CTRL_MODE_ONE_SHOT | CAN_CTRL_MODE_FD_BRS, 2000, 500000, can_rx_buffer);
for(;;)
{
Delay(1000);
}
}Notes and Warnings
The event callback fires until all frames are read from can_rx_buffer buffer; call can_frame_read inside the matching callback case to make space for new frames.