Class: Tinkerforge::BrickletCAN
- Defined in:
- lib/tinkerforge/bricklet_can.rb
Overview
Communicates with CAN bus devices
Constant Summary collapse
- DEVICE_IDENTIFIER =
:nodoc:
270- DEVICE_DISPLAY_NAME =
:nodoc:
'CAN Bricklet'- CALLBACK_FRAME_READ =
This callback is triggered if a data or remote frame was received by the CAN transceiver.
For remote frames the
datareturn 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 BrickletCAN#set_read_filter).
To enable this callback, use BrickletCAN#enable_frame_read_callback.
11- FUNCTION_WRITE_FRAME =
:nodoc:
1- FUNCTION_READ_FRAME =
:nodoc:
2- FUNCTION_ENABLE_FRAME_READ_CALLBACK =
:nodoc:
3- FUNCTION_DISABLE_FRAME_READ_CALLBACK =
:nodoc:
4- FUNCTION_IS_FRAME_READ_CALLBACK_ENABLED =
:nodoc:
5- FUNCTION_SET_CONFIGURATION =
:nodoc:
6- FUNCTION_GET_CONFIGURATION =
:nodoc:
7- FUNCTION_SET_READ_FILTER =
:nodoc:
8- FUNCTION_GET_READ_FILTER =
:nodoc:
9- FUNCTION_GET_ERROR_LOG =
:nodoc:
10- FUNCTION_GET_IDENTITY =
:nodoc:
255- FRAME_TYPE_STANDARD_DATA =
:nodoc:
0- FRAME_TYPE_STANDARD_REMOTE =
:nodoc:
1- FRAME_TYPE_EXTENDED_DATA =
:nodoc:
2- FRAME_TYPE_EXTENDED_REMOTE =
:nodoc:
3- BAUD_RATE_10KBPS =
:nodoc:
0- BAUD_RATE_20KBPS =
:nodoc:
1- BAUD_RATE_50KBPS =
:nodoc:
2- BAUD_RATE_125KBPS =
:nodoc:
3- BAUD_RATE_250KBPS =
:nodoc:
4- BAUD_RATE_500KBPS =
:nodoc:
5- BAUD_RATE_800KBPS =
:nodoc:
6- BAUD_RATE_1000KBPS =
:nodoc:
7- TRANSCEIVER_MODE_NORMAL =
:nodoc:
0- TRANSCEIVER_MODE_LOOPBACK =
:nodoc:
1- TRANSCEIVER_MODE_READ_ONLY =
:nodoc:
2- FILTER_MODE_DISABLED =
:nodoc:
0- FILTER_MODE_ACCEPT_ALL =
:nodoc:
1- FILTER_MODE_MATCH_STANDARD =
:nodoc:
2- FILTER_MODE_MATCH_STANDARD_AND_DATA =
:nodoc:
3- FILTER_MODE_MATCH_EXTENDED =
:nodoc:
4
Constants inherited from Device
Device::RESPONSE_EXPECTED_ALWAYS_FALSE, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE
Instance Attribute Summary
Attributes inherited from Device
#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #registered_callbacks, #uid
Instance Method Summary collapse
-
#disable_frame_read_callback ⇒ Object
Disables the CALLBACK_FRAME_READ callback.
-
#enable_frame_read_callback ⇒ Object
Enables the CALLBACK_FRAME_READ callback.
-
#get_configuration ⇒ Object
Returns the configuration as set by BrickletCAN#set_configuration.
-
#get_error_log ⇒ Object
Returns information about different kinds of errors.
-
#get_identity ⇒ Object
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
-
#get_read_filter ⇒ Object
Returns the read filter as set by BrickletCAN#set_read_filter.
-
#initialize(uid, ipcon) ⇒ BrickletCAN
constructor
Creates an object with the unique device ID
uidand adds it to the IP Connectionipcon. -
#is_frame_read_callback_enabled ⇒ Object
Returns true if the CALLBACK_FRAME_READ callback is enabled, false otherwise.
-
#read_frame ⇒ Object
Tries to read the next data or remote frame from the read buffer and return it.
-
#register_callback(id, &block) ⇒ Object
Registers a callback with ID
idto the blockblock. -
#set_configuration(baud_rate, transceiver_mode, write_timeout) ⇒ Object
Sets the configuration for the CAN bus communication.
-
#set_read_filter(mode, mask, filter1, filter2) ⇒ Object
Set the read filter configuration.
-
#write_frame(frame_type, identifier, data, length) ⇒ Object
Writes a data or remote frame to the write buffer to be transmitted over the CAN transceiver.
Methods inherited from Device
#dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all
Constructor Details
#initialize(uid, ipcon) ⇒ BrickletCAN
Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tinkerforge/bricklet_can.rb', line 64 def initialize(uid, ipcon) super uid, ipcon @api_version = [2, 0, 0] @response_expected[FUNCTION_WRITE_FRAME] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_READ_FRAME] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_ENABLE_FRAME_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_DISABLE_FRAME_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_IS_FRAME_READ_CALLBACK_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_CONFIGURATION] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_READ_FILTER] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_READ_FILTER] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_ERROR_LOG] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[CALLBACK_FRAME_READ] = RESPONSE_EXPECTED_ALWAYS_FALSE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_FRAME_READ] = 'C L C8 C' end |
Instance Method Details
#disable_frame_read_callback ⇒ Object
Disables the CALLBACK_FRAME_READ callback.
By default the callback is disabled.
133 134 135 |
# File 'lib/tinkerforge/bricklet_can.rb', line 133 def disable_frame_read_callback send_request(FUNCTION_DISABLE_FRAME_READ_CALLBACK, [], '', 0, '') end |
#enable_frame_read_callback ⇒ Object
Enables the CALLBACK_FRAME_READ callback.
By default the callback is disabled.
126 127 128 |
# File 'lib/tinkerforge/bricklet_can.rb', line 126 def enable_frame_read_callback send_request(FUNCTION_ENABLE_FRAME_READ_CALLBACK, [], '', 0, '') end |
#get_configuration ⇒ Object
Returns the configuration as set by BrickletCAN#set_configuration.
173 174 175 |
# File 'lib/tinkerforge/bricklet_can.rb', line 173 def get_configuration send_request(FUNCTION_GET_CONFIGURATION, [], '', 6, 'C C l') end |
#get_error_log ⇒ Object
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 BrickletCAN#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 BrickletCAN#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 BrickletCAN#set_read_filter) can help to reduce the amount of received frames.
-
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 BrickletCAN#read_frame function. Using the CALLBACK_FRAME_READ callback ensures that the read buffer can not overflow.
267 268 269 |
# File 'lib/tinkerforge/bricklet_can.rb', line 267 def get_error_log send_request(FUNCTION_GET_ERROR_LOG, [], '', 15, 'C C ? L L L') end |
#get_identity ⇒ Object
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 :ref:‘here <device_identifier>`. |device_identifier_constant|
279 280 281 |
# File 'lib/tinkerforge/bricklet_can.rb', line 279 def get_identity send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S') end |
#get_read_filter ⇒ Object
Returns the read filter as set by BrickletCAN#set_read_filter.
232 233 234 |
# File 'lib/tinkerforge/bricklet_can.rb', line 232 def get_read_filter send_request(FUNCTION_GET_READ_FILTER, [], '', 13, 'C L L L') end |
#is_frame_read_callback_enabled ⇒ Object
Returns true if the CALLBACK_FRAME_READ callback is enabled, false otherwise.
138 139 140 |
# File 'lib/tinkerforge/bricklet_can.rb', line 138 def is_frame_read_callback_enabled send_request(FUNCTION_IS_FRAME_READ_CALLBACK_ENABLED, [], '', 1, '?') end |
#read_frame ⇒ Object
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.
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 BrickletCAN#set_read_filter).
Instead of polling with this function, you can also use callbacks. See the BrickletCAN#enable_frame_read_callback function and the CALLBACK_FRAME_READ callback.
119 120 121 |
# File 'lib/tinkerforge/bricklet_can.rb', line 119 def read_frame send_request(FUNCTION_READ_FRAME, [], '', 15, '? C L C8 C') end |
#register_callback(id, &block) ⇒ Object
Registers a callback with ID id to the block block.
284 285 286 287 |
# File 'lib/tinkerforge/bricklet_can.rb', line 284 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end |
#set_configuration(baud_rate, transceiver_mode, write_timeout) ⇒ Object
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 and 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.
168 169 170 |
# File 'lib/tinkerforge/bricklet_can.rb', line 168 def set_configuration(baud_rate, transceiver_mode, write_timeout) send_request(FUNCTION_SET_CONFIGURATION, [baud_rate, transceiver_mode, write_timeout], 'C C l', 0, '') end |
#set_read_filter(mode, mask, filter1, filter2) ⇒ Object
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 28 (29 bits) of mask and filters are used to match the 29-bit identifier 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.
227 228 229 |
# File 'lib/tinkerforge/bricklet_can.rb', line 227 def set_read_filter(mode, mask, filter1, filter2) send_request(FUNCTION_SET_READ_FILTER, [mode, mask, filter1, filter2], 'C L L L', 0, '') end |
#write_frame(frame_type, identifier, data, length) ⇒ Object
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 extended 29-bit (CAN 2.0B) identifiers. 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 BrickletCAN#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 BrickletCAN#get_error_log).
101 102 103 |
# File 'lib/tinkerforge/bricklet_can.rb', line 101 def write_frame(frame_type, identifier, data, length) send_request(FUNCTION_WRITE_FRAME, [frame_type, identifier, data, length], 'C L C8 C', 1, '?') end |