MATLAB/Octave - CAN Bricklet 2.0

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

An installation guide for the MATLAB/Octave API bindings is part of their general description.

Examples

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

Loopback (MATLAB)

Download (matlab_example_loopback.m)

 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
function matlab_example_loopback()
    import com.tinkerforge.IPConnection;
    import com.tinkerforge.BrickletCANV2;

    HOST = 'localhost';
    PORT = 4223;
    UID = 'XYZ'; % Change XYZ to the UID of your CAN Bricklet 2.0

    ipcon = IPConnection(); % Create IP connection
    can = handle(BrickletCANV2(UID, ipcon), 'CallbackProperties'); % Create device object

    ipcon.connect(HOST, PORT); % Connect to brickd
    % Don't use device before ipcon is connected

    % Configure transceiver for loopback mode
    can.setTransceiverConfiguration(1000000, 625, ...
                                    BrickletCANV2.TRANSCEIVER_MODE_LOOPBACK);

    % Register frame read callback to function cb_frame_read
    set(can, 'FrameReadCallback', @(h, e) cb_frame_read(e));

    % Enable frame read callback
    can.setFrameReadCallbackConfiguration(true);

    % Write standard data frame with identifier 1742 and 3 bytes of data
    can.writeFrame(BrickletCANV2.FRAME_TYPE_STANDARD_DATA, 1742, [42 23 17]);

    input('Press key to exit\n', 's');

    can.setFrameReadCallbackConfiguration(false);

    ipcon.disconnect();
end

% Callback function for frame read callback
function cb_frame_read(e)
    if e.frameType == com.tinkerforge.BrickletCANV2.FRAME_TYPE_STANDARD_DATA
        fprintf('Frame Type: Standard Data\n');
    elseif e.frameType == com.tinkerforge.BrickletCANV2.FRAME_TYPE_STANDARD_REMOTE
        fprintf('Frame Type: Standard Remote\n');
    elseif e.frameType == com.tinkerforge.BrickletCANV2.FRAME_TYPE_EXTENDED_DATA
        fprintf('Frame Type: Extended Data\n');
    elseif e.frameType == com.tinkerforge.BrickletCANV2.FRAME_TYPE_EXTENDED_REMOTE
        fprintf('Frame Type: Extended Remote\n');
    end

    fprintf('Identifier: %i\n', e.identifier);
    fprintf('Data (Length: %i):', e.data.length);

    for i = 1:min(e.data.length, 8)
        fprintf(' %i', e.data(i));
    end

    fprintf('\n');
    fprintf('\n');
end

Loopback (Octave)

Download (octave_example_loopback.m)

 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
61
62
function octave_example_loopback()
    more off;

    HOST = "localhost";
    PORT = 4223;
    UID = "XYZ"; % Change XYZ to the UID of your CAN Bricklet 2.0

    ipcon = javaObject("com.tinkerforge.IPConnection"); % Create IP connection
    can = javaObject("com.tinkerforge.BrickletCANV2", UID, ipcon); % Create device object

    ipcon.connect(HOST, PORT); % Connect to brickd
    % Don't use device before ipcon is connected

    % Configure transceiver for loopback mode
    can.setTransceiverConfiguration(1000000, 625, can.TRANSCEIVER_MODE_LOOPBACK);

    % Register frame read callback to function cb_frame_read
    can.addFrameReadCallback(@cb_frame_read);

    % Enable frame read callback
    can.setFrameReadCallbackConfiguration(true);

    % Write standard data frame with identifier 1742 and 3 bytes of data
    can.writeFrame(can.FRAME_TYPE_STANDARD_DATA, 1742, [42 23 17]);

    input("Press key to exit\n", "s");

    can.setFrameReadCallbackConfiguration(false);

    ipcon.disconnect();
end

% Callback function for frame read callback
function cb_frame_read(e)
    if e.frameType == 0
        fprintf("Frame Type: Standard Data\n");
    elseif e.frameType == 1
        fprintf("Frame Type: Standard Remote\n");
    elseif e.frameType == 2
        fprintf("Frame Type: Extended Data\n");
    elseif e.frameType == 3
        fprintf("Frame Type: Extended Remote\n");
    end

    fprintf("Identifier: %d\n", java2int(e.identifier));
    fprintf("Data (Length: %d):", e.data.length);

    for i = 1:min(e.data.length, 8)
        fprintf(" %d", java2int(e.data(i)));
    end

    fprintf("\n");
    fprintf("\n");
end

function int = java2int(value)
    if compare_versions(version(), "3.8", "<=")
        int = value.intValue();
    else
        int = value;
    end
end

API

Generally, every method of the MATLAB bindings that returns a value can throw a TimeoutException. This exception gets thrown if the device did not respond. If a cable based connection is used, it is unlikely that this exception gets thrown (assuming nobody unplugs the device). However, if a wireless connection is used, timeouts will occur if the distance to the device gets too big.

Beside the TimeoutException there is also a NotConnectedException that is thrown if a method needs to communicate with the device while the IP Connection is not connected.

Since the MATLAB bindings are based on Java and Java does not support multiple return values and return by reference is not possible for primitive types, we use small classes that only consist of member variables. The member variables of the returned objects are described in the corresponding method descriptions.

The package for all Brick/Bricklet bindings and the IP Connection is com.tinkerforge.*

All methods listed below are thread-safe.

Basic Functions

class BrickletCANV2(String uid, IPConnection ipcon)
Parameters:
  • uid – Type: String
  • ipcon – Type: IPConnection
Returns:
  • canV2 – Type: BrickletCANV2

Creates an object with the unique device ID uid.

In MATLAB:

import com.tinkerforge.BrickletCANV2;

canV2 = BrickletCANV2('YOUR_DEVICE_UID', ipcon);

In Octave:

canV2 = java_new("com.tinkerforge.BrickletCANV2", "YOUR_DEVICE_UID", ipcon);

This object can then be used after the IP Connection is connected.

boolean BrickletCANV2.writeFrame(int frameType, long identifier, int[] data)
Parameters:
  • frameType – Type: int, Range: See constants
  • identifier – Type: long, Range: [0 to 230 - 1]
  • data – Type: int[], Length: variable, Range: [0 to 255]
Returns:
  • success – Type: boolean

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

The Bricklet supports the standard 11-bit (CAN 2.0A) and the additional extended 29-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 uses bit 0 to 28 from the identifier parameter as extended 29-bit identifier.

The data parameter can be up to 15 bytes long. For data frames up to 8 bytes will be used as the actual data. The length (DLC) field in the data or remote frame will be set to the actual length of the data parameter. This allows to transmit data and remote frames with excess length. For remote frames only the length of the data parameter is used. The actual data bytes are ignored.

Returns true if the frame was successfully added to the write queue. Returns false if the frame could not be added because write queue is already full or because the write buffer or the write backlog are configured with a size of zero (see setQueueConfiguration()).

The write queue 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 setTransceiverConfiguration()). 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 getErrorLog()).

The following constants are available for this function:

For frameType:

  • BrickletCANV2.FRAME_TYPE_STANDARD_DATA = 0
  • BrickletCANV2.FRAME_TYPE_STANDARD_REMOTE = 1
  • BrickletCANV2.FRAME_TYPE_EXTENDED_DATA = 2
  • BrickletCANV2.FRAME_TYPE_EXTENDED_REMOTE = 3
BrickletCANV2.ReadFrame BrickletCANV2.readFrame()
Return Object:
  • success – Type: boolean
  • frameType – Type: int, Range: See constants
  • identifier – Type: long, Range: [0 to 230 - 1]
  • data – Type: int[], Length: variable, Range: [0 to 255]

Tries to read the next data or remote frame from the read queue and returns 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 queue 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().

The data return value can be up to 15 bytes long. For data frames up to the first 8 bytes are the actual received data. All bytes after the 8th byte are always zero and only there to indicate the length of a data or remote frame with excess length. For remote frames the length of the data return value represents the requested length. The actual data bytes are always zero.

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

Instead of polling with this function, you can also use callbacks. See the setFrameReadCallbackConfiguration() function and the FrameReadCallback callback.

The following constants are available for this function:

For frameType:

  • BrickletCANV2.FRAME_TYPE_STANDARD_DATA = 0
  • BrickletCANV2.FRAME_TYPE_STANDARD_REMOTE = 1
  • BrickletCANV2.FRAME_TYPE_EXTENDED_DATA = 2
  • BrickletCANV2.FRAME_TYPE_EXTENDED_REMOTE = 3
void BrickletCANV2.setTransceiverConfiguration(long baudRate, int samplePoint, int transceiverMode)
Parameters:
  • baudRate – Type: long, Unit: 1 bit/s, Range: [10000 to 1000000], Default: 125000
  • samplePoint – Type: int, Unit: 1/10 %, Range: [500 to 900], Default: 625
  • transceiverMode – Type: int, Range: See constants, Default: 0

Sets the transceiver configuration for the CAN bus communication.

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

For transceiverMode:

  • BrickletCANV2.TRANSCEIVER_MODE_NORMAL = 0
  • BrickletCANV2.TRANSCEIVER_MODE_LOOPBACK = 1
  • BrickletCANV2.TRANSCEIVER_MODE_READ_ONLY = 2
BrickletCANV2.TransceiverConfiguration BrickletCANV2.getTransceiverConfiguration()
Return Object:
  • baudRate – Type: long, Unit: 1 bit/s, Range: [10000 to 1000000], Default: 125000
  • samplePoint – Type: int, Unit: 1/10 %, Range: [500 to 900], Default: 625
  • transceiverMode – Type: int, Range: See constants, Default: 0

Returns the configuration as set by setTransceiverConfiguration().

The following constants are available for this function:

For transceiverMode:

  • BrickletCANV2.TRANSCEIVER_MODE_NORMAL = 0
  • BrickletCANV2.TRANSCEIVER_MODE_LOOPBACK = 1
  • BrickletCANV2.TRANSCEIVER_MODE_READ_ONLY = 2

Advanced Functions

void BrickletCANV2.setQueueConfiguration(int writeBufferSize, int writeBufferTimeout, int writeBacklogSize, int[] readBufferSizes, int readBacklogSize)
Parameters:
  • writeBufferSize – Type: int, Range: [0 to 32], Default: 8
  • writeBufferTimeout – Type: int, Range: [-1 to 231 - 1], Default: 0
  • writeBacklogSize – Type: int, Range: [0 to 768], Default: 383
  • readBufferSizes – Type: int[], Length: variable, Range: [-32 to -1, 1 to 32], Default: [16, -8]
  • readBacklogSize – Type: int, Range: [0 to 768], Default: 383

Sets the write and read queue configuration.

The CAN transceiver has 32 buffers in total in hardware for transmitting and receiving frames. Additionally, the Bricklet has a backlog for 768 frames in total in software. The buffers and the backlog can be freely assigned to the write and read queues.

writeFrame() writes a frame into the write backlog. The Bricklet moves the frame from the backlog into a free write buffer. The CAN transceiver then transmits the frame from the write buffer to the CAN bus. If there are no write buffers (write_buffer_size is zero) or there is no write backlog (write_backlog_size is zero) then no frames can be transmitted and writeFrame() returns always false.

The CAN transceiver receives a frame from the CAN bus and stores it into a free read buffer. The Bricklet moves the frame from the read buffer into the read backlog. readFrame() reads the frame from the read backlog and returns it. If there are no read buffers (read_buffer_sizes is empty) or there is no read backlog (read_backlog_size is zero) then no frames can be received and readFrame() returns always false.

There can be multiple read buffers, because the CAN transceiver cannot receive data and remote frames into the same read buffer. A positive read buffer size represents a data frame read buffer and a negative read buffer size represents a remote frame read buffer. A read buffer size of zero is not allowed. By default the first read buffer is configured for data frames and the second read buffer is configured for remote frame. There can be up to 32 different read buffers, assuming that no write buffer is used. Each read buffer has its own filter configuration (see setReadFilterConfiguration()).

A valid queue configuration fulfills these conditions:

write_buffer_size + abs(read_buffer_size_0) + abs(read_buffer_size_1) + ... + abs(read_buffer_size_31) <= 32
write_backlog_size + read_backlog_size <= 768

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

  • Single-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 current content of the queues is lost when this function is called.

BrickletCANV2.QueueConfiguration BrickletCANV2.getQueueConfiguration()
Return Object:
  • writeBufferSize – Type: int, Range: [0 to 32], Default: 8
  • writeBufferTimeout – Type: int, Range: [-1 to 231 - 1], Default: 0
  • writeBacklogSize – Type: int, Range: [0 to 768], Default: 383
  • readBufferSizes – Type: int[], Length: variable, Range: [-32 to -1, 1 to 32], Default: [16, -8]
  • readBacklogSize – Type: int, Range: [0 to 768], Default: 383

Returns the queue configuration as set by setQueueConfiguration().

void BrickletCANV2.setReadFilterConfiguration(int bufferIndex, int filterMode, long filterMask, long filterIdentifier)
Parameters:
  • bufferIndex – Type: int, Range: [0 to 31]
  • filterMode – Type: int, Range: See constants, Default: 0
  • filterMask – Type: long, Range: [0 to 230 - 1]
  • filterIdentifier – Type: long, Range: [0 to 230 - 1]

Set the read filter configuration for the given read buffer index. 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 four different modes that define if and how the filter mask and the filter identifier are applied:

  • Accept-All: All frames are received.
  • Match-Standard-Only: Only standard frames with a matching identifier are received.
  • Match-Extended-Only: Only extended frames with a matching identifier are received.
  • Match-Standard-And-Extended: Standard and extended frames with a matching identifier are received.

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

  • Accept-All: Mask and identifier are ignored.
  • Match-Standard-Only: Bit 0 to 10 (11 bits) of filter mask and filter identifier are used to match the 11-bit identifier of standard frames.
  • Match-Extended-Only: Bit 0 to 28 (29 bits) of filter mask and filter identifier are used to match the 29-bit identifier of extended frames.
  • Match-Standard-And-Extended: Bit 18 to 28 (11 bits) of filter mask and filter identifier are used to match the 11-bit identifier of standard frames, bit 0 to 17 (18 bits) are ignored in this case. Bit 0 to 28 (29 bits) of filter mask and filter identifier are used to match the 29-bit identifier of extended frames.

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

Filter Mask Bit Filter Identifier Bit Frame Identifier 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-Only with 0x7FF as mask and 0x123 as identifier. 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.

There can be up to 32 different read filters configured at the same time, because there can be up to 32 read buffer (see setQueueConfiguration()).

The default mode is accept-all for all read buffers.

The following constants are available for this function:

For filterMode:

  • BrickletCANV2.FILTER_MODE_ACCEPT_ALL = 0
  • BrickletCANV2.FILTER_MODE_MATCH_STANDARD_ONLY = 1
  • BrickletCANV2.FILTER_MODE_MATCH_EXTENDED_ONLY = 2
  • BrickletCANV2.FILTER_MODE_MATCH_STANDARD_AND_EXTENDED = 3
BrickletCANV2.ReadFilterConfiguration BrickletCANV2.getReadFilterConfiguration(int bufferIndex)
Parameters:
  • bufferIndex – Type: int, Range: [0 to 31]
Return Object:
  • filterMode – Type: int, Range: See constants, Default: 0
  • filterMask – Type: long, Range: [0 to 230 - 1]
  • filterIdentifier – Type: long, Range: [0 to 230 - 1]

Returns the read filter configuration as set by setReadFilterConfiguration().

The following constants are available for this function:

For filterMode:

  • BrickletCANV2.FILTER_MODE_ACCEPT_ALL = 0
  • BrickletCANV2.FILTER_MODE_MATCH_STANDARD_ONLY = 1
  • BrickletCANV2.FILTER_MODE_MATCH_EXTENDED_ONLY = 2
  • BrickletCANV2.FILTER_MODE_MATCH_STANDARD_AND_EXTENDED = 3
BrickletCANV2.ErrorLog BrickletCANV2.getErrorLog()
Return Object:
  • transceiverState – Type: int, Range: See constants
  • transceiverWriteErrorLevel – Type: int, Range: [0 to 255]
  • transceiverReadErrorLevel – Type: int, Range: [0 to 255]
  • transceiverStuffingErrorCount – Type: long, Range: [0 to 232 - 1]
  • transceiverFormatErrorCount – Type: long, Range: [0 to 232 - 1]
  • transceiverACKErrorCount – Type: long, Range: [0 to 232 - 1]
  • transceiverBit1ErrorCount – Type: long, Range: [0 to 232 - 1]
  • transceiverBit0ErrorCount – Type: long, Range: [0 to 232 - 1]
  • transceiverCRCErrorCount – Type: long, Range: [0 to 232 - 1]
  • writeBufferTimeoutErrorCount – Type: long, Range: [0 to 232 - 1]
  • readBufferOverflowErrorCount – Type: long, Range: [0 to 232 - 1]
  • readBufferOverflowErrorOccurred – Type: boolean[], Length: variable
  • readBacklogOverflowErrorCount – 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 stuffing, form, acknowledgement, bit and checksum errors during CAN bus write and read operations. For each of this error kinds there is also an individual counter.

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 buffer timeout, read buffer and backlog overflow counts represents the number of these errors:

  • A write buffer timeout occurs if a frame could not be transmitted before the configured write buffer timeout expired (see setQueueConfiguration()).
  • A read buffer overflow occurs if a read buffer of the CAN transceiver still contains the last received frame when the next frame arrives. In this case the last received frame is lost. This happens if the CAN transceiver receives more frames than the Bricklet can handle. Using the read filter (see setReadFilterConfiguration()) 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 backlog overflow occurs if the read backlog of the Bricklet is already full when the next frame should be read from a read buffer of the CAN transceiver. In this case the frame in the read buffer is lost. This happens if the CAN transceiver receives more frames to be added to the read backlog than are removed from the read backlog using the readFrame() function. Using the FrameReadCallback callback ensures that the read backlog can not overflow.

The read buffer overflow counter counts the overflows of all configured read buffers. Which read buffer exactly suffered from an overflow can be figured out from the read buffer overflow occurrence list (read_buffer_overflow_error_occurred). Reading the error log clears the occurence list.

The following constants are available for this function:

For transceiverState:

  • BrickletCANV2.TRANSCEIVER_STATE_ACTIVE = 0
  • BrickletCANV2.TRANSCEIVER_STATE_PASSIVE = 1
  • BrickletCANV2.TRANSCEIVER_STATE_DISABLED = 2
void BrickletCANV2.setCommunicationLEDConfig(int config)
Parameters:
  • config – Type: int, Range: See constants, Default: 3

Sets the communication LED configuration. By default the LED shows CAN-Bus traffic, it flickers once for every 40 transmitted or received frames.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is off.

The following constants are available for this function:

For config:

  • BrickletCANV2.COMMUNICATION_LED_CONFIG_OFF = 0
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_ON = 1
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION = 3
int BrickletCANV2.getCommunicationLEDConfig()
Returns:
  • config – Type: int, Range: See constants, Default: 3

Returns the configuration as set by setCommunicationLEDConfig()

The following constants are available for this function:

For config:

  • BrickletCANV2.COMMUNICATION_LED_CONFIG_OFF = 0
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_ON = 1
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION = 3
void BrickletCANV2.setErrorLEDConfig(int config)
Parameters:
  • config – Type: int, Range: See constants, Default: 3

Sets the error LED configuration.

By default (show-transceiver-state) the error LED turns on if the CAN transceiver is passive or disabled state (see getErrorLog()). If the CAN transceiver is in active state the LED turns off.

If the LED is configured as show-error then the error LED turns on if any error occurs. If you call this function with the show-error option again, the LED will turn off until the next error occurs.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is off.

The following constants are available for this function:

For config:

  • BrickletCANV2.ERROR_LED_CONFIG_OFF = 0
  • BrickletCANV2.ERROR_LED_CONFIG_ON = 1
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_TRANSCEIVER_STATE = 3
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_ERROR = 4
int BrickletCANV2.getErrorLEDConfig()
Returns:
  • config – Type: int, Range: See constants, Default: 3

Returns the configuration as set by setErrorLEDConfig().

The following constants are available for this function:

For config:

  • BrickletCANV2.ERROR_LED_CONFIG_OFF = 0
  • BrickletCANV2.ERROR_LED_CONFIG_ON = 1
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_TRANSCEIVER_STATE = 3
  • BrickletCANV2.ERROR_LED_CONFIG_SHOW_ERROR = 4
BrickletCANV2.SPITFPErrorCount BrickletCANV2.getSPITFPErrorCount()
Return Object:
  • errorCountAckChecksum – Type: long, Range: [0 to 232 - 1]
  • errorCountMessageChecksum – Type: long, Range: [0 to 232 - 1]
  • errorCountFrame – Type: long, Range: [0 to 232 - 1]
  • errorCountOverflow – Type: long, Range: [0 to 232 - 1]

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,
  • message checksum errors,
  • framing errors and
  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.

void BrickletCANV2.setStatusLEDConfig(int config)
Parameters:
  • config – Type: int, Range: See constants, Default: 3

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.

The following constants are available for this function:

For config:

  • BrickletCANV2.STATUS_LED_CONFIG_OFF = 0
  • BrickletCANV2.STATUS_LED_CONFIG_ON = 1
  • BrickletCANV2.STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.STATUS_LED_CONFIG_SHOW_STATUS = 3
int BrickletCANV2.getStatusLEDConfig()
Returns:
  • config – Type: int, Range: See constants, Default: 3

Returns the configuration as set by setStatusLEDConfig()

The following constants are available for this function:

For config:

  • BrickletCANV2.STATUS_LED_CONFIG_OFF = 0
  • BrickletCANV2.STATUS_LED_CONFIG_ON = 1
  • BrickletCANV2.STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2
  • BrickletCANV2.STATUS_LED_CONFIG_SHOW_STATUS = 3
int BrickletCANV2.getChipTemperature()
Returns:
  • temperature – Type: int, Unit: 1 °C, Range: [-215 to 215 - 1]

Returns the temperature as measured inside the microcontroller. The value returned is not the ambient temperature!

The temperature is only proportional to the real temperature and it has bad accuracy. Practically it is only useful as an indicator for temperature changes.

void BrickletCANV2.reset()

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!

BrickletCANV2.Identity BrickletCANV2.getIdentity()
Return Object:
  • 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
    • 1: major – Type: short, Range: [0 to 255]
    • 2: minor – Type: short, Range: [0 to 255]
    • 3: revision – Type: short, Range: [0 to 255]
  • firmwareVersion – Type: short[], Length: 3
    • 1: major – Type: short, Range: [0 to 255]
    • 2: minor – Type: short, Range: [0 to 255]
    • 3: 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. There is also a constant for the device identifier of this Bricklet.

Callback Configuration Functions

void BrickletCANV2.setFrameReadCallbackConfiguration(boolean enabled)
Parameters:
  • enabled – Type: boolean, Default: false

Enables and disables the FrameReadCallback callback.

By default the callback is disabled. Enabling this callback will disable the FrameReadableCallback callback.

boolean BrickletCANV2.getFrameReadCallbackConfiguration()
Returns:
  • enabled – Type: boolean, Default: false

Returns true if the FrameReadCallback callback is enabled, false otherwise.

void BrickletCANV2.setFrameReadableCallbackConfiguration(boolean enabled)
Parameters:
  • enabled – Type: boolean, Default: false

Enables and disables the FrameReadableCallback callback.

By default the callback is disabled. Enabling this callback will disable the FrameReadCallback callback.

New in version 2.0.3 (Plugin).

boolean BrickletCANV2.getFrameReadableCallbackConfiguration()
Returns:
  • enabled – Type: boolean, Default: false

Returns true if the FrameReadableCallback callback is enabled, false otherwise.

New in version 2.0.3 (Plugin).

void BrickletCANV2.setErrorOccurredCallbackConfiguration(boolean enabled)
Parameters:
  • enabled – Type: boolean, Default: false

Enables and disables the ErrorOccurredCallback callback.

By default the callback is disabled.

New in version 2.0.3 (Plugin).

boolean BrickletCANV2.getErrorOccurredCallbackConfiguration()
Returns:
  • enabled – Type: boolean, Default: false

Returns true if the ErrorOccurredCallback callback is enabled, false otherwise.

New in version 2.0.3 (Plugin).

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with "set" function of MATLAB. The parameters consist of the IP Connection object, the callback name and the callback function. For example, it looks like this in MATLAB:

function my_callback(e)
    fprintf('Parameter: %s\n', e.param);
end

set(device, 'ExampleCallback', @(h, e) my_callback(e));

Due to a difference in the Octave Java support the "set" function cannot be used in Octave. The registration is done with "add*Callback" functions of the device object. It looks like this in Octave:

function my_callback(e)
    fprintf("Parameter: %s\n", e.param);
end

device.addExampleCallback(@my_callback);

It is possible to add several callbacks and to remove them with the corresponding "remove*Callback" function.

The parameters of the callback are passed to the callback function as fields of the structure e, which is derived from the java.util.EventObject class. The available callback names with corresponding structure fields 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.

callback BrickletCANV2.FrameReadCallback
Event Object:
  • frameType – Type: int, Range: See constants
  • identifier – Type: long, Range: [0 to 230 - 1]
  • data – Type: int[], Length: variable, Range: [0 to 255]

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

For details on the data return value see readFrame().

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

To enable this callback, use setFrameReadCallbackConfiguration().

Note

If reconstructing the value fails, the callback is triggered with null for data.

The following constants are available for this function:

For frameType:

  • BrickletCANV2.FRAME_TYPE_STANDARD_DATA = 0
  • BrickletCANV2.FRAME_TYPE_STANDARD_REMOTE = 1
  • BrickletCANV2.FRAME_TYPE_EXTENDED_DATA = 2
  • BrickletCANV2.FRAME_TYPE_EXTENDED_REMOTE = 3

In MATLAB the set() function can be used to register a callback function to this callback.

In Octave a callback function can be added to this callback using the addFrameReadCallback() function. An added callback function can be removed with the removeFrameReadCallback() function.

callback BrickletCANV2.FrameReadableCallback
Event Object:
  • empty object

This callback is triggered if a data or remote frame was received by the CAN transceiver. The received frame can be read with readFrame(). If additional frames are received, but readFrame() 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 setReadFilterConfiguration()).

To enable this callback, use setFrameReadableCallbackConfiguration().

New in version 2.0.3 (Plugin).

In MATLAB the set() function can be used to register a callback function to this callback.

In Octave a callback function can be added to this callback using the addFrameReadableCallback() function. An added callback function can be removed with the removeFrameReadableCallback() function.

callback BrickletCANV2.ErrorOccurredCallback
Event Object:
  • empty object

This callback is triggered if any error occurred while writing, reading or transmitting CAN frames.

The callback is only triggered once until getErrorLog() is called. That function will return details abount the error(s) occurred.

To enable this callback, use setErrorOccurredCallbackConfiguration().

New in version 2.0.3 (Plugin).

In MATLAB the set() function can be used to register a callback function to this callback.

In Octave a callback function can be added to this callback using the addErrorOccurredCallback() function. An added callback function can be removed with the removeErrorOccurredCallback() function.

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

short[] BrickletCANV2.getAPIVersion()
Return Object:
  • apiVersion – Type: short[], Length: 3
    • 1: major – Type: short, Range: [0 to 255]
    • 2: minor – Type: short, Range: [0 to 255]
    • 3: revision – Type: short, Range: [0 to 255]

Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

boolean BrickletCANV2.getResponseExpected(byte functionId)
Parameters:
  • functionId – Type: byte, Range: See constants
Returns:
  • responseExpected – Type: boolean

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 setResponseExpected(). 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 sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • BrickletCANV2.FUNCTION_SET_FRAME_READ_CALLBACK_CONFIGURATION = 3
  • BrickletCANV2.FUNCTION_SET_TRANSCEIVER_CONFIGURATION = 5
  • BrickletCANV2.FUNCTION_SET_QUEUE_CONFIGURATION = 7
  • BrickletCANV2.FUNCTION_SET_READ_FILTER_CONFIGURATION = 9
  • BrickletCANV2.FUNCTION_SET_COMMUNICATION_LED_CONFIG = 12
  • BrickletCANV2.FUNCTION_SET_ERROR_LED_CONFIG = 14
  • BrickletCANV2.FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION = 17
  • BrickletCANV2.FUNCTION_SET_ERROR_OCCURRED_CALLBACK_CONFIGURATION = 20
  • BrickletCANV2.FUNCTION_SET_WRITE_FIRMWARE_POINTER = 237
  • BrickletCANV2.FUNCTION_SET_STATUS_LED_CONFIG = 239
  • BrickletCANV2.FUNCTION_RESET = 243
  • BrickletCANV2.FUNCTION_WRITE_UID = 248
void BrickletCANV2.setResponseExpected(byte functionId, boolean responseExpected)
Parameters:
  • functionId – Type: byte, Range: See constants
  • responseExpected – Type: boolean

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 sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For functionId:

  • BrickletCANV2.FUNCTION_SET_FRAME_READ_CALLBACK_CONFIGURATION = 3
  • BrickletCANV2.FUNCTION_SET_TRANSCEIVER_CONFIGURATION = 5
  • BrickletCANV2.FUNCTION_SET_QUEUE_CONFIGURATION = 7
  • BrickletCANV2.FUNCTION_SET_READ_FILTER_CONFIGURATION = 9
  • BrickletCANV2.FUNCTION_SET_COMMUNICATION_LED_CONFIG = 12
  • BrickletCANV2.FUNCTION_SET_ERROR_LED_CONFIG = 14
  • BrickletCANV2.FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION = 17
  • BrickletCANV2.FUNCTION_SET_ERROR_OCCURRED_CALLBACK_CONFIGURATION = 20
  • BrickletCANV2.FUNCTION_SET_WRITE_FIRMWARE_POINTER = 237
  • BrickletCANV2.FUNCTION_SET_STATUS_LED_CONFIG = 239
  • BrickletCANV2.FUNCTION_RESET = 243
  • BrickletCANV2.FUNCTION_WRITE_UID = 248
void BrickletCANV2.setResponseExpectedAll(boolean responseExpected)
Parameters:
  • responseExpected – Type: boolean

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

Internal Functions

Internal functions are used for maintenance tasks such as flashing a new firmware of changing the UID of a Bricklet. These task should be performed using Brick Viewer instead of using the internal functions directly.

int BrickletCANV2.setBootloaderMode(int mode)
Parameters:
  • mode – Type: int, Range: See constants
Returns:
  • status – Type: int, Range: See constants

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

The following constants are available for this function:

For mode:

  • BrickletCANV2.BOOTLOADER_MODE_BOOTLOADER = 0
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE = 1
  • BrickletCANV2.BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT = 2
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT = 3
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT = 4

For status:

  • BrickletCANV2.BOOTLOADER_STATUS_OK = 0
  • BrickletCANV2.BOOTLOADER_STATUS_INVALID_MODE = 1
  • BrickletCANV2.BOOTLOADER_STATUS_NO_CHANGE = 2
  • BrickletCANV2.BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT = 3
  • BrickletCANV2.BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT = 4
  • BrickletCANV2.BOOTLOADER_STATUS_CRC_MISMATCH = 5
int BrickletCANV2.getBootloaderMode()
Returns:
  • mode – Type: int, Range: See constants

Returns the current bootloader mode, see setBootloaderMode().

The following constants are available for this function:

For mode:

  • BrickletCANV2.BOOTLOADER_MODE_BOOTLOADER = 0
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE = 1
  • BrickletCANV2.BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT = 2
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT = 3
  • BrickletCANV2.BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT = 4
void BrickletCANV2.setWriteFirmwarePointer(long pointer)
Parameters:
  • pointer – Type: long, Unit: 1 B, Range: [0 to 232 - 1]

Sets the firmware pointer for writeFirmware(). The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

int BrickletCANV2.writeFirmware(int[] data)
Parameters:
  • data – Type: int[], Length: 64, Range: [0 to 255]
Returns:
  • status – Type: int, Range: [0 to 255]

Writes 64 Bytes of firmware at the position as written by setWriteFirmwarePointer() before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

void BrickletCANV2.writeUID(long uid)
Parameters:
  • uid – Type: long, Range: [0 to 232 - 1]

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.

long BrickletCANV2.readUID()
Returns:
  • uid – Type: long, Range: [0 to 232 - 1]

Returns the current UID as an integer. Encode as Base58 to get the usual string version.

Constants

int BrickletCANV2.DEVICE_IDENTIFIER

This constant is used to identify a CAN Bricklet 2.0.

The getIdentity() function and the IPConnection.EnumerateCallback callback of the IP Connection have a deviceIdentifier parameter to specify the Brick's or Bricklet's type.

String BrickletCANV2.DEVICE_DISPLAY_NAME

This constant represents the human readable name of a CAN Bricklet 2.0.