MQTT - CAN Bricklet

This is the description of the MQTT 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 MQTT API bindings is part of their general description.

Examples

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

Loopback

Download (example-loopback.txt)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Change XYZ to the UID of your CAN Bricklet

setup:
    # Configure transceiver for loopback mode
    publish '{"baud_rate": "1000kbps", "transceiver_mode": "loopback", "write_timeout": 0}' to tinkerforge/request/can_bricklet/XYZ/set_configuration 
    
    # Handle incoming frame read callbacks
    subscribe to tinkerforge/callback/can_bricklet/XYZ/frame_read
        if a message arrives:
            # message contains frame_type as int with symbols, identifier as int and data as int array
            if frame_type == "StandardData"
                print "Frame Type: Standard Data"
            elseif frame_type == "StandardRemote"
                print "Frame Type: Standard Remote"
            elseif frame_type == "ExtendedData"
                print "Frame Type: Extended Remote"
            elseif frame_type == "ExtendedRemote"
                print "Frame Type: Extended Remote"
            
            print "Identifier: " + identifier
            print "Data: " + data
            print "\n"
        endif
    endsubscribe
    
    publish '{"register": true}' to tinkerforge/register/can_bricklet/XYZ/frame_read
    
    # Enable frame read callback
    publish '' to tinkerforge/request/can_bricklet/XYZ/enable_frame_read_callback 
    
    # Write standard data frame with identifier 1742 and 3 bytes of data
    publish '{"frame_type": "StandardData", "identifier": 1742, "data": [42, 23, 17, 0, 0, 0, 0, 0], "length": 3}' to tinkerforge/request/can_bricklet/XYZ/write_frame 

cleanup:
    # If you are done, run this to clean up
    publish '' to tinkerforge/request/can_bricklet/XYZ/disable_frame_read_callback 

API

All published payload to an from the MQTT bindings is in the JSON format.

If an error occures, the bindings publish a JSON object containing the error message as attribute "_ERROR". It is published on the corresponding response topic: .../response/... for .../request/... and .../callback/... for .../register/....

Basic Functions

request/can_bricklet/<UID>/write_frame
Request payload:
 
  • frame_type -- int (has symbols)
  • identifier -- int
  • data -- [int,... (x8)]
  • length -- int
Response payload:
 
  • success -- 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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/get_error_log).

The following symbols are available for this function:

for frame_type:

  • "StandardData" = 0
  • "StandardRemote" = 1
  • "ExtendedData" = 2
  • "ExtendedRemote" = 3
request/can_bricklet/<UID>/read_frame
Response payload:
 
  • success -- bool
  • frame_type -- int (has symbols)
  • identifier -- int
  • data -- [int,... (x8)]
  • length -- int

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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/set_read_filter).

Instead of polling with this function, you can also use callbacks. See the request/can_bricklet/<UID>/enable_frame_read_callback function and the register/can_bricklet/<UID>/frame_read callback.

The following symbols are available for this function:

for frame_type:

  • "StandardData" = 0
  • "StandardRemote" = 1
  • "ExtendedData" = 2
  • "ExtendedRemote" = 3
request/can_bricklet/<UID>/set_configuration
Request payload:
 
  • baud_rate -- int (has symbols)
  • transceiver_mode -- int (has symbols)
  • write_timeout -- int

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.

The following symbols are available for this function:

for baud_rate:

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

for transceiver_mode:

  • "Normal" = 0
  • "Loopback" = 1
  • "ReadOnly" = 2
request/can_bricklet/<UID>/get_configuration
Response payload:
 
  • baud_rate -- int (has symbols)
  • transceiver_mode -- int (has symbols)
  • write_timeout -- int

Returns the configuration as set by request/can_bricklet/<UID>/set_configuration.

The following symbols are available for this function:

for baud_rate:

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

for transceiver_mode:

  • "Normal" = 0
  • "Loopback" = 1
  • "ReadOnly" = 2

Advanced Functions

request/can_bricklet/<UID>/set_read_filter
Request payload:
 
  • mode -- int (has symbols)
  • mask -- int
  • filter1 -- int
  • filter2 -- int

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 default mode is accept-all.

The following symbols are available for this function:

for mode:

  • "Disabled" = 0
  • "AcceptAll" = 1
  • "MatchStandard" = 2
  • "MatchStandardAndData" = 3
  • "MatchExtended" = 4
request/can_bricklet/<UID>/get_read_filter
Response payload:
 
  • mode -- int (has symbols)
  • mask -- int
  • filter1 -- int
  • filter2 -- int

Returns the read filter as set by request/can_bricklet/<UID>/set_read_filter.

The following symbols are available for this function:

for mode:

  • "Disabled" = 0
  • "AcceptAll" = 1
  • "MatchStandard" = 2
  • "MatchStandardAndData" = 3
  • "MatchExtended" = 4
request/can_bricklet/<UID>/get_error_log
Response payload:
 
  • write_error_level -- int
  • read_error_level -- int
  • transceiver_disabled -- bool
  • write_timeout_count -- int
  • read_register_overflow_count -- int
  • read_buffer_overflow_count -- int

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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/read_frame function. Using the register/can_bricklet/<UID>/frame_read callback ensures that the read buffer can not overflow.
request/can_bricklet/<UID>/get_identity
Response payload:
 
  • uid -- string
  • connected_uid -- string
  • position -- string
  • hardware_version -- [int,int,int]
  • firmware_version -- [int,int,int]
  • device_identifier -- int (has symbols)
  • _display_name -- string

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. If symbolic output is not disabled, the device identifier is mapped to the corresponding name in the format used in topics.

The display name contains the CAN's name in a human readable form.

Callback Configuration Functions

request/can_bricklet/<UID>/enable_frame_read_callback

Enables the register/can_bricklet/<UID>/frame_read callback.

By default the callback is disabled.

request/can_bricklet/<UID>/disable_frame_read_callback

Disables the register/can_bricklet/<UID>/frame_read callback.

By default the callback is disabled.

request/can_bricklet/<UID>/is_frame_read_callback_enabled
Response payload:
 
  • enabled -- bool

Returns true if the register/can_bricklet/<UID>/frame_read callback is enabled, false otherwise.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the corresponding .../register/... topic and an optional suffix. This suffix can be used to deregister the callback later.

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.

register/can_bricklet/<UID>/frame_read
Request payload:
 
  • register -- bool
Response payload:
 
  • frame_type -- int (has symbols)
  • identifier -- int
  • data -- [int,... (x8)]
  • length -- int

A callback can be registered for this event by publishing to the .../register/can_bricklet/<UID>/frame_read[/<SUFFIX>] topic with the payload "true". An added callback can be removed by publishing to the same topic with the payload "false". To support multiple (de)registrations, e.g. for message filtering, an optional suffix can be used.

If the callback is triggered, a message with it's payload is published under the corresponding .../callback/can_bricklet/<UID>/frame_read[/<SUFFIX>] topic for each registered suffix.

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 request/can_bricklet/<UID>/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 request/can_bricklet/<UID>/set_read_filter).

To enable this callback, use request/can_bricklet/<UID>/enable_frame_read_callback.

The following symbols are available for this function:

for frame_type:

  • "StandardData" = 0
  • "StandardRemote" = 1
  • "ExtendedData" = 2
  • "ExtendedRemote" = 3