Initialization of the CAN bus sensor is achieved as follows:
The dwSensorParams.protocol
expects one of the following can interfaces:
can.socket
interface for system CAN bus.can.virtual
interface for reading CAN data from binary logs.The dwSensorParams.parameters
accepts the following parameters:
filter=id:mask[+id:mask]
:
filter
is a string of +
seperated filtersid
is a hex valuemask
is a hex valueExample: filter=123:FFF+60A:FFF
See the Setting message filters section to see how these values are used.
can.socket
:device=can0[,fifo-size=1024]
device
CAN device interface, usually canx, x starting from 0ifconfig
to list available CAN interfacespidin a | grep nvcan
to list available CAN interfacesfifo-size
size of internal buffer, in number of dwCANMessage
stored.discard-threshold
the timestamp of CAN data who is older than current timestamp by threshold will be discarded.read-number
data size to be read from driver buffer in one syscall, default number is 256do-not-send
disables send functionality when set to true
.can.simulator
:Identical parameters to can.socket
.
can.virtual
:file=/path/to/file.can[,create_seek,default_timeout_us,time-offset=0]
file
path to CAN bus recordingcreate_seek
create seek tabledefault_timeout_us
period after which sensor times outtime-offset
offset applied to CAN messages timestampsThe CAN buses are setup with CAN FD if supported. If not supported sending and receiving CAN FD messages will fail.
The CAN bus sensor can be started, stopped and released as follows:
CAN messages can be read from the bus as follows:
This will populate a dwCANMessage
structure with the next CAN message received on the bus, in this example waiting up to 10000us, or 10ms, for a message to arrive. Timeout can be set to DW_TIMEOUT_INFINITE
to indicate infinite waiting until new message arrives, or 0 to immediately return and only read messages already received. The dwCANMessage
structure can hold up to 64 bytes to accommodate CAN FD messages.
To only read messages available in the internal queue, without reading new messages on the CAN bus, the dwSensorCAN_popMessage()
method can be used. Together with dwSensorCAN_processRawData()
, raw binary CAN data can be read and parsed in dwCANMessage
format, see Reading CAN Messages from Raw Data (Binary).
CAN messages can be sent on the bus as follows:
This sends a message over the CAN bus within a specified timeout. A message is guaranteed to have been sent when this method returns DW_SUCCESS
. The method can block for up-to the specified amount of time if a bus is blocked or previous dwSensorCAN_readMessage()
operation has not yet finished.
If the msg.size
value is from 9 - 64, then the message will be sent as a CAN FD message.
CAN messages be filtered by ID as follows:
This function takes an array of IDs and an array of masks to setup filters. Once the filters are set, only messages with IDs matching the filters will be read. Each call to the above will reset previously set filters. To reset all filtering, call the above with 0 as number of filters.
Internally, only messages that meet the following condition for at least one filter are read:
Masks are optional; when passing NULL
, a default mask of 1FFFFFFF
is used.
The dwSensorCAN_setUseHwTimestamps()
function allows the user to enable or disable hardware timestamping of messages on compatible CAN Bus devices. By default, hardware timestamping is enabled if available, but can be turned off if the default behavior is not working properly with the current hardware stack.
The dwSensorCAN_readMessage()
function reads the next available CAN message with a given timeout. The function call is blocking, until a CAN message is available or the timeout is exceeded. Filters can be set with dwSensorCAN_setMessageFilter()
so that only specific messages are read from the bus. Messages can be sent with dwSensorCAN_sendMessage()
.
The dwCANMessage
structure holds a CAN message, consisting of a timestamp, an ID, message data (payload) and size. The CAN bus module supports the extended CAN frame format.
The following code shows the general structure of a program setting up CAN message filters and receiving/sending messages on the bus. Only messages with IDs 0x100
-0x1FF
and 0x6AB
will be read.
For more information, refer to CAN Message Logger Sample.