Skip to content

Commit d4af0e5

Browse files
committed
update examples
1 parent fc8fc06 commit d4af0e5

File tree

6 files changed

+169
-24
lines changed

6 files changed

+169
-24
lines changed

examples/serial/mb_serial_master/main/serial_master.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#define EACH_ITEM(array, length) \
5959
(typeof(*(array)) *pitem = (array); (pitem < &((array)[length])); pitem++)
6060

61+
#define MB_CUST_DATA_LEN 100 // The length of custom command buffer
62+
6163
static const char *TAG = "MASTER_TEST";
6264

6365
// Enumeration of modbus device addresses accessed by master device
@@ -217,6 +219,8 @@ const mb_parameter_descriptor_t device_parameters[] = {
217219
// Calculate number of parameters in the table
218220
const uint16_t num_device_parameters = (sizeof(device_parameters)/sizeof(device_parameters[0]));
219221

222+
static char my_custom_data[MB_CUST_DATA_LEN] = {0}; // the custom data buffer
223+
220224
static void *master_handle = NULL;
221225

222226
// The function to get pointer to parameter storage (instance) according to parameter description table
@@ -301,6 +305,19 @@ static void *master_get_param_data(const mb_parameter_descriptor_t *param_descri
301305
} \
302306
))
303307

308+
mb_exception_t my_custom_handler(void *, uint8_t *frame_ptr, uint16_t *plen)
309+
{
310+
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen && *plen < (MB_CUST_DATA_LEN - 1)), MB_EX_CRITICAL, TAG,
311+
"incorrect custom frame buffer");
312+
ESP_LOGW(TAG, "Custom handler, Frame ptr: %p, len: %u", frame_ptr, *plen);
313+
// This error handler will be executed to handle the request for the registered custom command
314+
// Refer the handler functions in `esp-modbus/modbus/mb_objects/functions/mbfuncinput_master.c` for more information.
315+
// Parameters: pframe: is pointer to incoming frame buffer, plen: is pointer to length including the function code
316+
strncpy((char *)&my_custom_data[0], (char *)&frame_ptr[1], MB_CUST_DATA_LEN);
317+
ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", &my_custom_data[0], (*plen - 1), ESP_LOG_WARN);
318+
return MB_EX_NONE;
319+
}
320+
304321
// User operation function to read slave values and check alarm
305322
static void master_operation_func(void *arg)
306323
{
@@ -309,6 +326,20 @@ static void master_operation_func(void *arg)
309326
const mb_parameter_descriptor_t *param_descriptor = NULL;
310327

311328
ESP_LOGI(TAG, "Start modbus test...");
329+
330+
char *pcustom_string = "Master";
331+
mb_param_request_t req = {
332+
.slave_addr = MB_DEVICE_ADDR1, // the slave UID to send the request
333+
.command = 0x41, // the custom function code,
334+
.reg_start = 0, // unused,
335+
.reg_size = (strlen(pcustom_string) >> 1) // length of the data to send (registers)
336+
};
337+
338+
// Send the request with custom command (vendor speciic)
339+
err = mbc_master_send_request(master_handle, &req, pcustom_string);
340+
if (err != ESP_OK) {
341+
ESP_LOGE("CUSTOM_DATA", "Send custom request fail.");
342+
}
312343

313344
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
314345
// Command - 17 (0x11) Report Slave ID
@@ -318,11 +349,11 @@ static void master_operation_func(void *arg)
318349
// The returned slave info data will be stored into the `info_buf`.
319350
// Request fields: slave_addr - the UID of slave, reg_start - not used,
320351
// reg_size = max size of buffer (registers).
321-
mb_param_request_t req = {
322-
.slave_addr = MB_DEVICE_ADDR1, // slave UID to retrieve ID
323-
.command = 0x11, // the <Report Slave ID> command,
324-
.reg_start = 0, // must be zero,
325-
.reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1) // the expected length of buffer in registers
352+
req = {
353+
.slave_addr = MB_DEVICE_ADDR1, // slave UID to retrieve ID
354+
.command = 0x11, // the <Report Slave ID> command,
355+
.reg_start = 0, // must be zero,
356+
.reg_size = (CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE >> 1) // the expected length of buffer in registers
326357
};
327358

328359
uint8_t info_buf[CONFIG_FMB_CONTROLLER_SLAVE_ID_MAX_SIZE] = {0}; // The buffer to save slave ID
@@ -492,6 +523,18 @@ static esp_err_t master_init(void)
492523
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
493524
"mb controller initialization fail, returns(0x%x).", (int)err);
494525

526+
uint8_t override_command = 0x41;
527+
err = mbc_master_set_handler(master_handle, override_command, NULL);
528+
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ESP_ERR_INVALID_STATE, TAG,
529+
"could not override handler, returned (0x%x).", (int)err);
530+
err = mbc_master_set_handler(master_handle, override_command, my_custom_handler);
531+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
532+
"could not override handler, returned (0x%x).", (int)err);
533+
mb_fn_handler_fp phandler = NULL;
534+
err = mbc_master_get_handler(master_handle, override_command, &phandler);
535+
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_handler), ESP_ERR_INVALID_STATE, TAG,
536+
"could not get handler for command %d, returned (0x%x).", (int)override_command, (int)err);
537+
495538
// Set UART pin numbers
496539
err = uart_set_pin(MB_PORT_NUM, CONFIG_MB_UART_TXD, CONFIG_MB_UART_RXD,
497540
CONFIG_MB_UART_RTS, UART_PIN_NO_CHANGE);

examples/serial/mb_serial_slave/main/serial_slave.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
#define MB_WRITE_MASK (MB_EVENT_HOLDING_REG_WR \
4444
| MB_EVENT_COILS_WR)
4545
#define MB_READ_WRITE_MASK (MB_READ_MASK | MB_WRITE_MASK)
46-
#define MB_TEST_VALUE 12345.0
46+
#define MB_TEST_VALUE (12345.0)
47+
#define MB_CUST_DATA_MAX_LEN (100)
4748

4849
static const char *TAG = "SLAVE_TEST";
4950

@@ -144,6 +145,22 @@ static void setup_reg_data(void)
144145
input_reg_params.input_data7 = 4.78;
145146
}
146147

148+
mb_exception_t my_custom_fc_handler(void *pinst, uint8_t *frame_ptr, uint16_t *plen)
149+
{
150+
char *str_append = ":Slave";
151+
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen < (MB_CUST_DATA_MAX_LEN - strlen(str_append))), MB_EX_ILLEGAL_DATA_VALUE, TAG,
152+
"incorrect custom frame");
153+
//ESP_LOGW("CUSTOM_DATA", "Custom handler, frame ptr: %p, len: %u", frame_ptr, *plen);
154+
//ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", frame_ptr, *plen, ESP_LOG_WARN);
155+
// This command handler will be executed to check the request for the custom command
156+
// See the `esp-modbus/modbus/mb_objects/functions/functions/mbfuncinput.c` for more information
157+
// pframe is pointer to the buffer starting from function code, plen - is pointer to length of the data
158+
frame_ptr[*plen] = '\0';
159+
strcat((char *)&frame_ptr[1], str_append);
160+
*plen = (strlen(str_append) + *plen); // the length of (response + command)
161+
return MB_EX_NONE; // Set the exception code for slave appropriately
162+
}
163+
147164
// An example application of Modbus slave. It is based on esp-modbus stack.
148165
// See deviceparams.h file for more information about assigned Modbus parameters.
149166
// These parameters can be accessed from main application and also can be changed
@@ -173,6 +190,18 @@ void app_main(void)
173190

174191
ESP_ERROR_CHECK(mbc_slave_create_serial(&comm_config, &mbc_slave_handle)); // Initialization of Modbus controller
175192

193+
uint8_t custom_command = 0x41; // The custom command to be sent to slave
194+
esp_err_t err = mbc_slave_set_handler(mbc_slave_handle, custom_command, NULL);
195+
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ;, TAG,
196+
"could not reset handler, returned (0x%x).", (int)err);
197+
err = mbc_slave_set_handler(mbc_slave_handle, custom_command, my_custom_fc_handler);
198+
MB_RETURN_ON_FALSE((err == ESP_OK), ;, TAG,
199+
"could not set or override handler, returned (0x%x).", (int)err);
200+
mb_fn_handler_fp phandler = NULL;
201+
err = mbc_slave_get_handler(mbc_slave_handle, custom_command, &phandler);
202+
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_fc_handler), ;, TAG,
203+
"could not get handler for command %d, returned (0x%x).", (int)custom_command, (int)err);
204+
176205
// The code below initializes Modbus register area descriptors
177206
// for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
178207
// Initialization should be done for each supported Modbus register area according to register map.
@@ -243,7 +272,7 @@ void app_main(void)
243272
ESP_ERROR_CHECK(uart_set_mode(MB_PORT_NUM, UART_MODE_RS485_HALF_DUPLEX));
244273

245274
// Starts of modbus controller and stack
246-
esp_err_t err = mbc_slave_start(mbc_slave_handle);
275+
err = mbc_slave_start(mbc_slave_handle);
247276

248277
#if CONFIG_FMB_CONTROLLER_SLAVE_ID_SUPPORT
249278
// Initialize the new slave identificator structure (example)

examples/serial/pytest_mb_master_slave.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
pattern_dict_slave = {Stages.STACK_IPV4: (r'I \([0-9]+\) example_connect: - IPv4 address: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'),
1616
Stages.STACK_IPV6: (r'I \([0-9]+\) example_connect: - IPv6 address: (([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})'),
17-
Stages.STACK_INIT: (r'I \(([0-9]+)\) MB_TCP_SLAVE_PORT: (Protocol stack initialized).'),
18-
Stages.STACK_CONNECT: (r'I\s\(([0-9]+)\) MB_TCP_SLAVE_PORT: Socket \(#[0-9]+\), accept client connection from address: '
17+
Stages.STACK_INIT: (r'I \(([0-9]+)\) SLAVE_TEST: (Modbus slave stack initialized)'),
18+
Stages.STACK_CONNECT: (r'I\s\(([0-9]+)\) SLAVE_TEST: Socket \(#[0-9]+\), accept client connection from address: '
1919
r'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'),
2020
Stages.STACK_START: (r'I\s\(([0-9]+)\) SLAVE_TEST: (Start modbus test)'),
2121
Stages.STACK_PAR_OK: (r'I\s\(([0-9]+)\) SLAVE_TEST: ([A-Z]+ [A-Z]+) \([a-zA-Z0-9_]+ us\),\s'
@@ -48,16 +48,16 @@
4848
@pytest.mark.parametrize('config', test_configs, indirect=True)
4949
@pytest.mark.parametrize(
5050
'count, app_path', [
51-
(2, f'{os.path.join(os.path.dirname(__file__), "mb_serial_master")}|{os.path.join(os.path.dirname(__file__), "mb_serial_slave")}')
51+
(2, f'{os.path.join(os.path.dirname(__file__), "mb_serial_slave")}|{os.path.join(os.path.dirname(__file__), "mb_serial_master")}')
5252
],
5353
indirect=True
5454
)
5555
def test_modbus_serial_communication(config: str, dut: Tuple[ModbusTestDut, ModbusTestDut]) -> None:
56-
dut_slave = dut[1]
57-
dut_master = dut[0]
56+
dut_slave = dut[0]
57+
dut_master = dut[1]
5858

59-
logger.info('DUT: %s start.', dut_master.dut_get_name())
6059
logger.info('DUT: %s start.', dut_slave.dut_get_name())
60+
logger.info('DUT: %s start.', dut_master.dut_get_name())
6161

6262
dut_slave.dut_test_start(dictionary=pattern_dict_slave)
6363
dut_master.dut_test_start(dictionary=pattern_dict_master)

examples/tcp/mb_tcp_master/main/tcp_master.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
#define EACH_ITEM(array, length) \
8484
(typeof(*(array)) *pitem = (array); (pitem < &((array)[length])); pitem++)
8585

86+
#define MB_CUST_DATA_LEN 100 // The length of custom command buffer
87+
8688
static const char *TAG = "MASTER_TEST";
8789

8890
// Enumeration of modbus device addresses accessed by master device
@@ -268,6 +270,7 @@ char* slave_ip_address_table[MB_DEVICE_COUNT + 1] = {
268270
};
269271

270272
const size_t ip_table_sz = (size_t)(sizeof(slave_ip_address_table) / sizeof(slave_ip_address_table[0]));
273+
static char my_custom_data[MB_CUST_DATA_LEN] = {0}; // custom data buffer to handle slave response
271274

272275
#if CONFIG_MB_SLAVE_IP_FROM_STDIN
273276

@@ -450,6 +453,20 @@ static void master_operation_func(void *arg)
450453

451454
ESP_LOGI(TAG, "Start modbus test...");
452455

456+
char *pcustom_string = "Master";
457+
mb_param_request_t req = {
458+
.slave_addr = MB_DEVICE_ADDR1, // the slave UID to send the request
459+
.command = 0x41, // the custom function code,
460+
.reg_start = 0, // unused,
461+
.reg_size = (strlen(pcustom_string) >> 1) // length of the data to send (registers)
462+
};
463+
464+
// Send the request with custom command (vendor speciic)
465+
err = mbc_master_send_request(master_handle, &req, pcustom_string);
466+
if (err != ESP_OK) {
467+
ESP_LOGE("CUSTOM_DATA", "Send custom request fail.");
468+
}
469+
453470
for(uint16_t retry = 0; retry <= MASTER_MAX_RETRY && (!alarm_state); retry++) {
454471
// Read all found characteristics from slave(s)
455472
for (uint16_t cid = 0; (err != ESP_ERR_NOT_FOUND) && cid < MASTER_MAX_CIDS; cid++) {
@@ -658,6 +675,19 @@ static esp_err_t destroy_services(void)
658675
return err;
659676
}
660677

678+
mb_exception_t my_custom_fc_handler(void *pinst, uint8_t *frame_ptr, uint16_t *plen)
679+
{
680+
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen && *plen < (MB_CUST_DATA_LEN - 1)), MB_EX_CRITICAL, TAG,
681+
"incorrect custom frame buffer");
682+
ESP_LOGW(TAG, "Custom handler, Frame ptr: %p, len: %u", frame_ptr, *plen);
683+
// This error handler will be executed to handle the request for the registered custom command
684+
// Refer the handler functions in `esp-modbus/modbus/mb_objects/functions/mbfuncinput_master.c` for more information.
685+
// Parameters: pframe: is pointer to incoming frame buffer, plen: is pointer to length including the function code
686+
strncpy((char *)&my_custom_data[0], (char *)&frame_ptr[1], MB_CUST_DATA_LEN);
687+
ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", &my_custom_data[0], (*plen - 1), ESP_LOG_WARN);
688+
return MB_EX_NONE;
689+
}
690+
661691
// Modbus master initialization
662692
static esp_err_t master_init(mb_communication_info_t *pcomm_info)
663693
{
@@ -670,6 +700,20 @@ static esp_err_t master_init(mb_communication_info_t *pcomm_info)
670700
"mb controller initialization fail, returns(0x%x).",
671701
(int)err);
672702

703+
// Add custom command handler
704+
uint8_t custom_command = 0x41;
705+
// Make sure the handler is undefined for the command
706+
err = mbc_master_set_handler(master_handle, custom_command, NULL);
707+
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ESP_ERR_INVALID_STATE, TAG,
708+
"could not override handler, returned (0x%x).", (int)err);
709+
err = mbc_master_set_handler(master_handle, custom_command, my_custom_fc_handler);
710+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
711+
"could not override handler, returned (0x%x).", (int)err);
712+
mb_fn_handler_fp phandler = NULL;
713+
err = mbc_master_get_handler(master_handle, custom_command, &phandler);
714+
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_fc_handler), ESP_ERR_INVALID_STATE, TAG,
715+
"could not get handler for command %d, returned (0x%x).", (int)custom_command, (int)err);
716+
673717
err = mbc_master_set_descriptor(master_handle, &device_parameters[0], num_device_parameters);
674718
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
675719
TAG,

examples/tcp/mb_tcp_slave/main/tcp_slave.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
#define MB_REG_HOLDING_START_AREA2_SIZE ((size_t)((HOLD_OFFSET(holding_area2_end) - HOLD_OFFSET(holding_u8_a)) << 1))
4444

4545
#define MB_PAR_INFO_GET_TOUT (10) // Timeout for get parameter info
46-
#define MB_CHAN_DATA_MAX_VAL (10)
47-
#define MB_CHAN_DATA_OFFSET (1.1f)
46+
#define MB_CHAN_DATA_MAX_VAL (50)
47+
#define MB_CHAN_DATA_OFFSET (10.1f)
4848

4949
#define MB_READ_MASK (MB_EVENT_INPUT_REG_RD \
5050
| MB_EVENT_HOLDING_REG_RD \
@@ -55,6 +55,7 @@
5555
#define MB_READ_WRITE_MASK (MB_READ_MASK | MB_WRITE_MASK)
5656
#define MB_TEST_VALUE (12345.0)
5757
#define MB_SLAVE_ADDR (CONFIG_MB_SLAVE_ADDR)
58+
#define MB_CUST_DATA_MAX_LEN (100)
5859

5960
static const char *TAG = "SLAVE_TEST";
6061

@@ -159,7 +160,7 @@ static void slave_operation_func(void *arg)
159160
(unsigned)reg_info.size);
160161
if (reg_info.address == (uint8_t*)&holding_reg_params.holding_data0)
161162
{
162-
(void)mbc_slave_unlock(slave_handle);
163+
(void)mbc_slave_lock(slave_handle);
163164
holding_reg_params.holding_data0 += MB_CHAN_DATA_OFFSET;
164165
if (holding_reg_params.holding_data0 >= (MB_CHAN_DATA_MAX_VAL - MB_CHAN_DATA_OFFSET)) {
165166
coil_reg_params.coils_port1 = 0xFF;
@@ -266,6 +267,23 @@ static esp_err_t destroy_services(void)
266267
return err;
267268
}
268269

270+
// This custom command handler will be executed to check the request for the custom command
271+
// See the `esp-modbus/modbus/mb_objects/functions/mbfuncinput.c` for more information
272+
// pframe is pointer to the buffer starting from function code, plen - is pointer to length of the data
273+
mb_exception_t my_custom_fc_handler(void *pinst, uint8_t *frame_ptr, uint16_t *plen)
274+
{
275+
char *str_append = ":Slave";
276+
MB_RETURN_ON_FALSE((frame_ptr && plen && *plen < (MB_CUST_DATA_MAX_LEN - strlen(str_append))),
277+
MB_EX_ILLEGAL_DATA_VALUE, TAG,
278+
"incorrect custom frame");
279+
ESP_LOGW("CUSTOM_DATA", "Custom handler, frame ptr: %p, len: %u", frame_ptr, *plen);
280+
ESP_LOG_BUFFER_HEXDUMP("CUSTOM_DATA", frame_ptr, *plen, ESP_LOG_WARN);
281+
frame_ptr[*plen] = '\0';
282+
strcat((char *)&frame_ptr[1], str_append);
283+
*plen = (strlen(str_append) + *plen); // the length of (response + command)
284+
return MB_EX_NONE; // Set the exception code for slave appropriately
285+
}
286+
269287
// Modbus slave initialization
270288
static esp_err_t slave_init(mb_communication_info_t *pcomm_info)
271289
{
@@ -277,6 +295,18 @@ static esp_err_t slave_init(mb_communication_info_t *pcomm_info)
277295
TAG,
278296
"mb controller create fail.");
279297

298+
uint8_t custom_command = 0x41;
299+
err = mbc_slave_set_handler(slave_handle, custom_command, NULL);
300+
MB_RETURN_ON_FALSE((err == ESP_OK || err == ESP_ERR_INVALID_STATE), ESP_ERR_INVALID_STATE, TAG,
301+
"could not reset handler, returned (0x%x).", (int)err);
302+
err = mbc_slave_set_handler(slave_handle, custom_command, my_custom_fc_handler);
303+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
304+
"could not set or override handler, returned (0x%x).", (int)err);
305+
mb_fn_handler_fp phandler = NULL;
306+
err = mbc_slave_get_handler(slave_handle, custom_command, &phandler);
307+
MB_RETURN_ON_FALSE((err == ESP_OK && phandler == my_custom_fc_handler), ESP_ERR_INVALID_STATE, TAG,
308+
"could not get handler for command %d, returned (0x%x).", (int)custom_command, (int)err);
309+
280310
// The code below initializes Modbus register area descriptors
281311
// for Modbus Holding Registers, Input Registers, Coils and Discrete Inputs
282312
// Initialization should be done for each supported Modbus register area according to register map.

0 commit comments

Comments
 (0)