Skip to main content

CAN

Controller Area Network (CAN) is a serial bus with built-in error checking and retransmission, commonly used in automotive and industrial systems. The wiring is typically two differential wires (CAN-H and CAN-L); the bus needs a 120 Ω termination resistor at each end.

CAN linear bus

Endpoint uses Iot.Device.SocketCan.CanRaw from .NET IoT for the actual send/receive. The only Endpoint-specific step is calling CanController.Initialize(port, bitrate) first, which muxes the port's TX/RX pins and brings the canN interface up at the chosen bitrate.

using System.Device.Can;
using Iot.Device.SocketCan;
using GHIElectronics.Endpoint.Core;

// Bring up CAN1 at 500 kbit/s
CanController.Initialize(EPDomino24.Can.Can1, bitrate: 500_000);

// Send a frame with the standard .NET IoT API
using var can = new CanRaw(EPDomino24.Can.Can1);
can.WriteFrame(new byte[] { 11, 22, 33 }, new CanId { Standard = 0x123 });

Read frames in a loop:

using var can = new CanRaw(EPDomino24.Can.Can1);
while (true) {
var data = new byte[8];
if (can.TryReadFrame(data, out int length, out CanId id)) {
Console.WriteLine($"ID=0x{id.Value:X3} len={length} data={BitConverter.ToString(data, 0, length)}");
}
}

When you are done, release the port's pins:

CanController.UnInitialize(EPDomino24.Can.Can1);
tip

CanController is chip-neutral; the same call works on any supported module. The port constant (EPDomino24.Can.Can1) is board-specific and lists only the ports actually bonded on EP-Domino24.