CAN (Controller Area Network) is a protocol where multiple devices share a single bus for communication. It is highly resistant to electrical noise and allows any terminal to initiate data transmission at any time.
The Intelligent Edge Kilo comes equipped with built-in CANBus support.
Initial Setup
To begin, specify the two pins (tx and rx) connected to the external transceiver.
The following additional parameters can also be configured:
| Parameter | Type | Description |
|---|---|---|
| mode | string | Select from 'normal', 'noack', or 'listen'. normal: Requires an ACK after transmission. noack: Does not require an ACK. listen: Receive-only mode (no transmission). |
| kbps | number | Specifies the communication speed in kbps. |
| filter_code | number | 4-byte code. Packets matching this code (after masking) will be received. (Compliant with ESP32 CANBus Filter) |
| filter_mask | number | 4-byte mask. Set to 0 for no masking. (Compliant with ESP32 CANBus Filter) |
Use the following syntax to initialize the interface. The example below demonstrates the standard 'normal' mode, configured to send and receive all packets.
// Javascript Example
// Intelligent Edge Kilo
obniz.components!.prepare();
obniz.components!.powerOnInterface(KiloInterface.CANBus_On);
obniz.canbus0!.start({
tx: 5,
rx: 6,
mode: 'normal',
kbps: 500,
filter_code: 0,
filter_mask: 0
});
Data Transmission and Reception
Receiving Data
When data is received, the function assigned to onreceive is automatically triggered.
Sending Data
Data can be sent using the send() function. The parameters for transmission are as follows:
| Parameter | Type | Description |
|---|---|---|
| obj.extended | boolean | Specifies whether to use an Extended ID. |
| obj.rtr | boolean | Specifies whether the frame is a Remote Transmission Request (RTR). |
| obj.single_shot | boolean | Specifies if the message should be sent as a single shot (no retransmission). |
| obj.self_reception | boolean | Specifies if the message is a Self Reception request. |
| id | number | The Identifier of the message. |
| data | number[] | The data payload of the message. |
Full Code Example
// Javascript Example
// Intelligent Edge Kilo
obniz.components!.prepare();
obniz.components!.powerOnInterface(KiloInterface.CANBus_On);
// Initialize CANBus
obniz.canbus0!.start({
tx: 5,
rx: 6,
mode: 'normal',
kbps: 500,
filter_code: 0,
filter_mask: 0
});
// Set up Reception Handler
obniz.canbus0!.onreceive = async (isExtended: boolean, isRTR: boolean, id: number, data: number[]) => {
console.log("Device ID:", obniz.id, "Extended:", isExtended, "RTR:", isRTR, "ID:", id, "Data:", data);
};
// Send a Message
const target_id = 0x01;
obniz.canbus0!.send({
extended: false,
rtr: false,
single_shot: false,
self_reception: false
}, target_id, [0x01, 0x02]);