Table of Contents

Class CanBus

Namespace
Pamoja.Can
Assembly
Pamoja.Can.dll

One node's place on a CAN bus.

public sealed class CanBus : IDisposable
Inheritance
CanBus
Implements
Inherited Members

Remarks

Open(string) opens a kernel interface such as can0 through SocketCAN on a Linux board, once it is up with ip link set can0 up type can bitrate 250000. Simulated() makes a bus inside the program, and Join() puts another node on the same bus. A node hears every frame the others send and none of its own, and keeps only the frames its filters pass.

A receive on a simulated bus with nothing waiting returns null at once and counts its timeout in WaitedMicros. A failure throws PamojaException with the reason.

Properties

Kind

What the bus is.

public CanBusKind Kind { get; }

Property Value

CanBusKind

Received

How many frames the node has received.

public long Received { get; }

Property Value

long

Sent

How many frames the node has sent.

public long Sent { get; }

Property Value

long

WaitedMicros

How long receives on the node have waited without a frame, in microseconds, whether or not the process slept through it.

public ulong WaitedMicros { get; }

Property Value

ulong

Methods

ClearFilters()

Keeps every frame again, as a node does when it joins.

public void ClearFilters()

Exceptions

PamojaException

The kernel refuses the change.

Dispose()

Releases the node; it leaves the bus.

public void Dispose()

Join()

Puts another node on the same bus.

public CanBus Join()

Returns

CanBus

The new node.

Exceptions

PamojaException

The kernel refuses another socket on the interface.

Open(string)

Opens a kernel CAN interface through SocketCAN, as one node on its bus.

public static CanBus Open(string interfaceName)

Parameters

interfaceName string

can0 for the first controller, vcan0 for a virtual one.

Returns

CanBus

The node.

Exceptions

PlatformNotSupportedException

The platform is not Linux.

PamojaException

The interface does not exist or cannot be bound.

Receive(TimeSpan)

Takes the next frame the node keeps, waiting up to a timeout for one.

public CanFrame? Receive(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait.

Returns

CanFrame

The frame, or null when the timeout passed with nothing.

Exceptions

PamojaException

The interface fails.

Send(CanFrame)

Sends a frame to every other node on the bus.

public void Send(CanFrame frame)

Parameters

frame CanFrame

The frame.

Exceptions

PamojaException

The kernel refuses the frame.

SetFilters(params CanFilter[])

Keeps only the frames that pass at least one of the filters, from now on; an empty list keeps nothing.

public void SetFilters(params CanFilter[] filters)

Parameters

filters CanFilter[]

The filters, at most 512.

Exceptions

PamojaException

The kernel refuses the filters.

Simulated()

A new bus inside the program, with this node the first on it.

public static CanBus Simulated()

Returns

CanBus

The node.