Shell - CAN Bricklet

This is the description of the Shell API bindings for the CAN Bricklet. General information and technical specifications for the CAN Bricklet are summarized in its hardware description.

An installation guide for the Shell API bindings is part of their general description.

Examples

The example code below is Public Domain (CC0 1.0).

Loopback

Download (example-loopback.sh)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
# Connects to localhost:4223 by default, use --host and --port to change this

uid=XYZ # Change XYZ to the UID of your CAN Bricklet

# Configure transceiver for loopback mode
tinkerforge call can-bricklet $uid set-configuration baud-rate-1000kbps transceiver-mode-loopback 0

# Handle incoming frame read callbacks
tinkerforge dispatch can-bricklet $uid frame-read &

# Enable frame read callback
tinkerforge call can-bricklet $uid enable-frame-read-callback

# Write standard data frame with identifier 1742 and 3 bytes of data
tinkerforge call can-bricklet $uid write-frame frame-type-standard-data 1742 42,23,1,.. 3

echo "Press key to exit"; read dummy

tinkerforge call can-bricklet $uid disable-frame-read-callback

kill -- -$$ # Stop callback dispatch in background

API

Possible exit codes for all tinkerforge commands are:

  • 1: interrupted (ctrl+c)
  • 2: syntax error
  • 21: Python 2.5 or newer is required
  • 22: Python argparse module is missing
  • 23: socket error
  • 24: other exception
  • 25: invalid placeholder in format string
  • 26: authentication error
  • 201: timeout occurred
  • 209: invalid argument value
  • 210: function is not supported
  • 211: unknown error

Command Structure

The common options of the call and dispatch commands are documented here. The specific command structure is shown below.

tinkerforge call can-bricklet [<option>..] <uid> <function> [<argument>..]
Parameters:
  • <uid> – Type: String
  • <function> – Type: String

The call command is used to call a function of the CAN Bricklet. It can take several options:

  • --help shows help for the specific call command and exits
  • --list-functions shows a list of known functions of the CAN Bricklet and exits
tinkerforge dispatch can-bricklet [<option>..] <uid> <callback>
Parameters:
  • <uid> – Type: String
  • <callback> – Type: String

The dispatch command is used to dispatch a callback of the CAN Bricklet. It can take several options:

  • --help shows help for the specific dispatch command and exits
  • --list-callbacks shows a list of known callbacks of the CAN Bricklet and exits
tinkerforge call can-bricklet <uid> <function> [<option>..] [<argument>..]
Parameters:
  • <uid> – Type: String
  • <function> – Type: String

The <function> to be called can take different options depending of its kind. All functions can take the following options:

  • --help shows help for the specific function and exits

Getter functions can take the following options:

  • --execute <command> shell command line to execute for each incoming response (see section about output formatting for details)

Setter functions can take the following options:

  • --expect-response requests response and waits for it

The --expect-response option for setter functions allows to detect timeouts and other error conditions calls of setters as well. The device will then send a response for this purpose. If this option is not given for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

tinkerforge dispatch can-bricklet <uid> <callback> [<option>..]
Parameters:
  • <uid> – Type: String
  • <callback> – Type: String

The <callback> to be dispatched can take several options:

  • --help shows help for the specific callback and exits
  • --execute <command> shell command line to execute for each incoming response (see section about output formatting for details)

Basic Functions

tinkerforge call can-bricklet <uid> write-frame <frame-type> <identifier> <data> <length>
Parameters:
  • <frame-type> – Type: Int, Range: See symbols
  • <identifier> – Type: Int, Range: [0 to 230 - 1]
  • <data> – Type: Int Array, Length: 8, Range: [0 to 255]
  • <length> – Type: Int, Range: [0 to 15]
Output:
  • success – Type: Bool

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).

The following symbols are available for this function:

For <frame-type>:

  • frame-type-standard-data = 0
  • frame-type-standard-remote = 1
  • frame-type-extended-data = 2
  • frame-type-extended-remote = 3
tinkerforge call can-bricklet <uid> read-frame
Output:
  • success – Type: Bool
  • frame-type – Type: Int, Range: See symbols
  • identifier – Type: Int, Range: [0 to 230 - 1]
  • data – Type: Int Array, Length: 8, Range: [0 to 255]
  • length – Type: Int, Range: [0 to 15]

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 callbacks. See the enable-frame-read-callback function and the frame-read callback.

The following symbols are available for this function:

For frame-type:

  • frame-type-standard-data = 0
  • frame-type-standard-remote = 1
  • frame-type-extended-data = 2
  • frame-type-extended-remote = 3
tinkerforge call can-bricklet <uid> set-configuration <baud-rate> <transceiver-mode> <write-timeout>
Parameters:
  • <baud-rate> – Type: Int, Range: See symbols, Default: 3
  • <transceiver-mode> – Type: Int, Range: See symbols, Default: 0
  • <write-timeout> – Type: Int, Range: [-1 to 231 - 1], Default: 0
Output:
  • no output

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 (= -1): 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 following symbols are available for this function:

For <baud-rate>:

  • baud-rate-10kbps = 0
  • baud-rate-20kbps = 1
  • baud-rate-50kbps = 2
  • baud-rate-125kbps = 3
  • baud-rate-250kbps = 4
  • baud-rate-500kbps = 5
  • baud-rate-800kbps = 6
  • baud-rate-1000kbps = 7

For <transceiver-mode>:

  • transceiver-mode-normal = 0
  • transceiver-mode-loopback = 1
  • transceiver-mode-read-only = 2
tinkerforge call can-bricklet <uid> get-configuration
Output:
  • baud-rate – Type: Int, Range: See symbols, Default: 3
  • transceiver-mode – Type: Int, Range: See symbols, Default: 0
  • write-timeout – Type: Int, Range: [-1 to 231 - 1], Default: 0

Returns the configuration as set by set-configuration.

The following symbols are available for this function:

For baud-rate:

  • baud-rate-10kbps = 0
  • baud-rate-20kbps = 1
  • baud-rate-50kbps = 2
  • baud-rate-125kbps = 3
  • baud-rate-250kbps = 4
  • baud-rate-500kbps = 5
  • baud-rate-800kbps = 6
  • baud-rate-1000kbps = 7

For transceiver-mode:

  • transceiver-mode-normal = 0
  • transceiver-mode-loopback = 1
  • transceiver-mode-read-only = 2

Advanced Functions

tinkerforge call can-bricklet <uid> set-read-filter <mode> <mask> <filter1> <filter2>
Parameters:
  • <mode> – Type: Int, Range: See symbols, Default: 1
  • <mask> – Type: Int, Range: [0 to 230 - 1]
  • <filter1> – Type: Int, Range: [0 to 230 - 1]
  • <filter2> – Type: Int, Range: [0 to 230 - 1]
Output:
  • no output

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 Bit Filter Bit Identifier/Data Bit Result
0 X X Accept
1 0 0 Accept
1 0 1 Reject
1 1 0 Reject
1 1 1 Accept

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 following symbols are available for this function:

For <mode>:

  • filter-mode-disabled = 0
  • filter-mode-accept-all = 1
  • filter-mode-match-standard = 2
  • filter-mode-match-standard-and-data = 3
  • filter-mode-match-extended = 4
tinkerforge call can-bricklet <uid> get-read-filter
Output:
  • mode – Type: Int, Range: See symbols, Default: 1
  • mask – Type: Int, Range: [0 to 230 - 1]
  • filter1 – Type: Int, Range: [0 to 230 - 1]
  • filter2 – Type: Int, Range: [0 to 230 - 1]

Returns the read filter as set by set-read-filter.

The following symbols are available for this function:

For mode:

  • filter-mode-disabled = 0
  • filter-mode-accept-all = 1
  • filter-mode-match-standard = 2
  • filter-mode-match-standard-and-data = 3
  • filter-mode-match-extended = 4
tinkerforge call can-bricklet <uid> get-error-log
Output:
  • write-error-level – Type: Int, Range: [0 to 255]
  • read-error-level – Type: Int, Range: [0 to 255]
  • transceiver-disabled – Type: Bool
  • write-timeout-count – Type: Int, Range: [0 to 232 - 1]
  • read-register-overflow-count – Type: Int, Range: [0 to 232 - 1]
  • read-buffer-overflow-count – Type: Int, Range: [0 to 232 - 1]

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 exceeds 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 frame-read callback ensures that the read buffer can not overflow.
tinkerforge call can-bricklet <uid> get-identity
Output:
  • uid – Type: String, Length: up to 8
  • connected-uid – Type: String, Length: up to 8
  • position – Type: Char, Range: [a to h, z]
  • hardware-version – Type: Int Array, Length: 3
    • 0: major – Type: Int, Range: [0 to 255]
    • 1: minor – Type: Int, Range: [0 to 255]
    • 2: revision – Type: Int, Range: [0 to 255]
  • firmware-version – Type: Int Array, Length: 3
    • 0: major – Type: Int, Range: [0 to 255]
    • 1: minor – Type: Int, Range: [0 to 255]
    • 2: revision – Type: Int, Range: [0 to 255]
  • device-identifier – Type: Int, Range: [0 to 216 - 1]

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', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here

Callback Configuration Functions

tinkerforge call can-bricklet <uid> enable-frame-read-callback
Output:
  • no output

Enables the frame-read callback.

By default the callback is disabled. Enabling this callback will disable the frame-readable callback.

tinkerforge call can-bricklet <uid> disable-frame-read-callback
Output:
  • no output

Disables the frame-read callback.

By default the callback is disabled.

tinkerforge call can-bricklet <uid> is-frame-read-callback-enabled
Output:
  • enabled – Type: Bool, Default: false

Returns true if the frame-read callback is enabled, false otherwise.

tinkerforge call can-bricklet <uid> set-frame-readable-callback-configuration <enabled>
Parameters:
  • <enabled> – Type: Bool, Default: false
Output:
  • no output

Enables/disables the frame-readable callback.

By default the callback is disabled. Enabling this callback will disable the frame-read callback.

New in version 2.0.1 (Plugin).

tinkerforge call can-bricklet <uid> get-frame-readable-callback-configuration
Output:
  • enabled – Type: Bool, Default: false

Returns true if the frame-readable callback is enabled, false otherwise.

New in version 2.0.1 (Plugin).

Callbacks

Callbacks can be used to receive time critical or recurring data from the device:

tinkerforge dispatch can-bricklet <uid> example

The available callbacks are described below.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

tinkerforge dispatch can-bricklet <uid> frame-read
Output:
  • frame-type – Type: Int, Range: See symbols
  • identifier – Type: Int, Range: [0 to 230 - 1]
  • data – Type: Int Array, Length: 8, Range: [0 to 255]
  • length – Type: Int, Range: [0 to 15]

This callback 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 callback, use enable-frame-read-callback.

The following symbols are available for this function:

For frame-type:

  • frame-type-standard-data = 0
  • frame-type-standard-remote = 1
  • frame-type-extended-data = 2
  • frame-type-extended-remote = 3
tinkerforge dispatch can-bricklet <uid> frame-readable
Output:
  • no output

This callback is triggered if a data or remote frame was received by the CAN transceiver. The received frame can be read with read-frame. If additional frames are received, but read-frame was not called yet, the callback will not trigger again.

A configurable read filter can be used to define which frames should be received by the CAN transceiver and put into the read queue (see set-read-filter).

To enable this callback, use set-frame-readable-callback-configuration.

New in version 2.0.1 (Plugin).