Skip to content

Commit

Permalink
fix slave id handling in master and slave
Browse files Browse the repository at this point in the history
  • Loading branch information
alisitsyn committed Oct 22, 2024
1 parent 0fa0d73 commit 3ca3b6c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
18 changes: 12 additions & 6 deletions freemodbus/modbus/functions/mbfuncother.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED

#define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
#define MB_PDU_FUNC_DATA_OFF ( MB_PDU_DATA_OFF + 1 )

/* ----------------------- Static variables ---------------------------------*/
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF] = {0};
static USHORT usMBSlaveIDLen;

/* ----------------------- Start implementation -----------------------------*/
Expand Down Expand Up @@ -93,8 +93,8 @@ eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
if( *usLen <= ( MB_FUNC_OTHER_REP_SLAVEID_BUF - 2 ) )
{
ucByteCount = ( UCHAR )( pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] );
ESP_LOGW("TEST", "Handle slave info command.");
eRegStatus = eMBMasterRegInputCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], 0, (ucByteCount >> 1) );
ESP_LOGE("TEST", "Master handler of slave info command %u bytes.", ucByteCount);
eRegStatus = eMBMasterRegInputCB( &pucFrame[MB_PDU_FUNC_DATA_OFF], 0, (ucByteCount >> 1) );
/* If an error occured convert it into a Modbus exception. */
if( eRegStatus != MB_ENOERR )
{
Expand Down Expand Up @@ -129,6 +129,7 @@ eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
( size_t )usAdditionalLen );
usMBSlaveIDLen += usAdditionalLen;
}
ESP_LOG_BUFFER_HEX_LEVEL("SET_SLAVE_ID_BUF", (void*)ucMBSlaveID, usMBSlaveIDLen, ESP_LOG_WARN);
}
else
{
Expand All @@ -137,11 +138,16 @@ eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
return eStatus;
}

// pucFrame points to ADU
eMBException
eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
{
memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
*usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
memcpy( &pucFrame[MB_PDU_FUNC_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
*usLen = ( USHORT )( MB_PDU_FUNC_DATA_OFF + usMBSlaveIDLen );
ESP_LOG_BUFFER_HEX_LEVEL("REPORT_SLAVE_ID", (void*)ucMBSlaveID, usMBSlaveIDLen, ESP_LOG_WARN);
pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] = usMBSlaveIDLen;
ESP_LOG_BUFFER_HEX_LEVEL("REPORT_SLAVE_ID_FRAME", (void*)pucFrame, *usLen, ESP_LOG_WARN);
printf("Report slave ID command is called %d.", *usLen);
return MB_EX_NONE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ static esp_err_t mbc_serial_master_set_parameter(uint16_t cid, char* name, uint8
*/
// Callback function for reading of MB Input Registers
eMBErrorCode eMBRegInputCBSerialMaster(UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNRegs)
USHORT usNRegs)
{
MB_MASTER_CHECK((mbm_interface_ptr != NULL),
MB_EILLSTATE,
Expand All @@ -510,13 +510,14 @@ eMBErrorCode eMBRegInputCBSerialMaster(UCHAR * pucRegBuffer, USHORT usAddress,
// If input or configuration parameters are incorrect then return an error to stack layer
if ((pucInputBuffer != NULL)
&& (usNRegs >= 1)
&& (usRegInputNregs == usRegs)) {
&& ((usRegInputNregs == usRegs) || !usAddress)) {
while (usRegs > 0) {
_XFER_2_RD(pucInputBuffer, pucRegBuffer);
usRegs -= 1;
}
} else {
eStatus = MB_ENOREG;
printf("error callback %u, %p, %u, %u, %u. ", usRegInputNregs, pucInputBuffer, usAddress, usNRegs, usRegs);
}
return eStatus;
}
Expand Down
14 changes: 5 additions & 9 deletions test/serial/mb_serial_master/main/master.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,22 @@ static void master_operation_func(void *arg)

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

mb_param_request_t req = {0};

uint8_t info_buf[64] = {0};

// Command - 17 (0x11) Report Slave ID (Serial Line only)
req.command = 0x11;
// The command contains vendor specific data.
// This version of command handler needs to define expected number of registers
// that will be returned from concrete slave.
// The returned slave info data will be stored in the `info_buf`.
req.reg_size = 16;
// This example will reques the slave infor from slave UID = 1.
// This example will request the slave info from slave UID = 1.
// It can be modified accordingly for other slaves.
req.slave_addr = 0x01;
mb_param_request_t req = {.slave_addr = 1, .command = 0x11, .reg_start = 1, .reg_size = 16};

uint8_t info_buf[32] = {0};

err = mbc_master_send_request(&req, &info_buf[0]);
if (err != ESP_OK) {
ESP_LOGE("SLAVE_INFO", "Read slave info fail.");
} else {
ESP_LOGI("SLAVE_INFO", "Slave ID array: %" PRIX32, *(uint32_t*)&info_buf[0]);
ESP_LOG_BUFFER_HEX_LEVEL("SLAVE_INFO", (void*)info_buf, sizeof(info_buf), ESP_LOG_WARN);
}

for(uint16_t retry = 0; retry <= MASTER_MAX_RETRY && (!alarm_state); retry++) {
Expand Down
15 changes: 15 additions & 0 deletions test/serial/mb_serial_slave/main/slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ static void setup_reg_data(void)
input_reg_params.input_data7 = 4.78;
}

extern int
eMBSetSlaveID(uint8_t slave_id, bool is_running,
uint8_t const *pdata, uint16_t data_len);

// An example application of Modbus slave. It is based on freemodbus stack.
// See deviceparams.h file for more information about assigned Modbus parameters.
// These parameters can be accessed from main application and also can be changed
Expand Down Expand Up @@ -226,6 +230,17 @@ void app_main(void)
ESP_LOGI(TAG, "Modbus slave stack initialized.");
ESP_LOGI(TAG, "Start modbus test...");

//const uint32_t ext_data = 0x11223344;
uint8_t ext_data[] = {0x11, 0x22, 0x33, 0x44, 0x55};

int err = eMBSetSlaveID(comm_info.slave_addr, true, (uint8_t *)&ext_data, sizeof(ext_data));
if (!err) {
//ESP_LOGI("SET_SLAVE_ID", "Set slave ID = %d, ext_data=0x%" PRIx32, comm_info.slave_addr, ext_data);
ESP_LOG_BUFFER_HEX_LEVEL("SET_SLAVE_ID", (void*)ext_data, sizeof(ext_data), ESP_LOG_WARN);
} else {
ESP_LOGE("SET_SLAVE_ID", "Set slave ID fail, err=%d.", err);
}

// The cycle below will be terminated when parameter holdingRegParams.dataChan0
// incremented each access cycle reaches the CHAN_DATA_MAX_VAL value.
for(;holding_reg_params.holding_data0 < MB_CHAN_DATA_MAX_VAL;) {
Expand Down

0 comments on commit 3ca3b6c

Please sign in to comment.