Flexs Q5 Pro
/ Industrial Protocols
/ can
can
CAN bus interface: initialise the bus, receive frames with a callback, and transmit standard or extended frames.
The can object connects the device to a CAN bus. Initialise the bus with a
data rate, register a receive handler, and transmit frames with can.tx() β
including extended IDs, CAN FD and remote (RTR) frames.
Functions
can.init() β Initialises the CAN bus interface.
can.listen() β Registers a receive handler for incoming CAN frames.
can.tx() β Transmits a CAN frame.
can.init(dataRate, canFD, autoRetransmit)
Initialises the CAN bus interface.
Arguments
Name Type Required Default Description
dataRate
number (kbps)
yes
β
Bus data rate in kbps, e.g. 125, 250, 500.
canFD
boolean
yes
β
Enable CAN FD framing.
autoRetransmit
boolean
yes
β
Enable automatic retransmission.
Example
Copy
can.init(125, false, false); // 125 kbps, classic CAN, no auto retransmission
can.listen(callback)
Registers a receive handler for incoming CAN frames.
The callback receives a message object with the frame id and data
payload. Decode binary payloads with the bin functions.
Arguments
Name Type Required Default Description
callback
function
yes
β
Callback function(msg){ β¦ } β msg = { id, data }.
Example
Copy
can.listen(function(msg){
print(string(msg)); // {"id":164242,"data":"hello from canbus"}
});
can.tx(id, payload, extendedId, canFD, remoteFrame)
Transmits a CAN frame.
Arguments
Name Type Required Default Description
id
number
yes
β
Frame identifier.
payload
string
yes
β
Frame payload β pack binary data with the bin functions.
extendedId
boolean
yes
β
true for a 29-bit extended identifier.
canFD
boolean
yes
β
true to send as a CAN FD frame.
remoteFrame
boolean
yes
β
true to send a remote (RTR) frame.
Example
Copy
can.tx(
12, // id
"Test Payload", // payload
false, // extended id
false, // canFD
false // remote frame (RTR)
);