Class: Tinkerforge::BrickletRS232

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_rs232.rb

Overview

Communicates with RS232 devices

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

254
DEVICE_DISPLAY_NAME =

:nodoc:

'RS232 Bricklet'
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.

8
CALLBACK_ERROR =

This callback is called if an error occurs. Possible errors are overrun, parity or framing error.

.. versionadded

2.0.1$nbsp;(Plugin)

9
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 until BrickletRS232#read or BrickletRS232#read_frame is called. This means, that if you have configured a frame size of X bytes, you can read exactly X bytes using the BrickletRS232#read_frame function, every time the callback triggers without checking the frame count parameter.

.. versionadded

2.0.4$nbsp;(Plugin)

13
CALLBACK_READ_CALLBACK =

:nodoc: for backward compatibility

8
CALLBACK_ERROR_CALLBACK =

:nodoc: for backward compatibility

9
FUNCTION_WRITE =

:nodoc:

1
FUNCTION_READ =

: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_BREAK_CONDITION =

:nodoc:

10
FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION =

:nodoc:

11
FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION =

:nodoc:

12
FUNCTION_READ_FRAME =

:nodoc:

14
FUNCTION_GET_IDENTITY =

:nodoc:

255
BAUDRATE_300 =

:nodoc:

0
BAUDRATE_600 =

:nodoc:

1
BAUDRATE_1200 =

:nodoc:

2
BAUDRATE_2400 =

:nodoc:

3
BAUDRATE_4800 =

:nodoc:

4
BAUDRATE_9600 =

:nodoc:

5
BAUDRATE_14400 =

:nodoc:

6
BAUDRATE_19200 =

:nodoc:

7
BAUDRATE_28800 =

:nodoc:

8
BAUDRATE_38400 =

:nodoc:

9
BAUDRATE_57600 =

:nodoc:

10
BAUDRATE_115200 =

:nodoc:

11
BAUDRATE_230400 =

:nodoc:

12
PARITY_NONE =

:nodoc:

0
PARITY_ODD =

:nodoc:

1
PARITY_EVEN =

:nodoc:

2
PARITY_FORCED_PARITY_1 =

:nodoc:

3
PARITY_FORCED_PARITY_0 =

:nodoc:

4
STOPBITS_1 =

:nodoc:

1
STOPBITS_2 =

:nodoc:

2
WORDLENGTH_5 =

:nodoc:

5
WORDLENGTH_6 =

:nodoc:

6
WORDLENGTH_7 =

:nodoc:

7
WORDLENGTH_8 =

:nodoc:

8
HARDWARE_FLOWCONTROL_OFF =

:nodoc:

0
HARDWARE_FLOWCONTROL_ON =

:nodoc:

1
SOFTWARE_FLOWCONTROL_OFF =

:nodoc:

0
SOFTWARE_FLOWCONTROL_ON =

:nodoc:

1
ERROR_OVERRUN =

:nodoc:

1
ERROR_PARITY =

:nodoc:

2
ERROR_FRAMING =

:nodoc:

4

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

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) ⇒ BrickletRS232

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tinkerforge/bricklet_rs232.rb', line 91

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

Instance Method Details

#disable_read_callbackObject

Disables the CALLBACK_READ callback.

By default the callback is disabled.



154
155
156
157
158
# File 'lib/tinkerforge/bricklet_rs232.rb', line 154

def disable_read_callback
  check_validity

  send_request FUNCTION_DISABLE_READ_CALLBACK, [], '', 8, ''
end

#enable_read_callbackObject

Enables the CALLBACK_READ callback. This will disable the CALLBACK_FRAME_READABLE callback.

By default the callback is disabled.



145
146
147
148
149
# File 'lib/tinkerforge/bricklet_rs232.rb', line 145

def enable_read_callback
  check_validity

  send_request FUNCTION_ENABLE_READ_CALLBACK, [], '', 8, ''
end

#get_configurationObject

Returns the configuration as set by BrickletRS232#set_configuration.



178
179
180
181
182
# File 'lib/tinkerforge/bricklet_rs232.rb', line 178

def get_configuration
  check_validity

  send_request FUNCTION_GET_CONFIGURATION, [], '', 14, 'C C C C C C'
end

#get_frame_readable_callback_configurationObject

Returns the callback configuration as set by BrickletRS232#set_frame_readable_callback_configuration.

.. versionadded

2.0.4$nbsp;(Plugin)



209
210
211
212
213
# File 'lib/tinkerforge/bricklet_rs232.rb', line 209

def get_frame_readable_callback_configuration
  check_validity

  send_request FUNCTION_GET_FRAME_READABLE_CALLBACK_CONFIGURATION, [], '', 9, 'C'
end

#get_identityObject

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|



236
237
238
# File 'lib/tinkerforge/bricklet_rs232.rb', line 236

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#is_read_callback_enabledObject

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



162
163
164
165
166
# File 'lib/tinkerforge/bricklet_rs232.rb', line 162

def is_read_callback_enabled
  check_validity

  send_request FUNCTION_IS_READ_CALLBACK_ENABLED, [], '', 9, '?'
end

#readObject

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.



136
137
138
139
140
# File 'lib/tinkerforge/bricklet_rs232.rb', line 136

def read
  check_validity

  send_request FUNCTION_READ, [], '', 69, 'k60 C'
end

#read_frameObject

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)



220
221
222
223
224
# File 'lib/tinkerforge/bricklet_rs232.rb', line 220

def read_frame
  check_validity

  send_request FUNCTION_READ_FRAME, [], '', 69, 'k60 C'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



241
242
243
244
# File 'lib/tinkerforge/bricklet_rs232.rb', line 241

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#set_break_condition(break_time) ⇒ Object

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)



188
189
190
191
192
# File 'lib/tinkerforge/bricklet_rs232.rb', line 188

def set_break_condition(break_time)
  check_validity

  send_request FUNCTION_SET_BREAK_CONDITION, [break_time], 'S', 8, ''
end

#set_configuration(baudrate, parity, stopbits, wordlength, hardware_flowcontrol, software_flowcontrol) ⇒ Object

Sets the configuration for the RS232 communication.

Hard-/Software flow control can either be on or off but not both simultaneously on.



171
172
173
174
175
# File 'lib/tinkerforge/bricklet_rs232.rb', line 171

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

#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.4$nbsp;(Plugin)



200
201
202
203
204
# File 'lib/tinkerforge/bricklet_rs232.rb', line 200

def set_frame_readable_callback_configuration(frame_size)
  check_validity

  send_request FUNCTION_SET_FRAME_READABLE_CALLBACK_CONFIGURATION, [frame_size], 'C', 8, ''
end

#write(message, length) ⇒ Object

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.



125
126
127
128
129
# File 'lib/tinkerforge/bricklet_rs232.rb', line 125

def write(message, length)
  check_validity

  send_request FUNCTION_WRITE, [message, length], 'k60 C', 9, 'C'
end