openHAB - CAN Bricklet

Warning

The openHAB bindings are still in beta, but the development was stopped.

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

Examples

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

Loopback

Download (ExampleLoopback.rules)

 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// For this example configure your CAN Bricklet to loopback mode in Paper UI.

import java.util.Arrays

val FRAME_TYPE_STANDARD_DATA = 0 as short
val FRAME_TYPE_STANDARD_REMOTE = 1 as short
val FRAME_TYPE_EXTENDED_DATA = 2 as short
val FRAME_TYPE_EXTENDED_REMOTE = 3 as short

rule "send"
when
    System started // Replace with trigger of your choice
then
    // Change XYZ to the UID of your CAN Bricklet
    val canActions = getActions("tinkerforge", "tinkerforge:brickletcan:XYZ")

    var data = newShortArrayOfSize(8)
    data.set(0, 42 as short)
    data.set(1, 23 as short)
    data.set(2, 17 as short)

    // Write standard data frame with identifier 1742 and 3 bytes of data
    canActions.brickletCANWriteFrame(FRAME_TYPE_STANDARD_DATA, 1742 as short, data, 3 as short)
end

rule "receive"
when
    // Change XYZ to the UID of your CAN Bricklet
    Channel "tinkerforge:brickletcan:XYZ:BrickletCANFrameReadable" triggered
then
    // Change XYZ to the UID of your CAN Bricklet
    val canActions = getActions("tinkerforge", "tinkerforge:brickletcan:XYZ")
    val frame = canActions.brickletCANReadFrame()
    
    val success = frame.get("success") as boolean
    if (success) {
        val frameType = frame.get("frameType") as short

        if(frameType == FRAME_TYPE_STANDARD_DATA) {
            logInfo("Example", "Frame Type: Standard Data")
        } else if(frameType == FRAME_TYPE_STANDARD_REMOTE) {
            logInfo("Example", "Frame Type: Standard Remote")
        } else if(frameType == FRAME_TYPE_EXTENDED_DATA) {
            logInfo("Example", "Frame Type: Extended Data")
        } else if(frameType == FRAME_TYPE_EXTENDED_REMOTE) {
            logInfo("Example", "Frame Type: Extended Remote")
        }

        logInfo("Example", "Identifier: " + frame.get("identifier"))
        
        // Extract data
        val len = frame.get("length") as int
        var data = frame.get("data") as short[]
        data = Arrays.copyOfRange(data, 0, len)

        logInfo("Example", "Data (Length: " + len + "): " + Arrays.toString(data))
    } else {
        logInfo("Example", "Failed to read frame")
    }
end

Thing

UID:
  • tinkerforge:brickletcan:[UID]
Required firmware version:
  • 2.0.1
Firmware update supported:
  • yes
Channels:
Actions:
Parameters:
  • Baud Rate – Type: Choice, Default: 125kbps
  • The baud rate to send/receive with.
  • Options: 10kbps, 20kbps, 50kbps, 125kbps, 250kbps, 500kbps, 800kbps, 1000kbps

  • Transceiver Mode – Type: Choice, Default: Normal
  • 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.
  • Options: Normal, Loopback, Read Only

  • Write Timeout – Type: integer, Default: 0, Min: -1, Max: 2147483647
  • 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.

Channels

Frame Readable

This channel is triggered when a new frame was received and can be read out. The channel will only trigger again if the frame was read.

Type:
  • Trigger (system.trigger)
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANFrameReadable
Read only:
  • No
Write Error Level

The write error level indicates the current level of checksum, acknowledgement, form, bit and stuffing errors during CAN bus write 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 error level is not available in read-only transceiver mode and is reset to 0 as a side effect of changing the configuration or the read filter.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Number:Dimensionless
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANWriteErrorLevel
Read only:
  • Yes
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.
Read Error Level

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

The read error level is not available in read-only transceiver mode and is reset to 0 as a side effect of changing the configuration or the read filter.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Number:Dimensionless
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANReadErrorLevel
Read only:
  • Yes
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.
Transceiver Disabled

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.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Contact
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANTransceiverDisabled
Read only:
  • No
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.
Write Timeouts

A write timeout occurs if a frame could not be transmitted before the configured write timeout expired.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Number:Dimensionless
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANWriteTimeoutCount
Read only:
  • Yes
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.
Read Register Overflows

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 the setReadFilter action) 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.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Number:Dimensionless
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANReadRegisterOverflowCount
Read only:
  • Yes
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.
Read Buffer Overflow Count

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 readFrame action.

This channel will only update after the configured update interval, not on changed values.

Type:
  • Number:Dimensionless
UID:
  • tinkerforge:brickletcan:[UID]:BrickletCANReadBufferOverflowCount
Read only:
  • Yes
Parameters:
  • Update Interval – Type: integer, Default: 1000, Unit: ms
  • Specifies the update interval in milliseconds. A value of 0 disables automatic updates.

Actions

Actions can be used in rules by creating an action object. All actions return a Map<String, Object>. Returned values can be accessed by name, sometimes the type deduction needs some hints, as shown below:

val actions = getActions("tinkerforge", "tinkerforge:brickletcan:[UID]")
val hwVersion = actions.brickletCANGetIdentity().get("hardwareVersion") as short[]
logInfo("Example", "Hardware version: " + hwVersion.get(0) + "." + hwVersion.get(1) + "." + hwVersion.get(2))

Basic Actions

brickletCANWriteFrame(short frameType, long identifier, short[] data, short length)
Parameters:
  • frameType – Type: short, Range: See constants
  • identifier – Type: long, Range: [0 to 230 - 1]
  • data – Type: short[], Length: 8, Range: [0 to 255]
  • length – Type: short, Range: [0 to 15]
Return Map:
  • success – Type: boolean

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 the thing 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 Write Error Level).

The following constants are available for this function:

For frameType:

  • val FRAME_TYPE_STANDARD_DATA = 0
  • val FRAME_TYPE_STANDARD_REMOTE = 1
  • val FRAME_TYPE_EXTENDED_DATA = 2
  • val FRAME_TYPE_EXTENDED_REMOTE = 3
brickletCANReadFrame()
Return Map:
  • success – Type: boolean
  • frameType – Type: short, Range: See constants
  • identifier – Type: long, Range: [0 to 230 - 1]
  • data – Type: short[], Length: 8, Range: [0 to 255]
  • length – Type: short, 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 WriteFrame().

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

The following constants are available for this function:

For frameType:

  • val FRAME_TYPE_STANDARD_DATA = 0
  • val FRAME_TYPE_STANDARD_REMOTE = 1
  • val FRAME_TYPE_EXTENDED_DATA = 2
  • val FRAME_TYPE_EXTENDED_REMOTE = 3
brickletCANGetConfiguration()
Return Map:
  • baudRate – Type: short, Range: See constants, Default: 3
  • transceiverMode – Type: short, Range: See constants, Default: 0
  • writeTimeout – Type: int, Range: [-1 to 231 - 1], Default: 0

Returns the configuration as set by the thing configuration.

The following constants are available for this function:

For baudRate:

  • val BAUD_RATE_10KBPS = 0
  • val BAUD_RATE_20KBPS = 1
  • val BAUD_RATE_50KBPS = 2
  • val BAUD_RATE_125KBPS = 3
  • val BAUD_RATE_250KBPS = 4
  • val BAUD_RATE_500KBPS = 5
  • val BAUD_RATE_800KBPS = 6
  • val BAUD_RATE_1000KBPS = 7

For transceiverMode:

  • val TRANSCEIVER_MODE_NORMAL = 0
  • val TRANSCEIVER_MODE_LOOPBACK = 1
  • val TRANSCEIVER_MODE_READ_ONLY = 2

Advanced Actions

brickletCANSetReadFilter(short mode, long mask, long filter1, long filter2)
Parameters:
  • mode – Type: short, Range: See constants, Default: 1
  • mask – Type: long, Range: [0 to 230 - 1]
  • filter1 – Type: long, Range: [0 to 230 - 1]
  • filter2 – Type: long, Range: [0 to 230 - 1]

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

For mode:

  • val FILTER_MODE_DISABLED = 0
  • val FILTER_MODE_ACCEPT_ALL = 1
  • val FILTER_MODE_MATCH_STANDARD = 2
  • val FILTER_MODE_MATCH_STANDARD_AND_DATA = 3
  • val FILTER_MODE_MATCH_EXTENDED = 4
brickletCANGetReadFilter()
Return Map:
  • mode – Type: short, Range: See constants, Default: 1
  • mask – Type: long, Range: [0 to 230 - 1]
  • filter1 – Type: long, Range: [0 to 230 - 1]
  • filter2 – Type: long, Range: [0 to 230 - 1]

Returns the read filter as set by SetReadFilter().

The following constants are available for this function:

For mode:

  • val FILTER_MODE_DISABLED = 0
  • val FILTER_MODE_ACCEPT_ALL = 1
  • val FILTER_MODE_MATCH_STANDARD = 2
  • val FILTER_MODE_MATCH_STANDARD_AND_DATA = 3
  • val FILTER_MODE_MATCH_EXTENDED = 4
brickletCANGetErrorLog()
Return Map:
  • writeErrorLevel – Type: short, Range: [0 to 255]
  • readErrorLevel – Type: short, Range: [0 to 255]
  • transceiverDisabled – Type: boolean
  • writeTimeoutCount – Type: long, Range: [0 to 232 - 1]
  • readRegisterOverflowCount – Type: long, Range: [0 to 232 - 1]
  • readBufferOverflowCount – Type: long, 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 the thing 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 the thing 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 SetReadFilter()) 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 ReadFrame() function.
brickletCANGetIdentity()
Return Map:
  • uid – Type: String, Length: up to 8
  • connectedUid – Type: String, Length: up to 8
  • position – Type: char, Range: ['a' to 'h', 'z']
  • hardwareVersion – Type: short[], Length: 3
    • 0: major – Type: short, Range: [0 to 255]
    • 1: minor – Type: short, Range: [0 to 255]
    • 2: revision – Type: short, Range: [0 to 255]
  • firmwareVersion – Type: short[], Length: 3
    • 0: major – Type: short, Range: [0 to 255]
    • 1: minor – Type: short, Range: [0 to 255]
    • 2: revision – Type: short, Range: [0 to 255]
  • deviceIdentifier – 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