pub struct CanBricklet { /* private fields */ }
Expand description

Communicates with CAN bus devices

Implementations

Creates an object with the unique device ID uid. This object can then be used after the IP Connection ip_connection is connected.

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by set_response_expected. For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is send and errors are silently ignored, because they cannot be detected.

See set_response_expected for the list of function ID constants available for this function.

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is send and errors are silently ignored, because they cannot be detected.

Changes the response expected flag for all setter and callback configuration functions of this device at once.

This receiver is triggered if a data or remote frame was received by the CAN transceiver.

The identifier return value follows the identifier format described for Write Frame.

For remote frames the data return value always contains invalid values.

A configurable read filter can be used to define which frames should be received by the CAN transceiver at all (see Set Read Filter).

To enable this receiver, use Enable Frame Read Receiver.

Writes a data or remote frame to the write buffer to be transmitted over the CAN transceiver.

The Bricklet supports the standard 11-bit (CAN 2.0A) and the additional extended 18-bit (CAN 2.0B) identifiers. For standard frames the Bricklet uses bit 0 to 10 from the identifier parameter as standard 11-bit identifier. For extended frames the Bricklet additionally uses bit 11 to 28 from the identifier parameter as extended 18-bit identifier.

For remote frames the data parameter is ignored.

Returns true if the frame was successfully added to the write buffer. Returns false if the frame could not be added because write buffer is already full.

The write buffer can overflow if frames are written to it at a higher rate than the Bricklet can transmitted them over the CAN transceiver. This may happen if the CAN transceiver is configured as read-only or is using a low baud rate (see Set Configuration). It can also happen if the CAN bus is congested and the frame cannot be transmitted because it constantly loses arbitration or because the CAN transceiver is currently disabled due to a high write error level (see Get Error Log).

Associated constants:

  • CANBRICKLET_FRAME_TYPE_STANDARD_DATA
  • CANBRICKLET_FRAME_TYPE_STANDARD_REMOTE
  • CANBRICKLET_FRAME_TYPE_EXTENDED_DATA
  • CANBRICKLET_FRAME_TYPE_EXTENDED_REMOTE

Tries to read the next data or remote frame from the read buffer and return it. If a frame was successfully read, then the success return value is set to true and the other return values contain the frame. If the read buffer is empty and no frame could be read, then the success return value is set to false and the other return values contain invalid data.

The identifier return value follows the identifier format described for Write Frame.

For remote frames the data return value always contains invalid data.

A configurable read filter can be used to define which frames should be received by the CAN transceiver and put into the read buffer (see Set Read Filter).

Instead of polling with this function, you can also use receivers. See the Enable Frame Read Receiver function and the get_frame_read_callback_receiver receiver.

Associated constants:

  • CANBRICKLET_FRAME_TYPE_STANDARD_DATA
  • CANBRICKLET_FRAME_TYPE_STANDARD_REMOTE
  • CANBRICKLET_FRAME_TYPE_EXTENDED_DATA
  • CANBRICKLET_FRAME_TYPE_EXTENDED_REMOTE

Enables the get_frame_read_callback_receiver receiver.

By default the receiver is disabled.

Disables the get_frame_read_callback_receiver receiver.

By default the receiver is disabled.

Returns true if the get_frame_read_callback_receiver receiver is enabled, false otherwise.

Sets the configuration for the CAN bus communication.

The baud rate can be configured in steps between 10 and 1000 kbit/s.

The CAN transceiver has three different modes:

  • Normal: Reads from and writes to the CAN bus and performs active bus error detection and acknowledgement.
  • Loopback: All reads and writes are performed internally. The transceiver is disconnected from the actual CAN bus.
  • Read-Only: Only reads from the CAN bus, but does neither active bus error detection nor acknowledgement. Only the receiving part of the transceiver is connected to the CAN bus.

The write timeout has three different modes that define how a failed frame transmission should be handled:

  • One-Shot (< 0): Only one transmission attempt will be made. If the transmission fails then the frame is discarded.
  • Infinite (= 0): Infinite transmission attempts will be made. The frame will never be discarded.
  • Milliseconds (> 0): A limited number of transmission attempts will be made. If the frame could not be transmitted successfully after the configured number of milliseconds then the frame is discarded.

The default is: 125 kbit/s, normal transceiver mode and infinite write timeout.

Associated constants:

  • CANBRICKLET_BAUD_RATE_10KBPS
  • CANBRICKLET_BAUD_RATE_20KBPS
  • CANBRICKLET_BAUD_RATE_50KBPS
  • CANBRICKLET_BAUD_RATE_125KBPS
  • CANBRICKLET_BAUD_RATE_250KBPS
  • CANBRICKLET_BAUD_RATE_500KBPS
  • CANBRICKLET_BAUD_RATE_800KBPS
  • CANBRICKLET_BAUD_RATE_1000KBPS
  • CANBRICKLET_TRANSCEIVER_MODE_NORMAL
  • CANBRICKLET_TRANSCEIVER_MODE_LOOPBACK
  • CANBRICKLET_TRANSCEIVER_MODE_READ_ONLY

Returns the configuration as set by Set Configuration.

Associated constants:

  • CANBRICKLET_BAUD_RATE_10KBPS
  • CANBRICKLET_BAUD_RATE_20KBPS
  • CANBRICKLET_BAUD_RATE_50KBPS
  • CANBRICKLET_BAUD_RATE_125KBPS
  • CANBRICKLET_BAUD_RATE_250KBPS
  • CANBRICKLET_BAUD_RATE_500KBPS
  • CANBRICKLET_BAUD_RATE_800KBPS
  • CANBRICKLET_BAUD_RATE_1000KBPS
  • CANBRICKLET_TRANSCEIVER_MODE_NORMAL
  • CANBRICKLET_TRANSCEIVER_MODE_LOOPBACK
  • CANBRICKLET_TRANSCEIVER_MODE_READ_ONLY

Set the read filter configuration. This can be used to define which frames should be received by the CAN transceiver and put into the read buffer.

The read filter has five different modes that define if and how the mask and the two filters are applied:

  • Disabled: No filtering is applied at all. All frames are received even incomplete and defective frames. This mode should be used for debugging only.
  • Accept-All: All complete and error-free frames are received.
  • Match-Standard: Only standard frames with a matching identifier are received.
  • Match-Standard-and-Data: Only standard frames with matching identifier and data bytes are received.
  • Match-Extended: Only extended frames with a matching identifier are received.

The mask and filters are used as bit masks. Their usage depends on the mode:

  • Disabled: Mask and filters are ignored.
  • Accept-All: Mask and filters are ignored.
  • Match-Standard: Bit 0 to 10 (11 bits) of mask and filters are used to match the 11-bit identifier of standard frames.
  • Match-Standard-and-Data: Bit 0 to 10 (11 bits) of mask and filters are used to match the 11-bit identifier of standard frames. Bit 11 to 18 (8 bits) and bit 19 to 26 (8 bits) of mask and filters are used to match the first and second data byte (if present) of standard frames.
  • Match-Extended: Bit 0 to 10 (11 bits) of mask and filters are used to match the standard 11-bit identifier part of extended frames. Bit 11 to 28 (18 bits) of mask and filters are used to match the extended 18-bit identifier part of extended frames.

The mask and filters are applied in this way: The mask is used to select the identifier and data bits that should be compared to the corresponding filter bits. All unselected bits are automatically accepted. All selected bits have to match one of the filters to be accepted. If all bits for the selected mode are accepted then the frame is accepted and is added to the read buffer.

Mask BitFilter BitIdentifier/Data BitResult
0XXAccept
100Accept
101Reject
110Reject
111Accept

For example, to receive standard frames with identifier 0x123 only the mode can be set to Match-Standard with 0x7FF as mask and 0x123 as filter 1 and filter 2. The mask of 0x7FF selects all 11 identifier bits for matching so that the identifier has to be exactly 0x123 to be accepted.

To accept identifier 0x123 and identifier 0x456 at the same time, just set filter 2 to 0x456 and keep mask and filter 1 unchanged.

The default mode is accept-all.

Associated constants:

  • CANBRICKLET_FILTER_MODE_DISABLED
  • CANBRICKLET_FILTER_MODE_ACCEPT_ALL
  • CANBRICKLET_FILTER_MODE_MATCH_STANDARD
  • CANBRICKLET_FILTER_MODE_MATCH_STANDARD_AND_DATA
  • CANBRICKLET_FILTER_MODE_MATCH_EXTENDED

Returns the read filter as set by Set Read Filter.

Associated constants:

  • CANBRICKLET_FILTER_MODE_DISABLED
  • CANBRICKLET_FILTER_MODE_ACCEPT_ALL
  • CANBRICKLET_FILTER_MODE_MATCH_STANDARD
  • CANBRICKLET_FILTER_MODE_MATCH_STANDARD_AND_DATA
  • CANBRICKLET_FILTER_MODE_MATCH_EXTENDED

Returns information about different kinds of errors.

The write and read error levels indicate the current level of checksum, acknowledgement, form, bit and stuffing errors during CAN bus write and read operations.

When the write error level extends 255 then the CAN transceiver gets disabled and no frames can be transmitted or received anymore. The CAN transceiver will automatically be activated again after the CAN bus is idle for a while.

The write and read error levels are not available in read-only transceiver mode (see Set Configuration) and are reset to 0 as a side effect of changing the configuration or the read filter.

The write timeout, read register and buffer overflow counts represents the number of these errors:

  • A write timeout occurs if a frame could not be transmitted before the configured write timeout expired (see Set Configuration).
  • A read register overflow occurs if the read register of the CAN transceiver still contains the last received frame when the next frame arrives. In this case the newly arrived frame is lost. This happens if the CAN transceiver receives more frames than the Bricklet can handle. Using the read filter (see Set Read Filter) can help to reduce the amount of received frames. This count is not exact, but a lower bound, because the Bricklet might not able detect all overflows if they occur in rapid succession.
  • A read buffer overflow occurs if the read buffer of the Bricklet is already full when the next frame should be read from the read register of the CAN transceiver. In this case the frame in the read register is lost. This happens if the CAN transceiver receives more frames to be added to the read buffer than are removed from the read buffer using the Read Frame function. Using the get_frame_read_callback_receiver receiver ensures that the read buffer can not overflow.

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be ‘a’, ‘b’, ‘c’ or ‘d’.

The device identifier numbers can be found here. |device_identifier_constant|

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.