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
+100-3Lines changed: 100 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,10 @@ The following overview describes how to setup Modbus master communication. The o
7
7
8
8
1. :ref:`modbus_api_port_initialization` - Initialization of Modbus controller interface for the selected port.
9
9
2. :ref:`modbus_api_master_configure_descriptor` - Configure data descriptors to access slave parameters.
10
-
3. :ref:`modbus_api_master_setup_communication_options` - Allows to setup communication options for selected port.
11
-
4. :ref:`modbus_api_master_start_communication` - Start stack and sending / receiving data.
12
-
5. :ref:`modbus_api_master_destroy` - Destroy Modbus controller and its resources.
10
+
3. :ref:`modbus_api_master_handler_customization` - Customization of Modbus function handling.
11
+
4. :ref:`modbus_api_master_setup_communication_options` - Allows to setup communication options for selected port.
12
+
5. :ref:`modbus_api_master_start_communication` - Start stack and sending / receiving data.
13
+
6. :ref:`modbus_api_master_destroy` - Destroy Modbus controller and its resources.
13
14
14
15
.. _modbus_api_master_configure_descriptor:
15
16
@@ -305,6 +306,102 @@ Initialization of master descriptor. The descriptor represents an array of type
305
306
The Data Dictionary can be initialized from SD card, MQTT or other source before start of stack. Once the initialization and setup is done, the Modbus controller allows the reading of complex parameters from any slave included in descriptor table using its CID.
306
307
Refer to :ref:`example TCP master <example_mb_tcp_master>`, :ref:`example Serial master <example_mb_master>` for more information.
307
308
309
+
.. _modbus_api_master_handler_customization:
310
+
311
+
Master Customize Function Handlers
312
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
313
+
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
+
316
+
:cpp:func:`mbc_master_set_handler`
317
+
318
+
:cpp:func:`mbc_master_get_handler`
319
+
320
+
The example code to overide the handler routine for command `0x04 - Read Input Registers`:
"could not override handler, returned (0x%x).", (int)err);
346
+
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.
348
+
349
+
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
+
351
+
.. code:: c
352
+
353
+
#define MB_CUST_DATA_LEN 100
354
+
static char my_custom_data[MB_CUST_DATA_LEN] = {0}; // custom data buffer for the request
Copy file name to clipboardExpand all lines: docs/en/overview_messaging_and_mapping.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -276,7 +276,7 @@ The below diagrams show how the extended data types appear on network layer.
276
276
277
277
The approach showed above can be used to pack the data into MBAP frames used by Modbus TCP as well as for other types with similar size.
278
278
279
-
The following sections give an overview of how to use the ESP_Modbus component found under `components/freemodbus`. The sections cover initialization of a Modbus port, and the setup a master or slave device accordingly:
279
+
The following sections give an overview of how to use the ESP_Modbus component found under `components/esp-modbus`. The sections cover initialization of a Modbus port, and the setup a master or slave device accordingly:
Copy file name to clipboardExpand all lines: docs/en/slave_api_overview.rst
+50-3Lines changed: 50 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,10 @@ The sections below represent typical programming workflow for the slave API whic
7
7
8
8
1. :ref:`modbus_api_port_initialization` - Initialization of Modbus controller interface using communication options.
9
9
2. :ref:`modbus_api_slave_configure_descriptor` - Configure data descriptors to access slave parameters.
10
-
3. :ref:`modbus_api_slave_setup_communication_options` - Allows to setup communication options for selected port.
11
-
4. :ref:`modbus_api_slave_communication` - Start stack and sending / receiving data. Filter events when master accesses the register areas.
12
-
5. :ref:`modbus_api_slave_destroy` - Destroy Modbus controller and its resources.
10
+
3. :ref:`modbus_api_slave_handler_customization` - Customization of Modbus function handling in slave object.
11
+
4. :ref:`modbus_api_slave_setup_communication_options` - Allows to setup communication options for selected port.
12
+
5. :ref:`modbus_api_slave_communication` - Start stack and sending / receiving data. Filter events when master accesses the register areas.
13
+
6. :ref:`modbus_api_slave_destroy` - Destroy Modbus controller and its resources.
13
14
14
15
.. _modbus_api_slave_configure_descriptor:
15
16
@@ -161,6 +162,52 @@ Example to get the actual slave identificator:
161
162
}
162
163
...
163
164
165
+
.. _modbus_api_slave_handler_customization:
166
+
167
+
Slave Customize Function Handlers
168
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.
171
+
172
+
:cpp:func:`mbc_slave_set_handler`
173
+
174
+
:cpp:func:`mbc_slave_get_handler`
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.
177
+
178
+
.. code:: c
179
+
180
+
static void *slave_handle = NULL; // Pointer to allocated interface structure (must be actual)
181
+
....
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
"could not get handler, returned (0x%x).", (int)err);
208
+
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.
0 commit comments