tinkerforge 2.1.13 → 2.1.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/lib/tinkerforge/brick_dc.rb +122 -34
  2. data/lib/tinkerforge/brick_imu.rb +135 -49
  3. data/lib/tinkerforge/brick_imu_v2.rb +137 -54
  4. data/lib/tinkerforge/brick_master.rb +206 -120
  5. data/lib/tinkerforge/brick_red.rb +64 -69
  6. data/lib/tinkerforge/brick_servo.rb +133 -44
  7. data/lib/tinkerforge/brick_silent_stepper.rb +151 -65
  8. data/lib/tinkerforge/brick_stepper.rb +139 -51
  9. data/lib/tinkerforge/bricklet_accelerometer.rb +17 -18
  10. data/lib/tinkerforge/bricklet_ambient_light.rb +16 -19
  11. data/lib/tinkerforge/bricklet_ambient_light_v2.rb +13 -14
  12. data/lib/tinkerforge/bricklet_analog_in.rb +20 -23
  13. data/lib/tinkerforge/bricklet_analog_in_v2.rb +18 -21
  14. data/lib/tinkerforge/bricklet_analog_out.rb +8 -7
  15. data/lib/tinkerforge/bricklet_analog_out_v2.rb +7 -6
  16. data/lib/tinkerforge/bricklet_barometer.rb +21 -24
  17. data/lib/tinkerforge/bricklet_can.rb +14 -14
  18. data/lib/tinkerforge/bricklet_co2.rb +11 -12
  19. data/lib/tinkerforge/bricklet_color.rb +22 -25
  20. data/lib/tinkerforge/bricklet_current12.rb +18 -22
  21. data/lib/tinkerforge/bricklet_current25.rb +18 -22
  22. data/lib/tinkerforge/bricklet_distance_ir.rb +18 -21
  23. data/lib/tinkerforge/bricklet_distance_us.rb +13 -14
  24. data/lib/tinkerforge/bricklet_dual_button.rb +8 -8
  25. data/lib/tinkerforge/bricklet_dual_relay.rb +9 -9
  26. data/lib/tinkerforge/bricklet_dust_detector.rb +13 -14
  27. data/lib/tinkerforge/bricklet_gps.rb +21 -25
  28. data/lib/tinkerforge/bricklet_gps_v2.rb +56 -41
  29. data/lib/tinkerforge/bricklet_hall_effect.rb +13 -13
  30. data/lib/tinkerforge/bricklet_humidity.rb +16 -19
  31. data/lib/tinkerforge/bricklet_industrial_analog_out.rb +13 -12
  32. data/lib/tinkerforge/bricklet_industrial_digital_in_4.rb +15 -15
  33. data/lib/tinkerforge/bricklet_industrial_digital_out_4.rb +12 -12
  34. data/lib/tinkerforge/bricklet_industrial_dual_0_20ma.rb +13 -14
  35. data/lib/tinkerforge/bricklet_industrial_dual_analog_in.rb +16 -17
  36. data/lib/tinkerforge/bricklet_industrial_quad_relay.rb +12 -12
  37. data/lib/tinkerforge/bricklet_io16.rb +18 -19
  38. data/lib/tinkerforge/bricklet_io4.rb +18 -19
  39. data/lib/tinkerforge/bricklet_joystick.rb +18 -23
  40. data/lib/tinkerforge/bricklet_laser_range_finder.rb +26 -29
  41. data/lib/tinkerforge/bricklet_lcd_16x2.rb +14 -15
  42. data/lib/tinkerforge/bricklet_lcd_20x4.rb +18 -19
  43. data/lib/tinkerforge/bricklet_led_strip.rb +20 -20
  44. data/lib/tinkerforge/bricklet_line.rb +11 -12
  45. data/lib/tinkerforge/bricklet_linear_poti.rb +16 -19
  46. data/lib/tinkerforge/bricklet_load_cell.rb +20 -21
  47. data/lib/tinkerforge/bricklet_moisture.rb +13 -14
  48. data/lib/tinkerforge/bricklet_motion_detector.rb +7 -8
  49. data/lib/tinkerforge/bricklet_multi_touch.rb +10 -10
  50. data/lib/tinkerforge/bricklet_nfc_rfid.rb +11 -11
  51. data/lib/tinkerforge/bricklet_oled_128x64.rb +10 -9
  52. data/lib/tinkerforge/bricklet_oled_64x48.rb +10 -9
  53. data/lib/tinkerforge/bricklet_piezo_buzzer.rb +6 -7
  54. data/lib/tinkerforge/bricklet_piezo_speaker.rb +7 -8
  55. data/lib/tinkerforge/bricklet_ptc.rb +23 -26
  56. data/lib/tinkerforge/bricklet_real_time_clock.rb +13 -14
  57. data/lib/tinkerforge/bricklet_remote_switch.rb +12 -12
  58. data/lib/tinkerforge/bricklet_rgb_led.rb +6 -5
  59. data/lib/tinkerforge/bricklet_rotary_encoder.rb +12 -15
  60. data/lib/tinkerforge/bricklet_rotary_poti.rb +16 -19
  61. data/lib/tinkerforge/bricklet_rs232.rb +12 -13
  62. data/lib/tinkerforge/bricklet_rs485.rb +1373 -0
  63. data/lib/tinkerforge/bricklet_segment_display_4x7.rb +8 -8
  64. data/lib/tinkerforge/bricklet_solid_state_relay.rb +8 -8
  65. data/lib/tinkerforge/bricklet_sound_intensity.rb +11 -12
  66. data/lib/tinkerforge/bricklet_temperature.rb +13 -14
  67. data/lib/tinkerforge/bricklet_temperature_ir.rb +18 -21
  68. data/lib/tinkerforge/bricklet_thermocouple.rb +14 -16
  69. data/lib/tinkerforge/bricklet_tilt.rb +8 -8
  70. data/lib/tinkerforge/bricklet_uv_light.rb +11 -12
  71. data/lib/tinkerforge/bricklet_voltage.rb +16 -19
  72. data/lib/tinkerforge/bricklet_voltage_current.rb +25 -30
  73. data/lib/tinkerforge/ip_connection.rb +207 -138
  74. data/lib/tinkerforge/version.rb +1 -1
  75. metadata +3 -2
@@ -0,0 +1,1373 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+ #############################################################
3
+ # This file was automatically generated on 2017-07-27. #
4
+ # #
5
+ # Ruby Bindings Version 2.1.14 #
6
+ # #
7
+ # If you have a bugfix for this file and want to commit it, #
8
+ # please fix the bug in the generator. You can find a link #
9
+ # to the generators git repository on tinkerforge.com #
10
+ #############################################################
11
+
12
+ module Tinkerforge
13
+ # Communicates with RS485/Modbus devices with full- or half-duplex
14
+ class BrickletRS485 < Device
15
+ DEVICE_IDENTIFIER = 277 # :nodoc:
16
+ DEVICE_DISPLAY_NAME = 'RS485 Bricklet' # :nodoc:
17
+
18
+ # This callback is called if new data is available.
19
+ #
20
+ # To enable this callback, use BrickletRS485#enable_read_callback.
21
+ CALLBACK_READ_LOW_LEVEL = 41
22
+
23
+ # This callback is called if a new error occurs. It returns
24
+ # the current overrun and parity error count.
25
+ CALLBACK_ERROR_COUNT = 42
26
+
27
+ # This callback is called only in Modbus slave mode when the slave receives a
28
+ # valid request from a Modbus master to read coils. The parameters are
29
+ # request ID of the request, the starting address and the number of coils to
30
+ # be read as received by the request.
31
+ #
32
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_read_coils_request.
33
+ CALLBACK_MODBUS_SLAVE_READ_COILS_REQUEST = 43
34
+
35
+ # This callback is called only in Modbus master mode when the master receives a
36
+ # valid response of a request to read coils.
37
+ #
38
+ # The parameters are request ID
39
+ # of the request, exception code of the response and the data as received by the
40
+ # response.
41
+ #
42
+ # Any non-zero exception code indicates a problem. If the exception code
43
+ # is greater than zero then the number represents a Modbus exception code. If it is
44
+ # less than zero then it represents other errors. For example, -1 indicates that
45
+ # the request timed out or that the master did not receive any valid response of the
46
+ # request within the master request timeout period as set by
47
+ # BrickletRS485#set_modbus_configuration.
48
+ CALLBACK_MODBUS_MASTER_READ_COILS_RESPONSE_LOW_LEVEL = 44
49
+
50
+ # This callback is called only in Modbus slave mode when the slave receives a
51
+ # valid request from a Modbus master to read holding registers. The parameters
52
+ # are request ID of the request, the starting address and the number of holding
53
+ # registers to be read as received by the request.
54
+ #
55
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_read_holding_registers_request.
56
+ CALLBACK_MODBUS_SLAVE_READ_HOLDING_REGISTERS_REQUEST = 45
57
+
58
+ # This callback is called only in Modbus master mode when the master receives a
59
+ # valid response of a request to read holding registers.
60
+ #
61
+ # The parameters are
62
+ # request ID of the request, exception code of the response and the data as received
63
+ # by the response.
64
+ #
65
+ # Any non-zero exception code indicates a problem. If the exception
66
+ # code is greater than zero then the number represents a Modbus exception code. If
67
+ # it is less than zero then it represents other errors. For example, -1 indicates that
68
+ # the request timed out or that the master did not receive any valid response of the
69
+ # request within the master request timeout period as set by
70
+ # BrickletRS485#set_modbus_configuration.
71
+ CALLBACK_MODBUS_MASTER_READ_HOLDING_REGISTERS_RESPONSE_LOW_LEVEL = 46
72
+
73
+ # This callback is called only in Modbus slave mode when the slave receives a
74
+ # valid request from a Modbus master to write a single coil. The parameters
75
+ # are request ID of the request, the coil address and the value of coil to be
76
+ # written as received by the request.
77
+ #
78
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_single_coil_request.
79
+ CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_COIL_REQUEST = 47
80
+
81
+ # This callback is called only in Modbus master mode when the master receives a
82
+ # valid response of a request to write a single coil.
83
+ #
84
+ # The parameters are
85
+ # request ID of the request and exception code of the response.
86
+ #
87
+ # Any non-zero exception code indicates a problem.
88
+ # If the exception code is greater than zero then the number represents a Modbus
89
+ # exception code. If it is less than zero then it represents other errors. For
90
+ # example, -1 indicates that the request timed out or that the master did not receive
91
+ # any valid response of the request within the master request timeout period as set
92
+ # by BrickletRS485#set_modbus_configuration.
93
+ CALLBACK_MODBUS_MASTER_WRITE_SINGLE_COIL_RESPONSE = 48
94
+
95
+ # This callback is called only in Modbus slave mode when the slave receives a
96
+ # valid request from a Modbus master to write a single register. The parameters
97
+ # are request ID of the request, the register address and the register value to
98
+ # be written as received by the request.
99
+ #
100
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_single_register_request.
101
+ CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_REGISTER_REQUEST = 49
102
+
103
+ # This callback is called only in Modbus master mode when the master receives a
104
+ # valid response of a request to write a single register.
105
+ #
106
+ # The parameters are
107
+ # request ID of the request and exception code of the response.
108
+ #
109
+ # Any non-zero exception code
110
+ # indicates a problem. If the exception code is greater than zero then the number
111
+ # represents a Modbus exception code. If it is less than zero then it represents
112
+ # other errors. For example, -1 indicates that the request timed out or that the
113
+ # master did not receive any valid response of the request within the master request
114
+ # timeout period as set by BrickletRS485#set_modbus_configuration.
115
+ CALLBACK_MODBUS_MASTER_WRITE_SINGLE_REGISTER_RESPONSE = 50
116
+
117
+ # This callback is called only in Modbus slave mode when the slave receives a
118
+ # valid request from a Modbus master to write multiple coils. The parameters
119
+ # are request ID of the request, the starting address and the data to be written as
120
+ # received by the request.
121
+ #
122
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_multiple_coils_request.
123
+ CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_COILS_REQUEST_LOW_LEVEL = 51
124
+
125
+ # This callback is called only in Modbus master mode when the master receives a
126
+ # valid response of a request to read coils.
127
+ #
128
+ # The parameters are
129
+ # request ID of the request and exception code of the response.
130
+ #
131
+ # Any non-zero exception code
132
+ # indicates a problem. If the exception code is greater than zero then the number
133
+ # represents a Modbus exception code. If it is less than zero then it represents
134
+ # other errors. For example, -1 indicates that the request timedout or that the
135
+ # master did not receive any valid response of the request within the master request
136
+ # timeout period as set by BrickletRS485#set_modbus_configuration.
137
+ CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_COILS_RESPONSE = 52
138
+
139
+ # This callback is called only in Modbus slave mode when the slave receives a
140
+ # valid request from a Modbus master to write multiple registers. The parameters
141
+ # are request ID of the request, the starting address and the data to be written as
142
+ # received by the request.
143
+ #
144
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_multiple_registers_request.
145
+ CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_REGISTERS_REQUEST_LOW_LEVEL = 53
146
+
147
+ # This callback is called only in Modbus master mode when the master receives a
148
+ # valid response of a request to write multiple registers.
149
+ #
150
+ # The parameters
151
+ # are request ID of the request and exception code of the response.
152
+ #
153
+ # Any non-zero
154
+ # exception code indicates a problem. If the exception code is greater than zero then
155
+ # the number represents a Modbus exception code. If it is less than zero then it
156
+ # represents other errors. For example, -1 indicates that the request timedout or
157
+ # that the master did not receive any valid response of the request within the master
158
+ # request timeout period as set by BrickletRS485#set_modbus_configuration.
159
+ CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_RESPONSE = 54
160
+
161
+ # This callback is called only in Modbus slave mode when the slave receives a
162
+ # valid request from a Modbus master to read discrete inputs. The parameters
163
+ # are request ID of the request, the starting address and the number of discrete
164
+ # inputs to be read as received by the request.
165
+ #
166
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_read_discrete_inputs_request.
167
+ CALLBACK_MODBUS_SLAVE_READ_DISCRETE_INPUTS_REQUEST = 55
168
+
169
+ # This callback is called only in Modbus master mode when the master receives a
170
+ # valid response of a request to read discrete inputs.
171
+ #
172
+ # The parameters are
173
+ # request ID of the request, exception code of the response and the data as received
174
+ # by the response.
175
+ #
176
+ # Any non-zero exception code indicates a problem. If the exception
177
+ # code is greater than zero then the number represents a Modbus exception code. If
178
+ # it is less than zero then it represents other errors. For example, -1 indicates that
179
+ # the request timedout or that the master did not receive any valid response of the
180
+ # request within the master request timeout period as set by
181
+ # BrickletRS485#set_modbus_configuration.
182
+ CALLBACK_MODBUS_MASTER_READ_DISCRETE_INPUTS_RESPONSE_LOW_LEVEL = 56
183
+
184
+ # This callback is called only in Modbus slave mode when the slave receives a
185
+ # valid request from a Modbus master to read input registers. The parameters
186
+ # are request ID of the request, the starting address and the number of input
187
+ # registers to be read as received by the request.
188
+ #
189
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_read_input_registers_request.
190
+ CALLBACK_MODBUS_SLAVE_READ_INPUT_REGISTERS_REQUEST = 57
191
+
192
+ # This callback is called only in Modbus master mode when the master receives a
193
+ # valid response of a request to read input registers.
194
+ #
195
+ # The parameters are
196
+ # request ID of the request, exception code of the response and the data as received
197
+ # by the response.
198
+ #
199
+ # Any non-zero exception code indicates a problem. If the exception
200
+ # code is greater than zero then the number represents a Modbus exception code. If
201
+ # it is less than zero then it represents other errors. For example, -1 indicates that
202
+ # the request timedout or that the master did not receive any valid response of the
203
+ # request within the master request timeout period as set by
204
+ # BrickletRS485#set_modbus_configuration.
205
+ CALLBACK_MODBUS_MASTER_READ_INPUT_REGISTERS_RESPONSE_LOW_LEVEL = 58
206
+
207
+ # This callback is called if new data is available.
208
+ #
209
+ # To enable this callback, use BrickletRS485#enable_read_callback.
210
+ CALLBACK_READ = -41
211
+
212
+ # This callback is called only in Modbus master mode when the master receives a
213
+ # valid response of a request to read coils.
214
+ #
215
+ # The parameters are request ID
216
+ # of the request, exception code of the response and the data as received by the
217
+ # response.
218
+ #
219
+ # Any non-zero exception code indicates a problem. If the exception code
220
+ # is greater than zero then the number represents a Modbus exception code. If it is
221
+ # less than zero then it represents other errors. For example, -1 indicates that
222
+ # the request timed out or that the master did not receive any valid response of the
223
+ # request within the master request timeout period as set by
224
+ # BrickletRS485#set_modbus_configuration.
225
+ CALLBACK_MODBUS_MASTER_READ_COILS_RESPONSE = -44
226
+
227
+ # This callback is called only in Modbus master mode when the master receives a
228
+ # valid response of a request to read holding registers.
229
+ #
230
+ # The parameters are
231
+ # request ID of the request, exception code of the response and the data as received
232
+ # by the response.
233
+ #
234
+ # Any non-zero exception code indicates a problem. If the exception
235
+ # code is greater than zero then the number represents a Modbus exception code. If
236
+ # it is less than zero then it represents other errors. For example, -1 indicates that
237
+ # the request timed out or that the master did not receive any valid response of the
238
+ # request within the master request timeout period as set by
239
+ # BrickletRS485#set_modbus_configuration.
240
+ CALLBACK_MODBUS_MASTER_READ_HOLDING_REGISTERS_RESPONSE = -46
241
+
242
+ # This callback is called only in Modbus slave mode when the slave receives a
243
+ # valid request from a Modbus master to write multiple coils. The parameters
244
+ # are request ID of the request, the starting address and the data to be written as
245
+ # received by the request.
246
+ #
247
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_multiple_coils_request.
248
+ CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_COILS_REQUEST = -51
249
+
250
+ # This callback is called only in Modbus slave mode when the slave receives a
251
+ # valid request from a Modbus master to write multiple registers. The parameters
252
+ # are request ID of the request, the starting address and the data to be written as
253
+ # received by the request.
254
+ #
255
+ # To send a response of this request use BrickletRS485#modbus_slave_answer_write_multiple_registers_request.
256
+ CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_REGISTERS_REQUEST = -53
257
+
258
+ # This callback is called only in Modbus master mode when the master receives a
259
+ # valid response of a request to read discrete inputs.
260
+ #
261
+ # The parameters are
262
+ # request ID of the request, exception code of the response and the data as received
263
+ # by the response.
264
+ #
265
+ # Any non-zero exception code indicates a problem. If the exception
266
+ # code is greater than zero then the number represents a Modbus exception code. If
267
+ # it is less than zero then it represents other errors. For example, -1 indicates that
268
+ # the request timedout or that the master did not receive any valid response of the
269
+ # request within the master request timeout period as set by
270
+ # BrickletRS485#set_modbus_configuration.
271
+ CALLBACK_MODBUS_MASTER_READ_DISCRETE_INPUTS_RESPONSE = -56
272
+
273
+ # This callback is called only in Modbus master mode when the master receives a
274
+ # valid response of a request to read input registers.
275
+ #
276
+ # The parameters are
277
+ # request ID of the request, exception code of the response and the data as received
278
+ # by the response.
279
+ #
280
+ # Any non-zero exception code indicates a problem. If the exception
281
+ # code is greater than zero then the number represents a Modbus exception code. If
282
+ # it is less than zero then it represents other errors. For example, -1 indicates that
283
+ # the request timedout or that the master did not receive any valid response of the
284
+ # request within the master request timeout period as set by
285
+ # BrickletRS485#set_modbus_configuration.
286
+ CALLBACK_MODBUS_MASTER_READ_INPUT_REGISTERS_RESPONSE = -58
287
+
288
+ FUNCTION_WRITE_LOW_LEVEL = 1 # :nodoc:
289
+ FUNCTION_READ_LOW_LEVEL = 2 # :nodoc:
290
+ FUNCTION_ENABLE_READ_CALLBACK = 3 # :nodoc:
291
+ FUNCTION_DISABLE_READ_CALLBACK = 4 # :nodoc:
292
+ FUNCTION_IS_READ_CALLBACK_ENABLED = 5 # :nodoc:
293
+ FUNCTION_SET_RS485_CONFIGURATION = 6 # :nodoc:
294
+ FUNCTION_GET_RS485_CONFIGURATION = 7 # :nodoc:
295
+ FUNCTION_SET_MODBUS_CONFIGURATION = 8 # :nodoc:
296
+ FUNCTION_GET_MODBUS_CONFIGURATION = 9 # :nodoc:
297
+ FUNCTION_SET_MODE = 10 # :nodoc:
298
+ FUNCTION_GET_MODE = 11 # :nodoc:
299
+ FUNCTION_SET_COMMUNICATION_LED_CONFIG = 12 # :nodoc:
300
+ FUNCTION_GET_COMMUNICATION_LED_CONFIG = 13 # :nodoc:
301
+ FUNCTION_SET_ERROR_LED_CONFIG = 14 # :nodoc:
302
+ FUNCTION_GET_ERROR_LED_CONFIG = 15 # :nodoc:
303
+ FUNCTION_SET_BUFFER_CONFIG = 16 # :nodoc:
304
+ FUNCTION_GET_BUFFER_CONFIG = 17 # :nodoc:
305
+ FUNCTION_GET_BUFFER_STATUS = 18 # :nodoc:
306
+ FUNCTION_ENABLE_ERROR_COUNT_CALLBACK = 19 # :nodoc:
307
+ FUNCTION_DISABLE_ERROR_COUNT_CALLBACK = 20 # :nodoc:
308
+ FUNCTION_IS_ERROR_COUNT_CALLBACK_ENABLED = 21 # :nodoc:
309
+ FUNCTION_GET_ERROR_COUNT = 22 # :nodoc:
310
+ FUNCTION_GET_MODBUS_COMMON_ERROR_COUNT = 23 # :nodoc:
311
+ FUNCTION_MODBUS_SLAVE_REPORT_EXCEPTION = 24 # :nodoc:
312
+ FUNCTION_MODBUS_SLAVE_ANSWER_READ_COILS_REQUEST_LOW_LEVEL = 25 # :nodoc:
313
+ FUNCTION_MODBUS_MASTER_READ_COILS = 26 # :nodoc:
314
+ FUNCTION_MODBUS_SLAVE_ANSWER_READ_HOLDING_REGISTERS_REQUEST_LOW_LEVEL = 27 # :nodoc:
315
+ FUNCTION_MODBUS_MASTER_READ_HOLDING_REGISTERS = 28 # :nodoc:
316
+ FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_COIL_REQUEST = 29 # :nodoc:
317
+ FUNCTION_MODBUS_MASTER_WRITE_SINGLE_COIL = 30 # :nodoc:
318
+ FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_REGISTER_REQUEST = 31 # :nodoc:
319
+ FUNCTION_MODBUS_MASTER_WRITE_SINGLE_REGISTER = 32 # :nodoc:
320
+ FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_COILS_REQUEST = 33 # :nodoc:
321
+ FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_COILS_LOW_LEVEL = 34 # :nodoc:
322
+ FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_REGISTERS_REQUEST = 35 # :nodoc:
323
+ FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_LOW_LEVEL = 36 # :nodoc:
324
+ FUNCTION_MODBUS_SLAVE_ANSWER_READ_DISCRETE_INPUTS_REQUEST_LOW_LEVEL = 37 # :nodoc:
325
+ FUNCTION_MODBUS_MASTER_READ_DISCRETE_INPUTS = 38 # :nodoc:
326
+ FUNCTION_MODBUS_SLAVE_ANSWER_READ_INPUT_REGISTERS_REQUEST_LOW_LEVEL = 39 # :nodoc:
327
+ FUNCTION_MODBUS_MASTER_READ_INPUT_REGISTERS = 40 # :nodoc:
328
+ FUNCTION_GET_SPITFP_ERROR_COUNT = 234 # :nodoc:
329
+ FUNCTION_SET_BOOTLOADER_MODE = 235 # :nodoc:
330
+ FUNCTION_GET_BOOTLOADER_MODE = 236 # :nodoc:
331
+ FUNCTION_SET_WRITE_FIRMWARE_POINTER = 237 # :nodoc:
332
+ FUNCTION_WRITE_FIRMWARE = 238 # :nodoc:
333
+ FUNCTION_SET_STATUS_LED_CONFIG = 239 # :nodoc:
334
+ FUNCTION_GET_STATUS_LED_CONFIG = 240 # :nodoc:
335
+ FUNCTION_GET_CHIP_TEMPERATURE = 242 # :nodoc:
336
+ FUNCTION_RESET = 243 # :nodoc:
337
+ FUNCTION_WRITE_UID = 248 # :nodoc:
338
+ FUNCTION_READ_UID = 249 # :nodoc:
339
+ FUNCTION_GET_IDENTITY = 255 # :nodoc:
340
+
341
+ PARITY_NONE = 0 # :nodoc:
342
+ PARITY_ODD = 1 # :nodoc:
343
+ PARITY_EVEN = 2 # :nodoc:
344
+ STOPBITS_1 = 1 # :nodoc:
345
+ STOPBITS_2 = 2 # :nodoc:
346
+ WORDLENGTH_5 = 5 # :nodoc:
347
+ WORDLENGTH_6 = 6 # :nodoc:
348
+ WORDLENGTH_7 = 7 # :nodoc:
349
+ WORDLENGTH_8 = 8 # :nodoc:
350
+ DUPLEX_HALF = 0 # :nodoc:
351
+ DUPLEX_FULL = 1 # :nodoc:
352
+ MODE_RS485 = 0 # :nodoc:
353
+ MODE_MODBUS_MASTER_RTU = 1 # :nodoc:
354
+ MODE_MODBUS_SLAVE_RTU = 2 # :nodoc:
355
+ COMMUNICATION_LED_CONFIG_OFF = 0 # :nodoc:
356
+ COMMUNICATION_LED_CONFIG_ON = 1 # :nodoc:
357
+ COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT = 2 # :nodoc:
358
+ COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION = 3 # :nodoc:
359
+ ERROR_LED_CONFIG_OFF = 0 # :nodoc:
360
+ ERROR_LED_CONFIG_ON = 1 # :nodoc:
361
+ ERROR_LED_CONFIG_SHOW_HEARTBEAT = 2 # :nodoc:
362
+ ERROR_LED_CONFIG_SHOW_ERROR = 3 # :nodoc:
363
+ EXCEPTION_CODE_TIMEOUT = -1 # :nodoc:
364
+ EXCEPTION_CODE_SUCCESS = 0 # :nodoc:
365
+ EXCEPTION_CODE_ILLEGAL_FUNCTION = 1 # :nodoc:
366
+ EXCEPTION_CODE_ILLEGAL_DATA_ADDRESS = 2 # :nodoc:
367
+ EXCEPTION_CODE_ILLEGAL_DATA_VALUE = 3 # :nodoc:
368
+ EXCEPTION_CODE_SLAVE_DEVICE_FAILURE = 4 # :nodoc:
369
+ EXCEPTION_CODE_ACKNOWLEDGE = 5 # :nodoc:
370
+ EXCEPTION_CODE_SLAVE_DEVICE_BUSY = 6 # :nodoc:
371
+ EXCEPTION_CODE_MEMORY_PARITY_ERROR = 8 # :nodoc:
372
+ EXCEPTION_CODE_GATEWAY_PATH_UNAVAILABLE = 10 # :nodoc:
373
+ EXCEPTION_CODE_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND = 11 # :nodoc:
374
+ BOOTLOADER_MODE_BOOTLOADER = 0 # :nodoc:
375
+ BOOTLOADER_MODE_FIRMWARE = 1 # :nodoc:
376
+ BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT = 2 # :nodoc:
377
+ BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT = 3 # :nodoc:
378
+ BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT = 4 # :nodoc:
379
+ BOOTLOADER_STATUS_OK = 0 # :nodoc:
380
+ BOOTLOADER_STATUS_INVALID_MODE = 1 # :nodoc:
381
+ BOOTLOADER_STATUS_NO_CHANGE = 2 # :nodoc:
382
+ BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT = 3 # :nodoc:
383
+ BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT = 4 # :nodoc:
384
+ BOOTLOADER_STATUS_CRC_MISMATCH = 5 # :nodoc:
385
+ STATUS_LED_CONFIG_OFF = 0 # :nodoc:
386
+ STATUS_LED_CONFIG_ON = 1 # :nodoc:
387
+ STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2 # :nodoc:
388
+ STATUS_LED_CONFIG_SHOW_STATUS = 3 # :nodoc:
389
+
390
+ # Creates an object with the unique device ID <tt>uid</tt> and adds it to
391
+ # the IP Connection <tt>ipcon</tt>.
392
+ def initialize(uid, ipcon)
393
+ super uid, ipcon
394
+
395
+ @api_version = [2, 0, 0]
396
+
397
+ @response_expected[FUNCTION_WRITE_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
398
+ @response_expected[FUNCTION_READ_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
399
+ @response_expected[FUNCTION_ENABLE_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE
400
+ @response_expected[FUNCTION_DISABLE_READ_CALLBACK] = RESPONSE_EXPECTED_TRUE
401
+ @response_expected[FUNCTION_IS_READ_CALLBACK_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
402
+ @response_expected[FUNCTION_SET_RS485_CONFIGURATION] = RESPONSE_EXPECTED_FALSE
403
+ @response_expected[FUNCTION_GET_RS485_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
404
+ @response_expected[FUNCTION_SET_MODBUS_CONFIGURATION] = RESPONSE_EXPECTED_FALSE
405
+ @response_expected[FUNCTION_GET_MODBUS_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
406
+ @response_expected[FUNCTION_SET_MODE] = RESPONSE_EXPECTED_FALSE
407
+ @response_expected[FUNCTION_GET_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
408
+ @response_expected[FUNCTION_SET_COMMUNICATION_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
409
+ @response_expected[FUNCTION_GET_COMMUNICATION_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
410
+ @response_expected[FUNCTION_SET_ERROR_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
411
+ @response_expected[FUNCTION_GET_ERROR_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
412
+ @response_expected[FUNCTION_SET_BUFFER_CONFIG] = RESPONSE_EXPECTED_FALSE
413
+ @response_expected[FUNCTION_GET_BUFFER_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
414
+ @response_expected[FUNCTION_GET_BUFFER_STATUS] = RESPONSE_EXPECTED_ALWAYS_TRUE
415
+ @response_expected[FUNCTION_ENABLE_ERROR_COUNT_CALLBACK] = RESPONSE_EXPECTED_TRUE
416
+ @response_expected[FUNCTION_DISABLE_ERROR_COUNT_CALLBACK] = RESPONSE_EXPECTED_TRUE
417
+ @response_expected[FUNCTION_IS_ERROR_COUNT_CALLBACK_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
418
+ @response_expected[FUNCTION_GET_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
419
+ @response_expected[FUNCTION_GET_MODBUS_COMMON_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
420
+ @response_expected[FUNCTION_MODBUS_SLAVE_REPORT_EXCEPTION] = RESPONSE_EXPECTED_FALSE
421
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_READ_COILS_REQUEST_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
422
+ @response_expected[FUNCTION_MODBUS_MASTER_READ_COILS] = RESPONSE_EXPECTED_ALWAYS_TRUE
423
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_READ_HOLDING_REGISTERS_REQUEST_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
424
+ @response_expected[FUNCTION_MODBUS_MASTER_READ_HOLDING_REGISTERS] = RESPONSE_EXPECTED_ALWAYS_TRUE
425
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_COIL_REQUEST] = RESPONSE_EXPECTED_FALSE
426
+ @response_expected[FUNCTION_MODBUS_MASTER_WRITE_SINGLE_COIL] = RESPONSE_EXPECTED_ALWAYS_TRUE
427
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_REGISTER_REQUEST] = RESPONSE_EXPECTED_FALSE
428
+ @response_expected[FUNCTION_MODBUS_MASTER_WRITE_SINGLE_REGISTER] = RESPONSE_EXPECTED_ALWAYS_TRUE
429
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_COILS_REQUEST] = RESPONSE_EXPECTED_FALSE
430
+ @response_expected[FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_COILS_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
431
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_REGISTERS_REQUEST] = RESPONSE_EXPECTED_FALSE
432
+ @response_expected[FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
433
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_READ_DISCRETE_INPUTS_REQUEST_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
434
+ @response_expected[FUNCTION_MODBUS_MASTER_READ_DISCRETE_INPUTS] = RESPONSE_EXPECTED_ALWAYS_TRUE
435
+ @response_expected[FUNCTION_MODBUS_SLAVE_ANSWER_READ_INPUT_REGISTERS_REQUEST_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
436
+ @response_expected[FUNCTION_MODBUS_MASTER_READ_INPUT_REGISTERS] = RESPONSE_EXPECTED_ALWAYS_TRUE
437
+ @response_expected[FUNCTION_GET_SPITFP_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
438
+ @response_expected[FUNCTION_SET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
439
+ @response_expected[FUNCTION_GET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
440
+ @response_expected[FUNCTION_SET_WRITE_FIRMWARE_POINTER] = RESPONSE_EXPECTED_FALSE
441
+ @response_expected[FUNCTION_WRITE_FIRMWARE] = RESPONSE_EXPECTED_ALWAYS_TRUE
442
+ @response_expected[FUNCTION_SET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
443
+ @response_expected[FUNCTION_GET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
444
+ @response_expected[FUNCTION_GET_CHIP_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
445
+ @response_expected[FUNCTION_RESET] = RESPONSE_EXPECTED_FALSE
446
+ @response_expected[FUNCTION_WRITE_UID] = RESPONSE_EXPECTED_FALSE
447
+ @response_expected[FUNCTION_READ_UID] = RESPONSE_EXPECTED_ALWAYS_TRUE
448
+ @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE
449
+
450
+ @callback_formats[CALLBACK_READ_LOW_LEVEL] = 'S S k60'
451
+ @callback_formats[CALLBACK_ERROR_COUNT] = 'L L'
452
+ @callback_formats[CALLBACK_MODBUS_SLAVE_READ_COILS_REQUEST] = 'C L S'
453
+ @callback_formats[CALLBACK_MODBUS_MASTER_READ_COILS_RESPONSE_LOW_LEVEL] = 'C c S S ?464'
454
+ @callback_formats[CALLBACK_MODBUS_SLAVE_READ_HOLDING_REGISTERS_REQUEST] = 'C L S'
455
+ @callback_formats[CALLBACK_MODBUS_MASTER_READ_HOLDING_REGISTERS_RESPONSE_LOW_LEVEL] = 'C c S S S29'
456
+ @callback_formats[CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_COIL_REQUEST] = 'C L ?'
457
+ @callback_formats[CALLBACK_MODBUS_MASTER_WRITE_SINGLE_COIL_RESPONSE] = 'C c'
458
+ @callback_formats[CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_REGISTER_REQUEST] = 'C L S'
459
+ @callback_formats[CALLBACK_MODBUS_MASTER_WRITE_SINGLE_REGISTER_RESPONSE] = 'C c'
460
+ @callback_formats[CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_COILS_REQUEST_LOW_LEVEL] = 'C L S S ?440'
461
+ @callback_formats[CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_COILS_RESPONSE] = 'C c'
462
+ @callback_formats[CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_REGISTERS_REQUEST_LOW_LEVEL] = 'C L S S S27'
463
+ @callback_formats[CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_RESPONSE] = 'C c'
464
+ @callback_formats[CALLBACK_MODBUS_SLAVE_READ_DISCRETE_INPUTS_REQUEST] = 'C L S'
465
+ @callback_formats[CALLBACK_MODBUS_MASTER_READ_DISCRETE_INPUTS_RESPONSE_LOW_LEVEL] = 'C c S S ?464'
466
+ @callback_formats[CALLBACK_MODBUS_SLAVE_READ_INPUT_REGISTERS_REQUEST] = 'C L S'
467
+ @callback_formats[CALLBACK_MODBUS_MASTER_READ_INPUT_REGISTERS_RESPONSE_LOW_LEVEL] = 'C c S S S29'
468
+
469
+ @high_level_callbacks[CALLBACK_READ] = [['stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
470
+ @high_level_callbacks[CALLBACK_MODBUS_MASTER_READ_COILS_RESPONSE] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
471
+ @high_level_callbacks[CALLBACK_MODBUS_MASTER_READ_HOLDING_REGISTERS_RESPONSE] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
472
+ @high_level_callbacks[CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_COILS_REQUEST] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
473
+ @high_level_callbacks[CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_REGISTERS_REQUEST] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
474
+ @high_level_callbacks[CALLBACK_MODBUS_MASTER_READ_DISCRETE_INPUTS_RESPONSE] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
475
+ @high_level_callbacks[CALLBACK_MODBUS_MASTER_READ_INPUT_REGISTERS_RESPONSE] = [[nil, nil, 'stream_length', 'stream_chunk_offset', 'stream_chunk_data'], {'fixed_length' => nil, 'single_chunk' => false}, nil]
476
+ end
477
+
478
+ # Writes characters to the RS485 interface. The characters can be binary data,
479
+ # ASCII or similar is not necessary.
480
+ #
481
+ # The return value is the number of characters that were written.
482
+ #
483
+ # See BrickletRS485#set_rs485_configuration for configuration possibilities
484
+ # regarding baudrate, parity and so on.
485
+ def write_low_level(message_length, message_chunk_offset, message_chunk_data)
486
+ send_request FUNCTION_WRITE_LOW_LEVEL, [message_length, message_chunk_offset, message_chunk_data], 'S S k60', 1, 'C'
487
+ end
488
+
489
+ # Returns up to *length* characters from receive buffer.
490
+ #
491
+ # Instead of polling with this function, you can also use
492
+ # callbacks. But note that this function will return available
493
+ # data only when the read callback is disabled.
494
+ # See BrickletRS485#enable_read_callback and CALLBACK_READ callback.
495
+ def read_low_level(length)
496
+ send_request FUNCTION_READ_LOW_LEVEL, [length], 'S', 64, 'S S k60'
497
+ end
498
+
499
+ # Enables the CALLBACK_READ callback.
500
+ #
501
+ # By default the callback is disabled.
502
+ def enable_read_callback
503
+ send_request FUNCTION_ENABLE_READ_CALLBACK, [], '', 0, ''
504
+ end
505
+
506
+ # Disables the CALLBACK_READ callback.
507
+ #
508
+ # By default the callback is disabled.
509
+ def disable_read_callback
510
+ send_request FUNCTION_DISABLE_READ_CALLBACK, [], '', 0, ''
511
+ end
512
+
513
+ # Returns *true* if the CALLBACK_READ callback is enabled,
514
+ # *false* otherwise.
515
+ def is_read_callback_enabled
516
+ send_request FUNCTION_IS_READ_CALLBACK_ENABLED, [], '', 1, '?'
517
+ end
518
+
519
+ # Sets the configuration for the RS485 communication. Available options:
520
+ #
521
+ # * Baudrate between 100 and 2000000 baud.
522
+ # * Parity of none, odd or even.
523
+ # * Stopbits can be 1 or 2.
524
+ # * Word length of 5 to 8.
525
+ # * Half- or Full-Duplex.
526
+ #
527
+ # The default is: 115200 baud, parity none, 1 stop bit, word length 8, half duplex.
528
+ def set_rs485_configuration(baudrate, parity, stopbits, wordlength, duplex)
529
+ send_request FUNCTION_SET_RS485_CONFIGURATION, [baudrate, parity, stopbits, wordlength, duplex], 'L C C C C', 0, ''
530
+ end
531
+
532
+ # Returns the configuration as set by BrickletRS485#set_rs485_configuration.
533
+ def get_rs485_configuration
534
+ send_request FUNCTION_GET_RS485_CONFIGURATION, [], '', 8, 'L C C C C'
535
+ end
536
+
537
+ # Sets the configuration for the RS485 Modbus communication. Available options:
538
+ #
539
+ # * Slave Address: Address to be used as the Modbus slave address in Modbus slave mode. Valid Modbus slave address range is 1 to 247.
540
+ # * Master Request Timeout: Specifies how long the master should wait for a response from a slave in milliseconds when in Modbus master mode.
541
+ #
542
+ # The default is: Slave Address = 1 and Master Request Timeout = 1000 milliseconds (1 second).
543
+ def set_modbus_configuration(slave_address, master_request_timeout)
544
+ send_request FUNCTION_SET_MODBUS_CONFIGURATION, [slave_address, master_request_timeout], 'C L', 0, ''
545
+ end
546
+
547
+ # Returns the configuration as set by BrickletRS485#set_modbus_configuration.
548
+ def get_modbus_configuration
549
+ send_request FUNCTION_GET_MODBUS_CONFIGURATION, [], '', 5, 'C L'
550
+ end
551
+
552
+ # Sets the mode of the Bricklet in which it operates. Available options are
553
+ #
554
+ # * RS485,
555
+ # * Modbus Master RTU and
556
+ # * Modbus Slave RTU.
557
+ #
558
+ # The default is: RS485 mode.
559
+ def set_mode(mode)
560
+ send_request FUNCTION_SET_MODE, [mode], 'C', 0, ''
561
+ end
562
+
563
+ # Returns the configuration as set by BrickletRS485#set_mode.
564
+ def get_mode
565
+ send_request FUNCTION_GET_MODE, [], '', 1, 'C'
566
+ end
567
+
568
+ # Sets the communication LED configuration. By default the LED shows
569
+ # communication traffic, it flickers once for every 10 received data packets.
570
+ #
571
+ # You can also turn the LED permanently on/off or show a heartbeat.
572
+ #
573
+ # If the Bricklet is in bootloader mode, the LED is off.
574
+ def set_communication_led_config(config)
575
+ send_request FUNCTION_SET_COMMUNICATION_LED_CONFIG, [config], 'C', 0, ''
576
+ end
577
+
578
+ # Returns the configuration as set by BrickletRS485#set_communication_led_config
579
+ def get_communication_led_config
580
+ send_request FUNCTION_GET_COMMUNICATION_LED_CONFIG, [], '', 1, 'C'
581
+ end
582
+
583
+ # Sets the error LED configuration.
584
+ #
585
+ # By default the error LED turns on if there is any error (see CALLBACK_ERROR_COUNT
586
+ # callback). If you call this function with the SHOW ERROR option again, the LED
587
+ # will turn off until the next error occurs.
588
+ #
589
+ # You can also turn the LED permanently on/off or show a heartbeat.
590
+ #
591
+ # If the Bricklet is in bootloader mode, the LED is off.
592
+ def set_error_led_config(config)
593
+ send_request FUNCTION_SET_ERROR_LED_CONFIG, [config], 'C', 0, ''
594
+ end
595
+
596
+ # Returns the configuration as set by BrickletRS485#set_error_led_config.
597
+ def get_error_led_config
598
+ send_request FUNCTION_GET_ERROR_LED_CONFIG, [], '', 1, 'C'
599
+ end
600
+
601
+ # Sets the send and receive buffer size in byte. In sum there is
602
+ # 10240 byte (10kb) buffer available and the minimum buffer size
603
+ # is 1024 byte (1kb) for both.
604
+ #
605
+ # The current buffer content is lost if this function is called.
606
+ #
607
+ # The send buffer holds data that is given by BrickletRS485#write and
608
+ # can not be written yet. The receive buffer holds data that is
609
+ # received through RS485 but could not yet be send to the
610
+ # user, either by BrickletRS485#read or through CALLBACK_READ callback.
611
+ #
612
+ # The default configuration is 5120 byte (5kb) per buffer.
613
+ def set_buffer_config(send_buffer_size, receive_buffer_size)
614
+ send_request FUNCTION_SET_BUFFER_CONFIG, [send_buffer_size, receive_buffer_size], 'S S', 0, ''
615
+ end
616
+
617
+ # Returns the buffer configuration as set by BrickletRS485#set_buffer_config.
618
+ def get_buffer_config
619
+ send_request FUNCTION_GET_BUFFER_CONFIG, [], '', 4, 'S S'
620
+ end
621
+
622
+ # Returns the currently used bytes for the send and received buffer.
623
+ #
624
+ # See BrickletRS485#set_buffer_config for buffer size configuration.
625
+ def get_buffer_status
626
+ send_request FUNCTION_GET_BUFFER_STATUS, [], '', 4, 'S S'
627
+ end
628
+
629
+ # Enables the CALLBACK_ERROR_COUNT callback.
630
+ #
631
+ # By default the callback is disabled.
632
+ def enable_error_count_callback
633
+ send_request FUNCTION_ENABLE_ERROR_COUNT_CALLBACK, [], '', 0, ''
634
+ end
635
+
636
+ # Disables the CALLBACK_ERROR_COUNT callback.
637
+ #
638
+ # By default the callback is disabled.
639
+ def disable_error_count_callback
640
+ send_request FUNCTION_DISABLE_ERROR_COUNT_CALLBACK, [], '', 0, ''
641
+ end
642
+
643
+ # Returns *true* if the CALLBACK_ERROR_COUNT callback is enabled,
644
+ # *false* otherwise.
645
+ def is_error_count_callback_enabled
646
+ send_request FUNCTION_IS_ERROR_COUNT_CALLBACK_ENABLED, [], '', 1, '?'
647
+ end
648
+
649
+ # Returns the current number of overrun and parity errors.
650
+ def get_error_count
651
+ send_request FUNCTION_GET_ERROR_COUNT, [], '', 8, 'L L'
652
+ end
653
+
654
+ # Returns the current number of errors occurred in Modbus mode.
655
+ #
656
+ # * Timeout Error Count: Number of timeouts occurred.
657
+ # * Checksum Error Count: Number of failures due to Modbus frame CRC16 checksum mismatch.
658
+ # * Frame Too Big Error Count: Number of times frames were rejected because they exceeded maximum Modbus frame size which is 256 bytes.
659
+ # * Illegal Function Error Count: Number of errors when an unimplemented or illegal function is requested. This corresponds to Modbus exception code 1.
660
+ # * Illegal Data Address Error Count: Number of errors due to invalid data address. This corresponds to Modbus exception code 2.
661
+ # * Illegal Data Value Error Count: Number of errors due to invalid data value. This corresponds to Modbus exception code 3.
662
+ # * Slave Device Failure Error Count: Number of errors occurred on the slave device which were unrecoverable. This corresponds to Modbus exception code 4.
663
+ def get_modbus_common_error_count
664
+ send_request FUNCTION_GET_MODBUS_COMMON_ERROR_COUNT, [], '', 28, 'L L L L L L L'
665
+ end
666
+
667
+ # In Modbus slave mode this function can be used to report a Modbus exception for
668
+ # a Modbus master request.
669
+ #
670
+ # * Request ID: Request ID of the request received by the slave.
671
+ # * Exception Code: Modbus exception code to report to the Modbus master.
672
+ def modbus_slave_report_exception(request_id, exception_code)
673
+ send_request FUNCTION_MODBUS_SLAVE_REPORT_EXCEPTION, [request_id, exception_code], 'C c', 0, ''
674
+ end
675
+
676
+ # In Modbus slave mode this function can be used to answer a master request to
677
+ # read coils.
678
+ #
679
+ # * Request ID: Request ID of the corresponding request that is being answered.
680
+ # * Coils: Data that is to be sent to the Modbus master for the corresponding request.
681
+ #
682
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_COILS_REQUEST callback
683
+ # with the Request ID as provided by the argument of the callback.
684
+ def modbus_slave_answer_read_coils_request_low_level(request_id, coils_length, coils_chunk_offset, coils_chunk_data)
685
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_READ_COILS_REQUEST_LOW_LEVEL, [request_id, coils_length, coils_chunk_offset, coils_chunk_data], 'C S S ?472', 0, ''
686
+ end
687
+
688
+ # In Modbus master mode this function can be used to read coils from a slave. This
689
+ # function creates a Modbus function code 1 request.
690
+ #
691
+ # * Slave Address: Address of the target Modbus slave.
692
+ # * Starting Address: Starting address of the read.
693
+ # * Count: Number of coils to read.
694
+ #
695
+ # Upon success the function will return a non-zero request ID which will represent
696
+ # the current request initiated by the Modbus master. In case of failure the returned
697
+ # request ID will be 0.
698
+ #
699
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_READ_COILS_RESPONSE
700
+ # callback. In this callback the Request ID provided by the callback argument must be
701
+ # matched with the Request ID returned from this function to verify that the callback
702
+ # is indeed for a particular request.
703
+ def modbus_master_read_coils(slave_address, starting_address, count)
704
+ send_request FUNCTION_MODBUS_MASTER_READ_COILS, [slave_address, starting_address, count], 'C L S', 1, 'C'
705
+ end
706
+
707
+ # In Modbus slave mode this function can be used to answer a master request to
708
+ # read holding registers.
709
+ #
710
+ # * Request ID: Request ID of the corresponding request that is being answered.
711
+ # * Holding Registers: Data that is to be sent to the Modbus master for the corresponding request.
712
+ #
713
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_HOLDING_REGISTERS_REQUEST
714
+ # callback with the Request ID as provided by the argument of the callback.
715
+ def modbus_slave_answer_read_holding_registers_request_low_level(request_id, holding_registers_length, holding_registers_chunk_offset, holding_registers_chunk_data)
716
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_READ_HOLDING_REGISTERS_REQUEST_LOW_LEVEL, [request_id, holding_registers_length, holding_registers_chunk_offset, holding_registers_chunk_data], 'C S S S29', 0, ''
717
+ end
718
+
719
+ # In Modbus master mode this function can be used to read holding registers from a slave.
720
+ # This function creates a Modbus function code 3 request.
721
+ #
722
+ # * Slave Address: Address of the target Modbus slave.
723
+ # * Starting Address: Starting address of the read.
724
+ # * Count: Number of holding registers to read.
725
+ #
726
+ # Upon success the function will return a non-zero request ID which will represent
727
+ # the current request initiated by the Modbus master. In case of failure the returned
728
+ # request ID will be 0.
729
+ #
730
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_READ_HOLDING_REGISTERS_RESPONSE
731
+ # callback. In this callback the Request ID provided by the callback argument must be matched
732
+ # with the Request ID returned from this function to verify that the callback is indeed for a
733
+ # particular request.
734
+ def modbus_master_read_holding_registers(slave_address, starting_address, count)
735
+ send_request FUNCTION_MODBUS_MASTER_READ_HOLDING_REGISTERS, [slave_address, starting_address, count], 'C L S', 1, 'C'
736
+ end
737
+
738
+ # In Modbus slave mode this function can be used to answer a master request to
739
+ # write a single coil.
740
+ #
741
+ # * Request ID: Request ID of the corresponding request that is being answered.
742
+ #
743
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_COIL_REQUEST
744
+ # callback with the Request ID as provided by the arguments of the callback.
745
+ def modbus_slave_answer_write_single_coil_request(request_id)
746
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_COIL_REQUEST, [request_id], 'C', 0, ''
747
+ end
748
+
749
+ # In Modbus master mode this function can be used to write a single coil of a slave.
750
+ # This function creates a Modbus function code 5 request.
751
+ #
752
+ # * Slave Address: Address of the target Modbus slave.
753
+ # * Coil Address: Address of the coil.
754
+ # * Coil Value: Value to be written.
755
+ #
756
+ # Upon success the function will return a non-zero request ID which will represent
757
+ # the current request initiated by the Modbus master. In case of failure the returned
758
+ # request ID will be 0.
759
+ #
760
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_SINGLE_COIL_RESPONSE
761
+ # callback. In this callback the Request ID provided by the callback argument must be matched
762
+ # with the Request ID returned from this function to verify that the callback is indeed for a
763
+ # particular request.
764
+ def modbus_master_write_single_coil(slave_address, coil_address, coil_value)
765
+ send_request FUNCTION_MODBUS_MASTER_WRITE_SINGLE_COIL, [slave_address, coil_address, coil_value], 'C L ?', 1, 'C'
766
+ end
767
+
768
+ # In Modbus slave mode this function can be used to answer a master request to
769
+ # write a single register.
770
+ #
771
+ # * Request ID: Request ID of the corresponding request that is being answered.
772
+ #
773
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_WRITE_SINGLE_REGISTER_REQUEST
774
+ # callback with the Request ID, Register Address and Register Value as provided by
775
+ # the arguments of the callback.
776
+ def modbus_slave_answer_write_single_register_request(request_id)
777
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_SINGLE_REGISTER_REQUEST, [request_id], 'C', 0, ''
778
+ end
779
+
780
+ # In Modbus master mode this function can be used to write a single register of a
781
+ # slave. This function creates a Modbus function code 6 request.
782
+ #
783
+ # * Slave Address: Address of the target Modbus slave.
784
+ # * Register Address: Address of the register.
785
+ # * Register Value: Value to be written.
786
+ #
787
+ # Upon success the function will return a non-zero request ID which will represent
788
+ # the current request initiated by the Modbus master. In case of failure the returned
789
+ # request ID will be 0.
790
+ #
791
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_SINGLE_REGISTER_RESPONSE
792
+ # callback. In this callback the Request ID provided by the callback argument must be matched
793
+ # with the Request ID returned from this function to verify that the callback is indeed for a
794
+ # particular request.
795
+ def modbus_master_write_single_register(slave_address, register_address, register_value)
796
+ send_request FUNCTION_MODBUS_MASTER_WRITE_SINGLE_REGISTER, [slave_address, register_address, register_value], 'C L S', 1, 'C'
797
+ end
798
+
799
+ # In Modbus slave mode this function can be used to answer a master request to
800
+ # write multiple coils.
801
+ #
802
+ # * Request ID: Request ID of the corresponding request that is being answered.
803
+ #
804
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_COILS_REQUEST
805
+ # callback with the Request ID of the callback.
806
+ def modbus_slave_answer_write_multiple_coils_request(request_id)
807
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_COILS_REQUEST, [request_id], 'C', 0, ''
808
+ end
809
+
810
+ # In Modbus master mode this function can be used to write multiple coils of a slave.
811
+ # This function creates a Modbus function code 15 request.
812
+ #
813
+ # * Slave Address: Address of the target Modbus slave.
814
+ # * Starting Address: Starting address of the write.
815
+ #
816
+ # Upon success the function will return a non-zero request ID which will represent
817
+ # the current request initiated by the Modbus master. In case of failure the returned
818
+ # request ID will be 0.
819
+ #
820
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_COILS_RESPONSE
821
+ # callback. In this callback the Request ID provided by the callback argument must be matched
822
+ # with the Request ID returned from this function to verify that the callback is indeed for a
823
+ # particular request.
824
+ def modbus_master_write_multiple_coils_low_level(slave_address, starting_address, coils_length, coils_chunk_offset, coils_chunk_data)
825
+ send_request FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_COILS_LOW_LEVEL, [slave_address, starting_address, coils_length, coils_chunk_offset, coils_chunk_data], 'C L S S ?440', 1, 'C'
826
+ end
827
+
828
+ # In Modbus slave mode this function can be used to answer a master request to
829
+ # write multiple registers.
830
+ #
831
+ # * Request ID: Request ID of the corresponding request that is being answered.
832
+ #
833
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_WRITE_MULTIPLE_REGISTERS_REQUEST
834
+ # callback with the Request ID of the callback.
835
+ def modbus_slave_answer_write_multiple_registers_request(request_id)
836
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_WRITE_MULTIPLE_REGISTERS_REQUEST, [request_id], 'C', 0, ''
837
+ end
838
+
839
+ # In Modbus master mode this function can be used to write multiple registers of a slave.
840
+ # This function creates a Modbus function code 16 request.
841
+ #
842
+ # * Slave Address: Address of the target Modbus slave.
843
+ # * Starting Address: Starting Address of the write.
844
+ #
845
+ # Upon success the function will return a non-zero request ID which will represent
846
+ # the current request initiated by the Modbus master. In case of failure the returned
847
+ # request ID will be 0.
848
+ #
849
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_RESPONSE
850
+ # callback. In this callback the Request ID provided by the callback argument must be matched
851
+ # with the Request ID returned from this function to verify that the callback is indeed for a
852
+ # particular request.
853
+ def modbus_master_write_multiple_registers_low_level(slave_address, starting_address, registers_length, registers_chunk_offset, registers_chunk_data)
854
+ send_request FUNCTION_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_LOW_LEVEL, [slave_address, starting_address, registers_length, registers_chunk_offset, registers_chunk_data], 'C L S S S27', 1, 'C'
855
+ end
856
+
857
+ # In Modbus slave mode this function can be used to answer a master request to
858
+ # read discrete inputs.
859
+ #
860
+ # * Request ID: Request ID of the corresponding request that is being answered.
861
+ # * Discrete Inputs: Data that is to be sent to the Modbus master for the corresponding request.
862
+ #
863
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_DISCRETE_INPUTS_REQUEST
864
+ # callback with the Request ID as provided by the argument of the callback.
865
+ def modbus_slave_answer_read_discrete_inputs_request_low_level(request_id, discrete_inputs_length, discrete_inputs_chunk_offset, discrete_inputs_chunk_data)
866
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_READ_DISCRETE_INPUTS_REQUEST_LOW_LEVEL, [request_id, discrete_inputs_length, discrete_inputs_chunk_offset, discrete_inputs_chunk_data], 'C S S ?472', 0, ''
867
+ end
868
+
869
+ # In Modbus master mode this function can be used to read discrete inputs from a slave.
870
+ # This function creates a Modbus function code 2 request.
871
+ #
872
+ # * Slave Address: Address of the target Modbus slave.
873
+ # * Starting Address: Starting address of the read.
874
+ # * Count: Number of discrete inputs to read.
875
+ #
876
+ # Upon success the function will return a non-zero request ID which will represent
877
+ # the current request initiated by the Modbus master. In case of failure the returned
878
+ # request ID will be 0.
879
+ #
880
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_READ_DISCRETE_INPUTS_RESPONSE
881
+ # callback. In this callback the Request ID provided by the callback argument must be matched
882
+ # with the Request ID returned from this function to verify that the callback is indeed for a
883
+ # particular request.
884
+ def modbus_master_read_discrete_inputs(slave_address, starting_address, count)
885
+ send_request FUNCTION_MODBUS_MASTER_READ_DISCRETE_INPUTS, [slave_address, starting_address, count], 'C L S', 1, 'C'
886
+ end
887
+
888
+ # In Modbus slave mode this function can be used to answer a master request to
889
+ # read input registers.
890
+ #
891
+ # * Request ID: Request ID of the corresponding request that is being answered.
892
+ # * Input Registers: Data that is to be sent to the Modbus master for the corresponding request.
893
+ #
894
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_INPUT_REGISTERS_REQUEST callback
895
+ # with the Request ID as provided by the argument of the callback.
896
+ def modbus_slave_answer_read_input_registers_request_low_level(request_id, input_registers_length, input_registers_chunk_offset, input_registers_chunk_data)
897
+ send_request FUNCTION_MODBUS_SLAVE_ANSWER_READ_INPUT_REGISTERS_REQUEST_LOW_LEVEL, [request_id, input_registers_length, input_registers_chunk_offset, input_registers_chunk_data], 'C S S S29', 0, ''
898
+ end
899
+
900
+ # In Modbus master mode this function can be used to read input registers from a slave.
901
+ # This function creates a Modbus function code 4 request.
902
+ #
903
+ # * Slave Address: Address of the target Modbus slave.
904
+ # * Starting Address: Starting address of the read.
905
+ # * Count: Number of input registers to read.
906
+ #
907
+ # Upon success the function will return a non-zero request ID which will represent
908
+ # the current request initiated by the Modbus master. In case of failure the returned
909
+ # request ID will be 0.
910
+ #
911
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_READ_INPUT_REGISTERS_RESPONSE
912
+ # callback. In this callback the Request ID provided by the callback argument must be matched
913
+ # with the Request ID returned from this function to verify that the callback is indeed for a
914
+ # particular request.
915
+ def modbus_master_read_input_registers(slave_address, starting_address, count)
916
+ send_request FUNCTION_MODBUS_MASTER_READ_INPUT_REGISTERS, [slave_address, starting_address, count], 'C L S', 1, 'C'
917
+ end
918
+
919
+ # Returns the error count for the communication between Brick and Bricklet.
920
+ #
921
+ # The errors are divided into
922
+ #
923
+ # * ack checksum errors,
924
+ # * message checksum errors,
925
+ # * frameing errors and
926
+ # * overflow errors.
927
+ #
928
+ # The errors counts are for errors that occur on the Bricklet side. All
929
+ # Bricks have a similar function that returns the errors on the Brick side.
930
+ def get_spitfp_error_count
931
+ send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 16, 'L L L L'
932
+ end
933
+
934
+ # Sets the bootloader mode and returns the status after the requested
935
+ # mode change was instigated.
936
+ #
937
+ # You can change from bootloader mode to firmware mode and vice versa. A change
938
+ # from bootloader mode to firmware mode will only take place if the entry function,
939
+ # device identifier und crc are present and correct.
940
+ #
941
+ # This function is used by Brick Viewer during flashing. It should not be
942
+ # necessary to call it in a normal user program.
943
+ def set_bootloader_mode(mode)
944
+ send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 1, 'C'
945
+ end
946
+
947
+ # Returns the current bootloader mode, see BrickletRS485#set_bootloader_mode.
948
+ def get_bootloader_mode
949
+ send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 1, 'C'
950
+ end
951
+
952
+ # Sets the firmware pointer for func:`WriteFirmware`. The pointer has
953
+ # to be increased by chunks of size 64. The data is written to flash
954
+ # every 4 chunks (which equals to one page of size 256).
955
+ #
956
+ # This function is used by Brick Viewer during flashing. It should not be
957
+ # necessary to call it in a normal user program.
958
+ def set_write_firmware_pointer(pointer)
959
+ send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 0, ''
960
+ end
961
+
962
+ # Writes 64 Bytes of firmware at the position as written by
963
+ # BrickletRS485#set_write_firmware_pointer before. The firmware is written
964
+ # to flash every 4 chunks.
965
+ #
966
+ # You can only write firmware in bootloader mode.
967
+ #
968
+ # This function is used by Brick Viewer during flashing. It should not be
969
+ # necessary to call it in a normal user program.
970
+ def write_firmware(data)
971
+ send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 1, 'C'
972
+ end
973
+
974
+ # Sets the status LED configuration. By default the LED shows
975
+ # communication traffic between Brick and Bricklet, it flickers once
976
+ # for every 10 received data packets.
977
+ #
978
+ # You can also turn the LED permanently on/off or show a heartbeat.
979
+ #
980
+ # If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
981
+ def set_status_led_config(config)
982
+ send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 0, ''
983
+ end
984
+
985
+ # Returns the configuration as set by BrickletRS485#set_status_led_config
986
+ def get_status_led_config
987
+ send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 1, 'C'
988
+ end
989
+
990
+ # Returns the temperature in °C as measured inside the microcontroller. The
991
+ # value returned is not the ambient temperature!
992
+ #
993
+ # The temperature is only proportional to the real temperature and it has bad
994
+ # accuracy. Practically it is only useful as an indicator for
995
+ # temperature changes.
996
+ def get_chip_temperature
997
+ send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 2, 's'
998
+ end
999
+
1000
+ # Calling this function will reset the Bricklet. All configurations
1001
+ # will be lost.
1002
+ #
1003
+ # After a reset you have to create new device objects,
1004
+ # calling functions on the existing ones will result in
1005
+ # undefined behavior!
1006
+ def reset
1007
+ send_request FUNCTION_RESET, [], '', 0, ''
1008
+ end
1009
+
1010
+ # Writes a new UID into flash. If you want to set a new UID
1011
+ # you have to decode the Base58 encoded UID string into an
1012
+ # integer first.
1013
+ #
1014
+ # We recommend that you use Brick Viewer to change the UID.
1015
+ def write_uid(uid)
1016
+ send_request FUNCTION_WRITE_UID, [uid], 'L', 0, ''
1017
+ end
1018
+
1019
+ # Returns the current UID as an integer. Encode as
1020
+ # Base58 to get the usual string version.
1021
+ def read_uid
1022
+ send_request FUNCTION_READ_UID, [], '', 4, 'L'
1023
+ end
1024
+
1025
+ # Returns the UID, the UID where the Bricklet is connected to,
1026
+ # the position, the hardware and firmware version as well as the
1027
+ # device identifier.
1028
+ #
1029
+ # The position can be 'a', 'b', 'c' or 'd'.
1030
+ #
1031
+ # The device identifier numbers can be found :ref:`here <device_identifier>`.
1032
+ # |device_identifier_constant|
1033
+ def get_identity
1034
+ send_request FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S'
1035
+ end
1036
+
1037
+ # Writes characters to the RS485 interface. The characters can be binary data,
1038
+ # ASCII or similar is not necessary.
1039
+ #
1040
+ # The return value is the number of characters that were written.
1041
+ #
1042
+ # See BrickletRS485#set_rs485_configuration for configuration possibilities
1043
+ # regarding baudrate, parity and so on.
1044
+ def write(message)
1045
+ if message.length > 65535
1046
+ raise ArgumentError, 'Message can be at most 65535 items long'
1047
+ end
1048
+
1049
+ message_length = message.length
1050
+ message_chunk_offset = 0
1051
+
1052
+ if message_length == 0
1053
+ message_chunk_data = ['\0'] * 60
1054
+ ret = write_low_level message_length, message_chunk_offset, message_chunk_data
1055
+ message_written = ret
1056
+ else
1057
+ message_written = 0 # assigned in block
1058
+
1059
+ @stream_mutex.synchronize {
1060
+ while message_chunk_offset < message_length
1061
+ message_chunk_data = message[message_chunk_offset, 60]
1062
+
1063
+ if message_chunk_data.length < 60
1064
+ message_chunk_data += ['\0'] * (60 - message_chunk_data.length)
1065
+ end
1066
+
1067
+ ret = write_low_level message_length, message_chunk_offset, message_chunk_data
1068
+ message_written += ret
1069
+
1070
+ if ret < 60
1071
+ break # either last chunk or short write
1072
+ end
1073
+
1074
+ message_chunk_offset += 60
1075
+ end
1076
+ }
1077
+ end
1078
+
1079
+ message_written
1080
+ end
1081
+
1082
+ # Returns up to *length* characters from receive buffer.
1083
+ #
1084
+ # Instead of polling with this function, you can also use
1085
+ # callbacks. But note that this function will return available
1086
+ # data only when the read callback is disabled.
1087
+ # See BrickletRS485#enable_read_callback and CALLBACK_READ callback.
1088
+ def read(length)
1089
+ message_length = nil # assigned in block
1090
+ message_data = nil # assigned in block
1091
+
1092
+ @stream_mutex.synchronize {
1093
+ ret = read_low_level length
1094
+ message_length = ret[0]
1095
+ message_chunk_offset = ret[1]
1096
+ message_out_of_sync = message_chunk_offset != 0
1097
+ message_data = ret[2]
1098
+
1099
+ while not message_out_of_sync and message_data.length < message_length
1100
+ ret = read_low_level length
1101
+ message_length = ret[0]
1102
+ message_chunk_offset = ret[1]
1103
+ message_out_of_sync = message_chunk_offset != message_data.length
1104
+ message_data += ret[2]
1105
+ end
1106
+
1107
+ if message_out_of_sync # discard remaining stream to bring it back in-sync
1108
+ while message_chunk_offset + 60 < message_length
1109
+ ret = read_low_level length
1110
+ message_length = ret[0]
1111
+ message_chunk_offset = ret[1]
1112
+ end
1113
+
1114
+ raise StreamOutOfSyncException, 'Message stream is out-of-sync'
1115
+ end
1116
+ }
1117
+
1118
+ message_data[0, message_length]
1119
+ end
1120
+
1121
+ # In Modbus slave mode this function can be used to answer a master request to
1122
+ # read coils.
1123
+ #
1124
+ # * Request ID: Request ID of the corresponding request that is being answered.
1125
+ # * Coils: Data that is to be sent to the Modbus master for the corresponding request.
1126
+ #
1127
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_COILS_REQUEST callback
1128
+ # with the Request ID as provided by the argument of the callback.
1129
+ def modbus_slave_answer_read_coils_request(request_id, coils)
1130
+ if coils.length > 65535
1131
+ raise ArgumentError, 'Coils can be at most 65535 items long'
1132
+ end
1133
+
1134
+ coils_length = coils.length
1135
+ coils_chunk_offset = 0
1136
+
1137
+ if coils_length == 0
1138
+ coils_chunk_data = [false] * 472
1139
+ ret = modbus_slave_answer_read_coils_request_low_level request_id, coils_length, coils_chunk_offset, coils_chunk_data
1140
+ else
1141
+ ret = nil # assigned in block
1142
+
1143
+ @stream_mutex.synchronize {
1144
+ while coils_chunk_offset < coils_length
1145
+ coils_chunk_data = coils[coils_chunk_offset, 472]
1146
+
1147
+ if coils_chunk_data.length < 472
1148
+ coils_chunk_data += [false] * (472 - coils_chunk_data.length)
1149
+ end
1150
+
1151
+ ret = modbus_slave_answer_read_coils_request_low_level request_id, coils_length, coils_chunk_offset, coils_chunk_data
1152
+ coils_chunk_offset += 472
1153
+ end
1154
+ }
1155
+ end
1156
+
1157
+ ret
1158
+ end
1159
+
1160
+ # In Modbus slave mode this function can be used to answer a master request to
1161
+ # read holding registers.
1162
+ #
1163
+ # * Request ID: Request ID of the corresponding request that is being answered.
1164
+ # * Holding Registers: Data that is to be sent to the Modbus master for the corresponding request.
1165
+ #
1166
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_HOLDING_REGISTERS_REQUEST
1167
+ # callback with the Request ID as provided by the argument of the callback.
1168
+ def modbus_slave_answer_read_holding_registers_request(request_id, holding_registers)
1169
+ if holding_registers.length > 65535
1170
+ raise ArgumentError, 'Holding Registers can be at most 65535 items long'
1171
+ end
1172
+
1173
+ holding_registers_length = holding_registers.length
1174
+ holding_registers_chunk_offset = 0
1175
+
1176
+ if holding_registers_length == 0
1177
+ holding_registers_chunk_data = [0] * 29
1178
+ ret = modbus_slave_answer_read_holding_registers_request_low_level request_id, holding_registers_length, holding_registers_chunk_offset, holding_registers_chunk_data
1179
+ else
1180
+ ret = nil # assigned in block
1181
+
1182
+ @stream_mutex.synchronize {
1183
+ while holding_registers_chunk_offset < holding_registers_length
1184
+ holding_registers_chunk_data = holding_registers[holding_registers_chunk_offset, 29]
1185
+
1186
+ if holding_registers_chunk_data.length < 29
1187
+ holding_registers_chunk_data += [0] * (29 - holding_registers_chunk_data.length)
1188
+ end
1189
+
1190
+ ret = modbus_slave_answer_read_holding_registers_request_low_level request_id, holding_registers_length, holding_registers_chunk_offset, holding_registers_chunk_data
1191
+ holding_registers_chunk_offset += 29
1192
+ end
1193
+ }
1194
+ end
1195
+
1196
+ ret
1197
+ end
1198
+
1199
+ # In Modbus master mode this function can be used to write multiple coils of a slave.
1200
+ # This function creates a Modbus function code 15 request.
1201
+ #
1202
+ # * Slave Address: Address of the target Modbus slave.
1203
+ # * Starting Address: Starting address of the write.
1204
+ #
1205
+ # Upon success the function will return a non-zero request ID which will represent
1206
+ # the current request initiated by the Modbus master. In case of failure the returned
1207
+ # request ID will be 0.
1208
+ #
1209
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_COILS_RESPONSE
1210
+ # callback. In this callback the Request ID provided by the callback argument must be matched
1211
+ # with the Request ID returned from this function to verify that the callback is indeed for a
1212
+ # particular request.
1213
+ def modbus_master_write_multiple_coils(slave_address, starting_address, coils)
1214
+ if coils.length > 65535
1215
+ raise ArgumentError, 'Coils can be at most 65535 items long'
1216
+ end
1217
+
1218
+ coils_length = coils.length
1219
+ coils_chunk_offset = 0
1220
+
1221
+ if coils_length == 0
1222
+ coils_chunk_data = [false] * 440
1223
+ ret = modbus_master_write_multiple_coils_low_level slave_address, starting_address, coils_length, coils_chunk_offset, coils_chunk_data
1224
+ else
1225
+ ret = nil # assigned in block
1226
+
1227
+ @stream_mutex.synchronize {
1228
+ while coils_chunk_offset < coils_length
1229
+ coils_chunk_data = coils[coils_chunk_offset, 440]
1230
+
1231
+ if coils_chunk_data.length < 440
1232
+ coils_chunk_data += [false] * (440 - coils_chunk_data.length)
1233
+ end
1234
+
1235
+ ret = modbus_master_write_multiple_coils_low_level slave_address, starting_address, coils_length, coils_chunk_offset, coils_chunk_data
1236
+ coils_chunk_offset += 440
1237
+ end
1238
+ }
1239
+ end
1240
+
1241
+ ret
1242
+ end
1243
+
1244
+ # In Modbus master mode this function can be used to write multiple registers of a slave.
1245
+ # This function creates a Modbus function code 16 request.
1246
+ #
1247
+ # * Slave Address: Address of the target Modbus slave.
1248
+ # * Starting Address: Starting Address of the write.
1249
+ #
1250
+ # Upon success the function will return a non-zero request ID which will represent
1251
+ # the current request initiated by the Modbus master. In case of failure the returned
1252
+ # request ID will be 0.
1253
+ #
1254
+ # When successful this function will also invoke the CALLBACK_MODBUS_MASTER_WRITE_MULTIPLE_REGISTERS_RESPONSE
1255
+ # callback. In this callback the Request ID provided by the callback argument must be matched
1256
+ # with the Request ID returned from this function to verify that the callback is indeed for a
1257
+ # particular request.
1258
+ def modbus_master_write_multiple_registers(slave_address, starting_address, registers)
1259
+ if registers.length > 65535
1260
+ raise ArgumentError, 'Registers can be at most 65535 items long'
1261
+ end
1262
+
1263
+ registers_length = registers.length
1264
+ registers_chunk_offset = 0
1265
+
1266
+ if registers_length == 0
1267
+ registers_chunk_data = [0] * 27
1268
+ ret = modbus_master_write_multiple_registers_low_level slave_address, starting_address, registers_length, registers_chunk_offset, registers_chunk_data
1269
+ else
1270
+ ret = nil # assigned in block
1271
+
1272
+ @stream_mutex.synchronize {
1273
+ while registers_chunk_offset < registers_length
1274
+ registers_chunk_data = registers[registers_chunk_offset, 27]
1275
+
1276
+ if registers_chunk_data.length < 27
1277
+ registers_chunk_data += [0] * (27 - registers_chunk_data.length)
1278
+ end
1279
+
1280
+ ret = modbus_master_write_multiple_registers_low_level slave_address, starting_address, registers_length, registers_chunk_offset, registers_chunk_data
1281
+ registers_chunk_offset += 27
1282
+ end
1283
+ }
1284
+ end
1285
+
1286
+ ret
1287
+ end
1288
+
1289
+ # In Modbus slave mode this function can be used to answer a master request to
1290
+ # read discrete inputs.
1291
+ #
1292
+ # * Request ID: Request ID of the corresponding request that is being answered.
1293
+ # * Discrete Inputs: Data that is to be sent to the Modbus master for the corresponding request.
1294
+ #
1295
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_DISCRETE_INPUTS_REQUEST
1296
+ # callback with the Request ID as provided by the argument of the callback.
1297
+ def modbus_slave_answer_read_discrete_inputs_request(request_id, discrete_inputs)
1298
+ if discrete_inputs.length > 65535
1299
+ raise ArgumentError, 'Discrete Inputs can be at most 65535 items long'
1300
+ end
1301
+
1302
+ discrete_inputs_length = discrete_inputs.length
1303
+ discrete_inputs_chunk_offset = 0
1304
+
1305
+ if discrete_inputs_length == 0
1306
+ discrete_inputs_chunk_data = [false] * 472
1307
+ ret = modbus_slave_answer_read_discrete_inputs_request_low_level request_id, discrete_inputs_length, discrete_inputs_chunk_offset, discrete_inputs_chunk_data
1308
+ else
1309
+ ret = nil # assigned in block
1310
+
1311
+ @stream_mutex.synchronize {
1312
+ while discrete_inputs_chunk_offset < discrete_inputs_length
1313
+ discrete_inputs_chunk_data = discrete_inputs[discrete_inputs_chunk_offset, 472]
1314
+
1315
+ if discrete_inputs_chunk_data.length < 472
1316
+ discrete_inputs_chunk_data += [false] * (472 - discrete_inputs_chunk_data.length)
1317
+ end
1318
+
1319
+ ret = modbus_slave_answer_read_discrete_inputs_request_low_level request_id, discrete_inputs_length, discrete_inputs_chunk_offset, discrete_inputs_chunk_data
1320
+ discrete_inputs_chunk_offset += 472
1321
+ end
1322
+ }
1323
+ end
1324
+
1325
+ ret
1326
+ end
1327
+
1328
+ # In Modbus slave mode this function can be used to answer a master request to
1329
+ # read input registers.
1330
+ #
1331
+ # * Request ID: Request ID of the corresponding request that is being answered.
1332
+ # * Input Registers: Data that is to be sent to the Modbus master for the corresponding request.
1333
+ #
1334
+ # This function must be called from the CALLBACK_MODBUS_SLAVE_READ_INPUT_REGISTERS_REQUEST callback
1335
+ # with the Request ID as provided by the argument of the callback.
1336
+ def modbus_slave_answer_read_input_registers_request(request_id, input_registers)
1337
+ if input_registers.length > 65535
1338
+ raise ArgumentError, 'Input Registers can be at most 65535 items long'
1339
+ end
1340
+
1341
+ input_registers_length = input_registers.length
1342
+ input_registers_chunk_offset = 0
1343
+
1344
+ if input_registers_length == 0
1345
+ input_registers_chunk_data = [0] * 29
1346
+ ret = modbus_slave_answer_read_input_registers_request_low_level request_id, input_registers_length, input_registers_chunk_offset, input_registers_chunk_data
1347
+ else
1348
+ ret = nil # assigned in block
1349
+
1350
+ @stream_mutex.synchronize {
1351
+ while input_registers_chunk_offset < input_registers_length
1352
+ input_registers_chunk_data = input_registers[input_registers_chunk_offset, 29]
1353
+
1354
+ if input_registers_chunk_data.length < 29
1355
+ input_registers_chunk_data += [0] * (29 - input_registers_chunk_data.length)
1356
+ end
1357
+
1358
+ ret = modbus_slave_answer_read_input_registers_request_low_level request_id, input_registers_length, input_registers_chunk_offset, input_registers_chunk_data
1359
+ input_registers_chunk_offset += 29
1360
+ end
1361
+ }
1362
+ end
1363
+
1364
+ ret
1365
+ end
1366
+
1367
+ # Registers a callback with ID <tt>id</tt> to the block <tt>block</tt>.
1368
+ def register_callback(id, &block)
1369
+ callback = block
1370
+ @registered_callbacks[id] = callback
1371
+ end
1372
+ end
1373
+ end