class Tinkerforge::BrickletRS232V2
Communicates with RS232 devices
Constants
- CALLBACK_ERROR_COUNT
This callback is called if a new error occurs. It returns the current overrun and parity error count.
- 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 untilBrickletRS232V2#read
is called. This means, that if you have configured a frame size of X bytes, you can read exactly X bytes using theBrickletRS232V2#read
function, every time the callback triggers without checking the frame count parameter.- .. versionadded
-
2.0.3$nbsp;(Plugin)
- 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.
- CALLBACK_READ_LOW_LEVEL
See
CALLBACK_READ
Public Class Methods
Creates an object with the unique device ID uid
and adds it to the IP Connection ipcon
.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 98 def initialize(uid, ipcon) super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME @api_version = [2, 0, 1] @response_expected[FUNCTION_WRITE_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_READ_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_ENABLE_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_DISABLE_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_IS_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_BUFFER_CONFIG] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_BUFFER_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_BUFFER_STATUS] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_TRUE @response_expected[FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_SPITFP_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_WRITE_FIRMWARE_POINTER] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_WRITE_FIRMWARE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_SET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_GET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_CHIP_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_RESET] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_WRITE_UID] = RESPONSE_EXPECTED_FALSE @response_expected[FUNCTION_READ_UID] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_READ_LOW_LEVEL] = [72, 'S S k60'] @callback_formats[CALLBACK_ERROR_COUNT] = [16, 'L L'] @callback_formats[CALLBACK_FRAME_READABLE] = [10, 'S'] @high_level_callbacks[CALLBACK_READ] = [['stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil] @ipcon.add_device self end
Public Instance Methods
Disables the CALLBACK_READ
callback.
By default the callback is disabled.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 174 def disable_read_callback check_validity send_request FUNCTION_DISABLE_READ_CALLBACK, [], '', 8, '' end
Enables the CALLBACK_READ
callback. This will disable the CALLBACK_FRAME_READABLE
callback.
By default the callback is disabled.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 165 def enable_read_callback check_validity send_request FUNCTION_ENABLE_READ_CALLBACK, [], '', 8, '' end
Returns the current bootloader mode, see BrickletRS232V2#set_bootloader_mode
.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 294 def get_bootloader_mode check_validity send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C' end
Returns the buffer configuration as set by BrickletRS232V2#set_buffer_config
.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 218 def get_buffer_config check_validity send_request FUNCTION_GET_BUFFER_CONFIG, [], '', 12, 'S S' end
Returns the currently used bytes for the send and received buffer.
See BrickletRS232V2#set_buffer_config
for buffer size configuration.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 227 def get_buffer_status check_validity send_request FUNCTION_GET_BUFFER_STATUS, [], '', 12, 'S S' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 352 def get_chip_temperature check_validity send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's' end
Returns the configuration as set by BrickletRS232V2#set_configuration
.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 196 def get_configuration check_validity send_request FUNCTION_GET_CONFIGURATION, [], '', 16, 'L C C C C' end
Returns the current number of overrun and parity errors.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 234 def get_error_count check_validity send_request FUNCTION_GET_ERROR_COUNT, [], '', 16, 'L L' end
Returns the callback configuration as set by BrickletRS232V2#set_frame_readable_callback_configuration
.
- .. versionadded
-
2.0.3$nbsp;(Plugin)
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 255 def get_frame_readable_callback_configuration check_validity send_request FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION, [], '', 10, 'S' end
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|
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 399 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 272 def get_spitfp_error_count check_validity send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L' end
Returns the configuration as set by BrickletRS232V2#set_status_led_config
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 340 def get_status_led_config check_validity send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C' end
Returns true if the CALLBACK_READ
callback is enabled, false otherwise.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 182 def is_read_callback_enabled check_validity send_request FUNCTION_IS_READ_CALLBACK_ENABLED, [], '', 9, '?' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 454 def read(length) message_length = nil # assigned in block message_data = nil # assigned in block @stream_mutex.synchronize { ret = read_low_level length message_length = ret[0] message_chunk_offset = ret[1] message_out_of_sync = message_chunk_offset != 0 message_data = ret[2] while not message_out_of_sync and message_data.length < message_length ret = read_low_level length message_length = ret[0] message_chunk_offset = ret[1] message_out_of_sync = message_chunk_offset != message_data.length message_data += ret[2] end if message_out_of_sync # discard remaining stream to bring it back in-sync while message_chunk_offset + 60 < message_length ret = read_low_level length message_length = ret[0] message_chunk_offset = ret[1] end raise StreamOutOfSyncException, 'Message stream is out-of-sync' end } message_data[0, message_length] end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 156 def read_low_level(length) check_validity send_request FUNCTION_READ_LOW_LEVEL, [length], 'S', 72, 'S S k60' end
Returns the current UID as an integer. Encode as Base58
to get the usual string version.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 383 def read_uid check_validity send_request FUNCTION_READ_UID, [], '', 12, 'L' end
Registers a callback with ID id
to the block block
.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 488 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end
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!
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 364 def reset check_validity send_request FUNCTION_RESET, [], '', 8, '' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 287 def set_bootloader_mode(mode) check_validity send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 211 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
Sets the configuration for the RS232 communication.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 189 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
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)
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 246 def set_frame_readable_callback_configuration(frame_size) check_validity send_request FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION, [frame_size], 'S', 8, '' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 333 def set_status_led_config(config) check_validity send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, '' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 306 def set_write_firmware_pointer(pointer) check_validity send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, '' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 410 def write(message) if message.length > 65535 raise ArgumentError, 'Message can be at most 65535 items long' end message_length = message.length message_chunk_offset = 0 if message_length == 0 message_chunk_data = ['\0'] * 60 ret = write_low_level message_length, message_chunk_offset, message_chunk_data message_written = ret else message_written = 0 # assigned in block @stream_mutex.synchronize { while message_chunk_offset < message_length message_chunk_data = message[message_chunk_offset, 60] if message_chunk_data.length < 60 message_chunk_data += ['\0'] * (60 - message_chunk_data.length) end ret = write_low_level message_length, message_chunk_offset, message_chunk_data message_written += ret if ret < 60 break # either last chunk or short write end message_chunk_offset += 60 end } end message_written end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 320 def write_firmware(data) check_validity send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 144 def write_low_level(message_length, message_chunk_offset, message_chunk_data) check_validity send_request FUNCTION_WRITE_LOW_LEVEL, [message_length, message_chunk_offset, message_chunk_data], 'S S k60', 9, 'C' end
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.
# File lib/tinkerforge/bricklet_rs232_v2.rb, line 375 def write_uid(uid) check_validity send_request FUNCTION_WRITE_UID, [uid], 'L', 8, '' end