Skip to content

Commit b2b7841

Browse files
committed
enable slave ID transfer from master to slave
1 parent 7ff65a2 commit b2b7841

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

modbus/mb_controller/common/esp_modbus_slave.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ esp_err_t mbc_slave_delete(void *ctx)
9191
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
9292
"Slave interface is not correctly initialized.");
9393
mbs_controller_iface_t *mbs_controller = MB_SLAVE_GET_IFACE(ctx);
94-
94+
9595
// Check if interface has been initialized
9696
MB_RETURN_ON_FALSE(mbs_controller->delete,
9797
ESP_ERR_INVALID_STATE, TAG,
@@ -259,7 +259,7 @@ esp_err_t mbc_slave_set_descriptor(void *ctx, mb_register_area_descriptor_t desc
259259
} else {
260260
mb_slave_options_t *mbs_opts = &mbs_controller->opts;
261261

262-
MB_RETURN_ON_FALSE((descr_data.size < MB_INST_MAX_SIZE) && (descr_data.size >= MB_INST_MIN_SIZE),
262+
MB_RETURN_ON_FALSE((descr_data.size < MB_INST_MAX_SIZE) && (descr_data.size >= MB_INST_MIN_SIZE),
263263
ESP_ERR_INVALID_ARG, TAG, "mb area size is incorrect.");
264264
uint16_t reg_size = REG_SIZE(descr_data.type, descr_data.size);
265265

@@ -300,8 +300,14 @@ static esp_err_t mbc_slave_send_param_info(void *ctx, mb_event_group_t par_type,
300300
MB_RETURN_ON_FALSE(ctx, ESP_ERR_INVALID_STATE, TAG,
301301
"Slave interface is not correctly initialized.");
302302
mb_slave_options_t *mbs_opts = MB_SLAVE_GET_OPTS(ctx);
303+
mbs_controller_iface_t *mbs_controller = MB_SLAVE_GET_IFACE(ctx);
303304
esp_err_t error = ESP_FAIL;
304305
mb_param_info_t par_info;
306+
uint8_t unit_id = MB_TCP_PSEUDO_ADDRESS;
307+
if (mbs_controller && mbs_controller->mb_base) {
308+
unit_id = mbs_get_request_unit_id(mbs_controller->mb_base);
309+
}
310+
par_info.unit_id = unit_id;
305311
// Check if queue is not full the send parameter information
306312
par_info.type = par_type;
307313
par_info.size = par_size;

modbus/mb_controller/common/include/esp_modbus_slave.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern "C" {
5050
typedef struct {
5151
uint32_t time_stamp; /*!< Timestamp of Modbus Event (uS)*/
5252
uint16_t mb_offset; /*!< Modbus register offset */
53+
uint8_t unit_id; /*!< Unit identifier from the request */
5354
mb_event_group_t type; /*!< Modbus event type */
5455
uint8_t *address; /*!< Modbus data storage address */
5556
size_t size; /*!< Modbus event register size (number of registers)*/
@@ -80,7 +81,7 @@ typedef struct {
8081
*
8182
* @param[out] ctx context pointer of the initialized modbus interface
8283
* @param[in] config - pointer to configuration structure for the slave
83-
*
84+
*
8485
* @return
8586
* - ESP_OK Success
8687
* - ESP_ERR_NO_MEM Parameter error
@@ -94,7 +95,7 @@ esp_err_t mbc_slave_create_tcp(mb_communication_info_t *config, void **ctx);
9495
*
9596
* @param[out] ctx context pointer of the initialized modbus interface
9697
* @param[in] config - pointer to configuration structure for the slave
97-
*
98+
*
9899
* @return
99100
* - ESP_OK Success
100101
* - ESP_ERR_NO_MEM Parameter error
@@ -181,7 +182,7 @@ mb_event_group_t mbc_slave_check_event(void *ctx, mb_event_group_t group);
181182
* @param[out] reg_info parameter info structure
182183
* @param[in] timeout Timeout in milliseconds to read information from
183184
* parameter queue
184-
*
185+
*
185186
* @return
186187
* - ESP_OK Success
187188
* - ESP_ERR_TIMEOUT Can not get data from parameter queue

modbus/mb_objects/include/mb_slave.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ mb_err_enum_t mbs_delete_handler(mb_base_t *inst, uint8_t func_code);
2727
// The helper function to get count of handlers for slave
2828
mb_err_enum_t mbs_get_handler_count(mb_base_t *inst, uint16_t *count);
2929

30+
// Get the unit identifier from the currently processed request
31+
uint8_t mbs_get_request_unit_id(mb_base_t *inst);
32+
3033
#ifdef __cplusplus
3134
}
32-
#endif
35+
#endif

modbus/mb_objects/mb_slave.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ mb_err_enum_t mbs_get_handler_count(mb_base_t *inst, uint16_t *count)
8989
return MB_ENOERR;
9090
}
9191

92+
uint8_t mbs_get_request_unit_id(mb_base_t *inst)
93+
{
94+
MB_RETURN_ON_FALSE(inst, MB_TCP_PSEUDO_ADDRESS, TAG, "slave instance invalid.");
95+
mbs_object_t *mbs_obj = MB_GET_OBJ_CTX(inst, mbs_object_t, base);
96+
return mbs_obj ? mbs_obj->rcv_addr : MB_TCP_PSEUDO_ADDRESS;
97+
}
98+
9299
static mb_exception_t mbs_check_invoke_handler(mb_base_t *inst, uint8_t func_code, uint8_t *buf, uint16_t *len)
93100
{
94101
mbs_object_t *mbs_obj = MB_GET_OBJ_CTX(inst, mbs_object_t, base);
@@ -201,7 +208,7 @@ mb_err_enum_t mbs_rtu_create(mb_serial_opts_t *ser_opts, void **in_out_obj)
201208
TAG, "name alloc fail, err: %d", (int)res);
202209
transp_obj = (mb_trans_base_t *)mbs_obj;
203210
ret = mbs_rtu_transp_create(ser_opts, (void **)&transp_obj);
204-
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
211+
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
205212
TAG, "transport creation, err: %d", (int)ret);
206213
ret = mbs_register_default_handlers(&mbs_obj->base);
207214
MB_GOTO_ON_FALSE((ret == MB_ENOERR), MB_EILLSTATE, error,
@@ -215,7 +222,7 @@ mb_err_enum_t mbs_rtu_create(mb_serial_opts_t *ser_opts, void **in_out_obj)
215222
*in_out_obj = (void *)&(mbs_obj->base);
216223
ESP_LOGD(TAG, "created object %s", mbs_obj->base.descr.parent_name);
217224
return MB_ENOERR;
218-
225+
219226
error:
220227
if (transp_obj) {
221228
mbs_rtu_transp_delete(transp_obj);
@@ -260,7 +267,7 @@ mb_err_enum_t mbs_ascii_create(mb_serial_opts_t *ser_opts, void **in_out_obj)
260267
TAG, "name alloc fail, err: %d", (int)res);
261268
transp_obj = (mb_trans_base_t *)mbs_obj;
262269
ret = mbs_ascii_transp_create(ser_opts, (void **)&transp_obj);
263-
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
270+
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
264271
TAG, "transport creation, err: %d", (int)ret);
265272
ret = mbs_register_default_handlers(&mbs_obj->base);
266273
MB_GOTO_ON_FALSE((ret == MB_ENOERR), MB_EILLSTATE, error,
@@ -274,7 +281,7 @@ mb_err_enum_t mbs_ascii_create(mb_serial_opts_t *ser_opts, void **in_out_obj)
274281
*in_out_obj = (void *)&(mbs_obj->base);
275282
ESP_LOGD(TAG, "created object %s", mbs_obj->base.descr.parent_name);
276283
return MB_ENOERR;
277-
284+
278285
error:
279286
if (transp_obj) {
280287
mbs_ascii_transp_delete(transp_obj);
@@ -320,7 +327,7 @@ mb_err_enum_t mbs_tcp_create(mb_tcp_opts_t *tcp_opts, void **in_out_obj)
320327
TAG, "name alloc fail, err: %d", (int)res);
321328
transp_obj = (mb_trans_base_t *)mbs_obj;
322329
ret = mbs_tcp_transp_create(tcp_opts, (void **)&transp_obj);
323-
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
330+
MB_GOTO_ON_FALSE((transp_obj && (ret == MB_ENOERR)), MB_EILLSTATE, error,
324331
TAG, "transport creation, err: %d", (int)ret);
325332
ret = mbs_register_default_handlers(&mbs_obj->base);
326333
MB_GOTO_ON_FALSE((ret == MB_ENOERR), MB_EILLSTATE, error,
@@ -334,7 +341,7 @@ mb_err_enum_t mbs_tcp_create(mb_tcp_opts_t *tcp_opts, void **in_out_obj)
334341
*in_out_obj = (void *)&(mbs_obj->base);
335342
ESP_LOGD(TAG, "created object %s", mbs_obj->base.descr.parent_name);
336343
return MB_ENOERR;
337-
344+
338345
error:
339346
if (transp_obj) {
340347
mbs_tcp_transp_delete(transp_obj);
@@ -469,7 +476,7 @@ mb_err_enum_t mbs_poll(mb_base_t *inst)
469476
ESP_LOGD(TAG, MB_OBJ_FMT":EV_READY", MB_OBJ_PARENT(inst));
470477
mb_port_event_res_release(MB_OBJ(inst->port_obj));
471478
break;
472-
479+
473480
case EV_FRAME_RECEIVED:
474481
ESP_LOGD(TAG, MB_OBJ_FMT":EV_FRAME_RECEIVED", MB_OBJ_PARENT(inst));
475482
mbs_obj->length = event.length;

0 commit comments

Comments
 (0)