Class: Tinkerforge::BrickletRS232V2
- Defined in:
- lib/tinkerforge/bricklet_rs232_v2.rb
Overview
Communicates with RS232 devices
Constant Summary collapse
- DEVICE_IDENTIFIER =
:nodoc:
2108
- DEVICE_DISPLAY_NAME =
:nodoc:
'RS232 Bricklet 2.0'
- CALLBACK_READ_LOW_LEVEL =
See CALLBACK_READ
12
- CALLBACK_ERROR_COUNT =
This callback is called if a new error occurs. It returns the current overrun and parity error count.
13
- CALLBACK_FRAME_READABLE =
This callback is called if at least one frame of data is readable. The frame size is configured with BrickletRS232V2#set_frame_readable_callback_configuration. The frame count parameter is the number of frames that can be read. This callback is triggered only once until BrickletRS232V2#read is called. This means, that if you have configured a frame size of X bytes, you can read exactly X bytes using the BrickletRS232V2#read function, every time the callback triggers without checking the frame count parameter.
- .. versionadded
-
2.0.3$nbsp;(Plugin)
16
- CALLBACK_READ =
This callback is called if new data is available.
To enable this callback, use BrickletRS232V2#enable_read_callback.
- .. note
-
If reconstructing the value fails, the callback is triggered with nil for message.
-12
- FUNCTION_WRITE_LOW_LEVEL =
:nodoc:
1
- FUNCTION_READ_LOW_LEVEL =
:nodoc:
2
- FUNCTION_ENABLE_READ_CALLBACK =
:nodoc:
3
- FUNCTION_DISABLE_READ_CALLBACK =
:nodoc:
4
- FUNCTION_IS_READ_CALLBACK_ENABLED =
:nodoc:
5
- FUNCTION_SET_CONFIGURATION =
:nodoc:
6
- FUNCTION_GET_CONFIGURATION =
:nodoc:
7
- FUNCTION_SET_BUFFER_CONFIG =
:nodoc:
8
- FUNCTION_GET_BUFFER_CONFIG =
:nodoc:
9
- FUNCTION_GET_BUFFER_STATUS =
:nodoc:
10
- FUNCTION_GET_ERROR_COUNT =
:nodoc:
11
- FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION =
:nodoc:
14
- FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION =
:nodoc:
15
- FUNCTION_GET_SPITFP_ERROR_COUNT =
:nodoc:
234
- FUNCTION_SET_BOOTLOADER_MODE =
:nodoc:
235
- FUNCTION_GET_BOOTLOADER_MODE =
:nodoc:
236
- FUNCTION_SET_WRITE_FIRMWARE_POINTER =
:nodoc:
237
- FUNCTION_WRITE_FIRMWARE =
:nodoc:
238
- FUNCTION_SET_STATUS_LED_CONFIG =
:nodoc:
239
- FUNCTION_GET_STATUS_LED_CONFIG =
:nodoc:
240
- FUNCTION_GET_CHIP_TEMPERATURE =
:nodoc:
242
- FUNCTION_RESET =
:nodoc:
243
- FUNCTION_WRITE_UID =
:nodoc:
248
- FUNCTION_READ_UID =
:nodoc:
249
- FUNCTION_GET_IDENTITY =
:nodoc:
255
- PARITY_NONE =
:nodoc:
0
- PARITY_ODD =
:nodoc:
1
- PARITY_EVEN =
:nodoc:
2
- STOPBITS_1 =
:nodoc:
1
- STOPBITS_2 =
:nodoc:
2
- WORDLENGTH_5 =
:nodoc:
5
- WORDLENGTH_6 =
:nodoc:
6
- WORDLENGTH_7 =
:nodoc:
7
- WORDLENGTH_8 =
:nodoc:
8
- FLOWCONTROL_OFF =
:nodoc:
0
- FLOWCONTROL_SOFTWARE =
:nodoc:
1
- FLOWCONTROL_HARDWARE =
:nodoc:
2
- BOOTLOADER_MODE_BOOTLOADER =
:nodoc:
0
- BOOTLOADER_MODE_FIRMWARE =
:nodoc:
1
- BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT =
:nodoc:
2
- BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT =
:nodoc:
3
- BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT =
:nodoc:
4
- BOOTLOADER_STATUS_OK =
:nodoc:
0
- BOOTLOADER_STATUS_INVALID_MODE =
:nodoc:
1
- BOOTLOADER_STATUS_NO_CHANGE =
:nodoc:
2
- BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT =
:nodoc:
3
- BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT =
:nodoc:
4
- BOOTLOADER_STATUS_CRC_MISMATCH =
:nodoc:
5
- STATUS_LED_CONFIG_OFF =
:nodoc:
0
- STATUS_LED_CONFIG_ON =
:nodoc:
1
- STATUS_LED_CONFIG_SHOW_HEARTBEAT =
:nodoc:
2
- STATUS_LED_CONFIG_SHOW_STATUS =
:nodoc:
3
Constants inherited from Device
Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, 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, #high_level_callbacks, #registered_callbacks, #replaced, #uid
Instance Method Summary collapse
-
#disable_read_callback ⇒ Object
Disables the CALLBACK_READ callback.
-
#enable_read_callback ⇒ Object
Enables the CALLBACK_READ callback.
-
#get_bootloader_mode ⇒ Object
Returns the current bootloader mode, see BrickletRS232V2#set_bootloader_mode.
-
#get_buffer_config ⇒ Object
Returns the buffer configuration as set by BrickletRS232V2#set_buffer_config.
-
#get_buffer_status ⇒ Object
Returns the currently used bytes for the send and received buffer.
-
#get_chip_temperature ⇒ Object
Returns the temperature as measured inside the microcontroller.
-
#get_configuration ⇒ Object
Returns the configuration as set by BrickletRS232V2#set_configuration.
-
#get_error_count ⇒ Object
Returns the current number of overrun and parity errors.
-
#get_frame_readable_callback_configuration ⇒ Object
Returns the callback configuration as set by BrickletRS232V2#set_frame_readable_callback_configuration.
-
#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_spitfp_error_count ⇒ Object
Returns the error count for the communication between Brick and Bricklet.
-
#get_status_led_config ⇒ Object
Returns the configuration as set by BrickletRS232V2#set_status_led_config.
-
#initialize(uid, ipcon) ⇒ BrickletRS232V2
constructor
Creates an object with the unique device ID
uid
and adds it to the IP Connectionipcon
. -
#is_read_callback_enabled ⇒ Object
Returns true if the CALLBACK_READ callback is enabled, false otherwise.
-
#read(length) ⇒ Object
Returns up to length characters from receive buffer.
-
#read_low_level(length) ⇒ Object
Returns up to length characters from receive buffer.
-
#read_uid ⇒ Object
Returns the current UID as an integer.
-
#register_callback(id, &block) ⇒ Object
Registers a callback with ID
id
to the blockblock
. -
#reset ⇒ Object
Calling this function will reset the Bricklet.
-
#set_bootloader_mode(mode) ⇒ Object
Sets the bootloader mode and returns the status after the requested mode change was instigated.
-
#set_buffer_config(send_buffer_size, receive_buffer_size) ⇒ Object
Sets the send and receive buffer size in byte.
-
#set_configuration(baudrate, parity, stopbits, wordlength, flowcontrol) ⇒ Object
Sets the configuration for the RS232 communication.
-
#set_frame_readable_callback_configuration(frame_size) ⇒ Object
Configures the CALLBACK_FRAME_READABLE callback.
-
#set_status_led_config(config) ⇒ Object
Sets the status LED configuration.
-
#set_write_firmware_pointer(pointer) ⇒ Object
Sets the firmware pointer for BrickletRS232V2#write_firmware.
-
#write(message) ⇒ Object
Writes characters to the RS232 interface.
-
#write_firmware(data) ⇒ Object
Writes 64 Bytes of firmware at the position as written by BrickletRS232V2#set_write_firmware_pointer before.
-
#write_low_level(message_length, message_chunk_offset, message_chunk_data) ⇒ Object
Writes characters to the RS232 interface.
-
#write_uid(uid) ⇒ Object
Writes a new UID into flash.
Methods inherited from Device
#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all
Constructor Details
#initialize(uid, ipcon) ⇒ BrickletRS232V2
Creates an object with the unique device ID uid
and adds it to the IP Connection ipcon
.
Instance Method Details
#disable_read_callback ⇒ Object
Disables the CALLBACK_READ callback.
By default the callback is disabled.
175 176 177 178 179 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 175 def disable_read_callback check_validity send_request FUNCTION_DISABLE_READ_CALLBACK, [], '', 8, '' end |
#enable_read_callback ⇒ Object
Enables the CALLBACK_READ callback. This will disable the CALLBACK_FRAME_READABLE callback.
By default the callback is disabled.
166 167 168 169 170 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 166 def enable_read_callback check_validity send_request FUNCTION_ENABLE_READ_CALLBACK, [], '', 8, '' end |
#get_bootloader_mode ⇒ Object
Returns the current bootloader mode, see BrickletRS232V2#set_bootloader_mode.
295 296 297 298 299 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 295 def get_bootloader_mode check_validity send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C' end |
#get_buffer_config ⇒ Object
Returns the buffer configuration as set by BrickletRS232V2#set_buffer_config.
219 220 221 222 223 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 219 def get_buffer_config check_validity send_request FUNCTION_GET_BUFFER_CONFIG, [], '', 12, 'S S' end |
#get_buffer_status ⇒ Object
Returns the currently used bytes for the send and received buffer.
See BrickletRS232V2#set_buffer_config for buffer size configuration.
228 229 230 231 232 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 228 def get_buffer_status check_validity send_request FUNCTION_GET_BUFFER_STATUS, [], '', 12, 'S S' end |
#get_chip_temperature ⇒ Object
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.
353 354 355 356 357 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 353 def get_chip_temperature check_validity send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's' end |
#get_configuration ⇒ Object
Returns the configuration as set by BrickletRS232V2#set_configuration.
197 198 199 200 201 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 197 def get_configuration check_validity send_request FUNCTION_GET_CONFIGURATION, [], '', 16, 'L C C C C' end |
#get_error_count ⇒ Object
Returns the current number of overrun and parity errors.
235 236 237 238 239 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 235 def get_error_count check_validity send_request FUNCTION_GET_ERROR_COUNT, [], '', 16, 'L L' end |
#get_frame_readable_callback_configuration ⇒ Object
Returns the callback configuration as set by BrickletRS232V2#set_frame_readable_callback_configuration.
- .. versionadded
-
2.0.3$nbsp;(Plugin)
256 257 258 259 260 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 256 def get_frame_readable_callback_configuration check_validity send_request FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION, [], '', 10, 'S' 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’, ‘d’, ‘e’, ‘f’, ‘g’ or ‘h’ (Bricklet Port). A Bricklet connected to an :ref:‘Isolator Bricklet <isolator_bricklet>` is always at position ’z’.
The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|
400 401 402 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 400 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end |
#get_spitfp_error_count ⇒ Object
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.
273 274 275 276 277 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 273 def get_spitfp_error_count check_validity send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L' end |
#get_status_led_config ⇒ Object
Returns the configuration as set by BrickletRS232V2#set_status_led_config
341 342 343 344 345 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 341 def get_status_led_config check_validity send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C' end |
#is_read_callback_enabled ⇒ Object
Returns true if the CALLBACK_READ callback is enabled, false otherwise.
183 184 185 186 187 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 183 def is_read_callback_enabled check_validity send_request FUNCTION_IS_READ_CALLBACK_ENABLED, [], '', 9, '?' end |
#read(length) ⇒ Object
Returns up to length characters from receive buffer.
Instead of polling with this function, you can also use callbacks. But note that this function will return available data only when the read callback is disabled. See BrickletRS232V2#enable_read_callback and CALLBACK_READ callback.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 455 def read(length) = nil # assigned in block = nil # assigned in block @stream_mutex.synchronize { ret = read_low_level length = ret[0] = ret[1] = != 0 = ret[2] while not and .length < ret = read_low_level length = ret[0] = ret[1] = != .length += ret[2] end if # discard remaining stream to bring it back in-sync while + 60 < ret = read_low_level length = ret[0] = ret[1] end raise StreamOutOfSyncException, 'Message stream is out-of-sync' end } [0, ] end |
#read_low_level(length) ⇒ Object
Returns up to length characters from receive buffer.
Instead of polling with this function, you can also use callbacks. But note that this function will return available data only when the read callback is disabled. See BrickletRS232V2#enable_read_callback and CALLBACK_READ callback.
157 158 159 160 161 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 157 def read_low_level(length) check_validity send_request FUNCTION_READ_LOW_LEVEL, [length], 'S', 72, 'S S k60' end |
#read_uid ⇒ Object
Returns the current UID as an integer. Encode as Base58 to get the usual string version.
384 385 386 387 388 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 384 def read_uid check_validity send_request FUNCTION_READ_UID, [], '', 12, 'L' end |
#register_callback(id, &block) ⇒ Object
Registers a callback with ID id
to the block block
.
489 490 491 492 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 489 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end |
#reset ⇒ Object
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!
365 366 367 368 369 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 365 def reset check_validity send_request FUNCTION_RESET, [], '', 8, '' end |
#set_bootloader_mode(mode) ⇒ Object
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.
288 289 290 291 292 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 288 def set_bootloader_mode(mode) check_validity send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C' end |
#set_buffer_config(send_buffer_size, receive_buffer_size) ⇒ Object
Sets the send and receive buffer size in byte. In total the buffers have to be 10240 byte (10KiB) in size, the minimum buffer size is 1024 byte (1KiB) for each.
The current buffer content is lost if this function is called.
The send buffer holds data that is given by BrickletRS232V2#write and can not be written yet. The receive buffer holds data that is received through RS232 but could not yet be send to the user, either by BrickletRS232V2#read or through CALLBACK_READ callback.
212 213 214 215 216 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 212 def set_buffer_config(send_buffer_size, receive_buffer_size) check_validity send_request FUNCTION_SET_BUFFER_CONFIG, [send_buffer_size, receive_buffer_size], 'S S', 8, '' end |
#set_configuration(baudrate, parity, stopbits, wordlength, flowcontrol) ⇒ Object
Sets the configuration for the RS232 communication.
190 191 192 193 194 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 190 def set_configuration(baudrate, parity, stopbits, wordlength, flowcontrol) check_validity send_request FUNCTION_SET_CONFIGURATION, [baudrate, parity, stopbits, wordlength, flowcontrol], 'L C C C C', 8, '' end |
#set_frame_readable_callback_configuration(frame_size) ⇒ Object
Configures the CALLBACK_FRAME_READABLE callback. The frame size is the number of bytes, that have to be readable to trigger the callback. A frame size of 0 disables the callback. A frame size greater than 0 enables the callback and disables the CALLBACK_READ callback.
By default the callback is disabled.
- .. versionadded
-
2.0.3$nbsp;(Plugin)
247 248 249 250 251 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 247 def set_frame_readable_callback_configuration(frame_size) check_validity send_request FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION, [frame_size], 'S', 8, '' end |
#set_status_led_config(config) ⇒ Object
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.
334 335 336 337 338 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 334 def set_status_led_config(config) check_validity send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, '' end |
#set_write_firmware_pointer(pointer) ⇒ Object
Sets the firmware pointer for BrickletRS232V2#write_firmware. 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.
307 308 309 310 311 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 307 def set_write_firmware_pointer(pointer) check_validity send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, '' end |
#write(message) ⇒ Object
Writes characters to the RS232 interface. The characters can be binary data, ASCII or similar is not necessary.
The return value is the number of characters that were written.
See BrickletRS232V2#set_configuration for configuration possibilities regarding baud rate, parity and so on.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 411 def write() if .length > 65535 raise ArgumentError, 'Message can be at most 65535 items long' end = .length = 0 if == 0 = ['\0'] * 60 ret = write_low_level , , = ret else = 0 # assigned in block @stream_mutex.synchronize { while < = [, 60] if .length < 60 += ['\0'] * (60 - .length) end ret = write_low_level , , += ret if ret < 60 break # either last chunk or short write end += 60 end } end end |
#write_firmware(data) ⇒ Object
Writes 64 Bytes of firmware at the position as written by BrickletRS232V2#set_write_firmware_pointer 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.
321 322 323 324 325 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 321 def write_firmware(data) check_validity send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C' end |
#write_low_level(message_length, message_chunk_offset, message_chunk_data) ⇒ Object
Writes characters to the RS232 interface. The characters can be binary data, ASCII or similar is not necessary.
The return value is the number of characters that were written.
See BrickletRS232V2#set_configuration for configuration possibilities regarding baud rate, parity and so on.
145 146 147 148 149 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 145 def write_low_level(, , ) check_validity send_request FUNCTION_WRITE_LOW_LEVEL, [, , ], 'S S k60', 9, 'C' end |
#write_uid(uid) ⇒ Object
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.
376 377 378 379 380 |
# File 'lib/tinkerforge/bricklet_rs232_v2.rb', line 376 def write_uid(uid) check_validity send_request FUNCTION_WRITE_UID, [uid], 'L', 8, '' end |