You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/en/master_api_overview.rst
+50-32
Original file line number
Diff line number
Diff line change
@@ -313,38 +313,56 @@ Master Customize Function Handlers
313
313
314
314
The Master object contains the command handling tables to define the specific handling functionality for each supported Modbus command. The default handling functions in this table support most common Modbus commands. However, the list of commands can be extended by adding the new command into handling table with its custom handling behavior. It is also possible overriding the function handler for the specific command. The below described API functions allow using this behavior for master objects.
315
315
316
-
:cpp:func:`mbc_master_set_handler`
316
+
:cpp:func:`mbc_set_handler`
317
317
318
-
:cpp:func:`mbc_master_get_handler`
318
+
The function adds new handler for the function or overrides the existing handler for the function.
319
319
320
-
The example code to overide the handler routine for command `0x04 - Read Input Registers`:
320
+
:cpp:func:`mbc_get_handler`
321
+
322
+
The function returns the handler for the specified function code from handling table. Allows to keep and use the predefined handlers for standard functions.
323
+
324
+
:cpp:func:`mbc_delete_handler`
325
+
326
+
The function allows to delete the handler for specified command and free the handler table entry for this.
327
+
328
+
:cpp:func:`mbc_get_handler_count`
329
+
330
+
The function returns the actual number of command handlers registered for the object reffered by parameter.
331
+
332
+
The example code to override the handler routine for the command `<0x04 - Read Input Registers>` is below. This example allows to perform a custom action and then calls the standard handler, which maps the device data to the command buffer from the actual parameter. This is just recommended behavior for handling functions, but users can change the order of the calls if absolutely required. Please refer to the existing handler :cpp:func:`mbm_fn_read_inp_reg` for more information.
"could not override handler, returned (0x%x).", (int)err);
346
364
347
-
.. note:: The custom handler set by the function :cpp:func:`mbc_master_set_handler` should be as short as possible and should contain simple and safe logic to not break the normal functionality of the stack. This is user application responsibility to handle the command appropriately.
365
+
.. note:: The custom handler set by the function :cpp:func:`mbc_set_handler` should be as short as possible and should contain simple and safe logic to not break the normal functionality of the stack. This is user application responsibility to handle the command appropriately.
348
366
349
367
The example code to handle custom vendor specific command is below. This example sends the 'Master' string to slave and gets the response from slave with the string being appended from slave. It is just a simple echo example to demonstrate the approach.
350
368
@@ -355,33 +373,29 @@ The example code to handle custom vendor specific command is below. This example
Copy file name to clipboardexpand all lines: docs/en/slave_api_overview.rst
+41-21
Original file line number
Diff line number
Diff line change
@@ -167,46 +167,66 @@ Example to get the actual slave identificator:
167
167
Slave Customize Function Handlers
168
168
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
169
169
170
-
The Slave object contains the command handling tables to define the specific handling functionality for each supported Modbus command. The default handling functions in this table support most useful Modbus commands. However, the list of commands can be extended by adding the new command into handling table with its custom handling behavior. It is also possible overriding the function handler for the specific command. The below described API functions allow using this behavior slave objects.
170
+
The Slave object contains the command handling tables to define the specific handling functionality for each supported Modbus command. The default handling functions in this table support most useful Modbus commands. However, the list of commands can be extended by adding the new command into handling table with its custom handling behavior. It is also possible overriding the function handler for the specific command. The below described API functions allow using this behavior for slave objects.
171
171
172
-
:cpp:func:`mbc_slave_set_handler`
172
+
:cpp:func:`mbc_set_handler`
173
173
174
-
:cpp:func:`mbc_slave_get_handler`
174
+
The function adds new handler for the function or overrides the existing handler for the function.
175
175
176
-
The following example allows to override the standard command to read input registers. Refer to examples for more information on how to handle custom commands.
176
+
:cpp:func:`mbc_get_handler`
177
+
178
+
The function returns the handler for the specified function code from handling table. Allows to keep and use the predefined handlers for standard functions.
179
+
180
+
:cpp:func:`mbc_delete_handler`
181
+
182
+
The function allows to delete the handler for specified command and free the handler table entry for this.
183
+
184
+
:cpp:func:`mbc_get_handler_count`
185
+
186
+
The function returns the actual number of command handlers registered for the object reffered by parameter.
187
+
188
+
The following example allows to override the standard command to read input registers. Refer to standard handler function :cpp:func:`mbs_fn_read_input_reg` for more information on how to handle custom commands.
177
189
178
190
.. code:: c
179
191
180
192
static void *slave_handle = NULL; // Pointer to allocated interface structure (must be actual)
193
+
mb_fn_handler_fp pstandard_handler = NULL;
181
194
....
182
-
// The custom function handler for the function returns exception code for now
183
-
// Please place your custom handling behavior here.
184
-
// This error handler will be executed to check the request for the command 0x04
185
-
// See the default handler in the file `esp-modbus//modbus/mb_objects/functions/mbfuncinput_slave.c` for more information.
186
-
// The pframe is pointer to command buffer, plen - is pointer to the variable with the length of the buffer
195
+
// This is the custom function handler for the command.
196
+
// The handler is executed from the context of modbus controller event task and should be as simple as possible.
197
+
// Parameters: frame_ptr - the pointer to the incoming ADU request frame from master starting from function code,
198
+
// plen - the pointer to length of the frame. The handler body can override the buffer and return the length of data.
199
+
// After return from the handler the modbus object will handle the end of transaction according to the exception returned,
200
+
// then builds the response frame and send it back to the master. If the whole transaction time including the response
201
+
// latency exceeds the configured slave response time set in the master configuration the master will ignore the transaction.
"could not get handler, returned (0x%x).", (int)err);
208
226
209
-
.. note:: The custom handlers set by the function :cpp:func:`mbc_slave_set_handler` should be as short as possible and should contain simple logic to not break the normal functionality of the stack. This is user application responsibility to handle the command appropriately.
227
+
Refer to :ref:`example Serial slave <example_mb_slave>` for more information.
228
+
229
+
.. note:: The custom handlers set by the function :cpp:func:`mbc_set_handler` should be as short as possible, contain simple and safe logic and avoid blocking calls to not break the normal functionality of the stack. The possible latency in this handler may prevent to respond properly to the master request which waits for response during the slave response time configured in the configuration structure. If the slave does not respond to the master during the slave response time the master will report timeout failure and ignores the late response. This is user application responsibility to handle the command appropriately.
0 commit comments