class Tinkerforge::BrickletRS232
Communicates with RS232 devices
Constants
- CALLBACK_ERROR
This callback is called if an error occurs. Possible errors are overrun, parity or framing error.
- .. versionadded
-
2.0.1$nbsp;(Plugin)
- CALLBACK_FRAME_READABLE
This callback is called if at least one frame of data is readable. The frame size is configured with
BrickletRS232#set_frame_readable_callback_configuration
. The frame count parameter is the number of frames that can be read. This callback is triggered only once untilBrickletRS232#read
orBrickletRS232#read_frame
is called. This means, that if you have configured a frame size of X bytes, you can read exactly X bytes using theBrickletRS232#read_frame
function, every time the callback triggers without checking the frame count parameter.- .. versionadded
-
2.0.4$nbsp;(Plugin)
- CALLBACK_READ
This callback is called if new data is available. The message has a maximum size of 60 characters. The actual length of the message is given in addition.
To enable this callback, use
BrickletRS232#enable_read_callback
.
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.rb, line 90 def initialize(uid, ipcon) super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME @api_version = [2, 0, 3] @response_expected[FUNCTION_WRITE] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_READ] = 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_BREAK_CONDITION] = RESPONSE_EXPECTED_FALSE @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_READ_FRAME] = RESPONSE_EXPECTED_ALWAYS_TRUE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_READ] = [69, 'k60 C'] @callback_formats[CALLBACK_ERROR] = [9, 'C'] @callback_formats[CALLBACK_FRAME_READABLE] = [9, 'C'] @ipcon.add_device self end
Public Instance Methods
Disables the CALLBACK_READ
callback.
By default the callback is disabled.
# File lib/tinkerforge/bricklet_rs232.rb, line 153 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.rb, line 144 def enable_read_callback check_validity send_request FUNCTION_ENABLE_READ_CALLBACK, [], '', 8, '' end
Returns the configuration as set by BrickletRS232#set_configuration
.
# File lib/tinkerforge/bricklet_rs232.rb, line 177 def get_configuration check_validity send_request FUNCTION_GET_CONFIGURATION, [], '', 14, 'C C C C C C' end
Returns the callback configuration as set by BrickletRS232#set_frame_readable_callback_configuration
.
- .. versionadded
-
2.0.4$nbsp;(Plugin)
# File lib/tinkerforge/bricklet_rs232.rb, line 208 def get_frame_readable_callback_configuration check_validity send_request FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION, [], '', 9, 'C' 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.rb, line 235 def get_identity send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S' end
Returns true if the CALLBACK_READ
callback is enabled, false otherwise.
# File lib/tinkerforge/bricklet_rs232.rb, line 161 def is_read_callback_enabled check_validity send_request FUNCTION_IS_READ_CALLBACK_ENABLED, [], '', 9, '?' end
Returns the currently buffered message. The maximum length of message is 60. If the returned length is 0, no new data was available.
Instead of polling with this function, you can also use callbacks. See BrickletRS232#enable_read_callback
and CALLBACK_READ
callback.
# File lib/tinkerforge/bricklet_rs232.rb, line 135 def read check_validity send_request FUNCTION_READ, [], '', 69, 'k60 C' end
Returns up to one frame of bytes from the read buffer. The frame size is configured with BrickletRS232#set_frame_readable_callback_configuration
. If the returned length is 0, no new data was available.
- .. versionadded
-
2.0.4$nbsp;(Plugin)
# File lib/tinkerforge/bricklet_rs232.rb, line 219 def read_frame check_validity send_request FUNCTION_READ_FRAME, [], '', 69, 'k60 C' end
Registers a callback with ID id
to the block block
.
# File lib/tinkerforge/bricklet_rs232.rb, line 240 def register_callback(id, &block) callback = block @registered_callbacks[id] = callback end
Sets a break condition (the TX output is forced to a logic 0 state). The parameter sets the hold-time of the break condition.
- .. versionadded
-
2.0.2$nbsp;(Plugin)
# File lib/tinkerforge/bricklet_rs232.rb, line 187 def set_break_condition(break_time) check_validity send_request FUNCTION_SET_BREAK_CONDITION, [break_time], 'S', 8, '' end
Sets the configuration for the RS232 communication.
Hard-/Software flow control can either be on or off but not both simultaneously on.
# File lib/tinkerforge/bricklet_rs232.rb, line 170 def set_configuration(baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol) check_validity send_request FUNCTION_SET_CONFIGURATION, [baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol], 'C C 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.4$nbsp;(Plugin)
# File lib/tinkerforge/bricklet_rs232.rb, line 199 def set_frame_readable_callback_configuration(frame_size) check_validity send_request FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION, [frame_size], 'C', 8, '' end
Writes a string of up to 60 characters to the RS232 interface. The string can be binary data, ASCII or similar is not necessary.
The length of the string has to be given as an additional parameter.
The return value is the number of bytes that could be written.
See BrickletRS232#set_configuration
for configuration possibilities regarding baudrate, parity and so on.
# File lib/tinkerforge/bricklet_rs232.rb, line 124 def write(message, length) check_validity send_request FUNCTION_WRITE, [message, length], 'k60 C', 9, 'C' end