Skip to main content

CanController Class

NuGet: GHIElectronics.TinyCLR.Devices.Can
Assembly: GHIElectronics.TinyCLR.Devices.Can
Namespace: GHIElectronics.TinyCLR.Devices.Can

Represents a CAN bus controller. Configure bit timing and acceptance filters, Enable the controller, then exchange CanMessages. Subscribe to MessageReceived for event-driven receive instead of polling MessagesToRead.

public class CanController : IDisposable

Properties​

Provider​

public ICanControllerProvider Provider { get; }

The low-level provider backing this controller.

WriteBufferSize​

public int WriteBufferSize { get; set; }

Size of the transmit message queue.

ReadBufferSize​

public int ReadBufferSize { get; set; }

Size of the receive message queue.

MessagesToWrite​

public int MessagesToWrite { get; }

Messages currently queued for transmission.

MessagesToRead​

public int MessagesToRead { get; }

Messages currently available to read.

CanWriteMessage​

public bool CanWriteMessage { get; }

True when the transmit queue has room.

CanReadMessage​

public bool CanReadMessage { get; }

True when at least one received message is available.

WriteErrorCount​

public int WriteErrorCount { get; }

Current transmit error counter (TEC).

ReadErrorCount​

public int ReadErrorCount { get; }

Current receive error counter (REC).

SourceClock​

public int SourceClock { get; }

Source clock feeding the CAN prescaler, in Hz. Used to compute bit timing.

Filter​

public Filter Filter { get; }

Acceptance filter configuration for this controller.

Methods​

GetDefault()​

public static CanController GetDefault()

Returns the default CAN controller for this device.

Returns CanController

FromName(string name)​

public static CanController FromName(string name)

Returns a CAN controller identified by its native API name.

Returns CanController

FromProvider(ICanControllerProvider provider)​

public static CanController FromProvider(ICanControllerProvider provider)

Creates a controller from a custom ICanControllerProvider.

Returns CanController

Dispose()​

public void Dispose()

Releases the underlying provider.

Enable()​

public void Enable()

Powers on the controller. Call after configuring timing and filters.

Disable()​

public void Disable()

Powers off the controller.

WriteMessage(CanMessage message)​

public bool WriteMessage(CanMessage message)

Queues a single message for transmission. Returns true on success.

Returns bool

WriteMessages(CanMessage[] messages, int offset, int count)​

public int WriteMessages(CanMessage[] messages, int offset, int count)

Queues a slice of messages for transmission.

Returns int — Number of messages successfully queued.

ReadMessage(out CanMessage message)​

public bool ReadMessage(out CanMessage message)

Dequeues a single received message. Returns true if one was available.

Returns bool

ReadMessages(CanMessage[] messages, int offset, int count)​

public int ReadMessages(CanMessage[] messages, int offset, int count)

Dequeues up to count received messages into messages.

Returns int — Number of messages actually read.

SetNominalBitTiming(CanBitTiming bitTiming)​

public void SetNominalBitTiming(CanBitTiming bitTiming)

Configures the arbitration-phase bit timing (used for the whole frame in classic CAN).

SetDataBitTiming(CanBitTiming bitTiming)​

public void SetDataBitTiming(CanBitTiming bitTiming)

Configures the data-phase bit timing for CAN-FD frames with bit-rate switching.

ClearWriteBuffer()​

public void ClearWriteBuffer()

Empties the transmit queue.

ClearReadBuffer()​

public void ClearReadBuffer()

Empties the receive queue.

Events​

MessageReceived​

public event MessageReceivedEventHandler MessageReceived

Raised when one or more messages have arrived. Call ReadMessage from the handler.

ErrorReceived​

public event ErrorReceivedEventHandler ErrorReceived

Raised when the controller detects a bus error or enters bus-off.