can_frame_transmit()
Description
Transmits a single CAN or CAN FD frame built from a can_frame_header structure and its associated data array.
Syntax
can_frame_transmit(can_interface, frame_header, frame_data);
Parameters
Parameter | Array structure | Description |
|
| CAN interface to transmit on – CAN1 or CAN2. |
|
| CAN identifier of the frame (11-bit standard or 29-bit extended, depending on the type flags). |
| Bitwise combination of frame-type flags: | |
| Number of data bytes in the frame: 0–8 for classic CAN, 0–64 for CAN FD. | |
| Set to true when the associated data array is a packed byte array (declared with {}, as in can_frame_data{64}) rather than a normal cell array. | |
|
| Packed byte array holding up to 64 data bytes (for FD frames) to transmit; only the first frame_header.length bytes are sent. |
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_frame_header[.can_id, .type, .length, .is_packed];
new can_frame_data{64};
new can_rx_buffer{4096};
main()
{
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(;;)
{
can_frame_header.can_id = 0x18DAEEFB;
can_frame_header.type = CAN_TYPE_ID_EXTENDED | CAN_TYPE_FRAME_FD | CAN_TYPE_FRAME_BRS;
can_frame_header.length = 64;
can_frame_header.is_packed = true;
can_frame_data{0} = can_frame_data{0} + 8;
can_frame_data{1} = can_frame_data{1} + 7;
can_frame_data{2} = can_frame_data{2} + 6;
can_frame_data{3} = can_frame_data{3} + 5;
can_frame_data{4} = can_frame_data{4} + 4;
can_frame_data{5} = can_frame_data{5} + 3;
can_frame_data{6} = can_frame_data{6} + 2;
can_frame_data{7} = can_frame_data{7} + 1;
can_frame_data{63} = can_frame_data{63} + 2;
can_frame_transmit(CAN_BUS, can_frame_header, can_frame_data);
Delay(1000);
}
}Notes and Warnings
frame_header.length must not exceed 8 for classic CAN frames or 64 for CAN FD frames (CAN_TYPE_FRAME_FD set).
CAN_TYPE_FRAME_BRS only has an effect when the interface was initialized with CAN_CTRL_MODE_FD_BRS and a data bit rate has been configured.