forked from espressif/esp-modbus
add the 0x11 command - get slave info
This commit is contained in:
@@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
/* ----------------------- Modbus includes ----------------------------------*/
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
#include "mb.h"
|
#include "mb.h"
|
||||||
|
#include "mb_m.h"
|
||||||
#include "mbframe.h"
|
#include "mbframe.h"
|
||||||
#include "mbproto.h"
|
#include "mbproto.h"
|
||||||
#include "mbconfig.h"
|
#include "mbconfig.h"
|
||||||
@@ -52,11 +53,61 @@
|
|||||||
|
|
||||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
#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 )
|
||||||
|
|
||||||
/* ----------------------- Static variables ---------------------------------*/
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
||||||
static USHORT usMBSlaveIDLen;
|
static USHORT usMBSlaveIDLen;
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
|
eMBMasterReqErrCode
|
||||||
|
eMBMasterReqReportSlaveID( UCHAR ucSndAddr, LONG lTimeOut )
|
||||||
|
{
|
||||||
|
UCHAR *ucMBFrame;
|
||||||
|
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
|
||||||
|
|
||||||
|
if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
|
||||||
|
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_OTHER_REPORT_SLAVEID;
|
||||||
|
vMBMasterSetPDUSndLength( 1 );
|
||||||
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_TRANSMIT | EV_MASTER_TRANS_START );
|
||||||
|
eErrStatus = eMBMasterWaitRequestFinish( );
|
||||||
|
}
|
||||||
|
return eErrStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
UCHAR ucByteCount = 0;
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
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) );
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
eMBErrorCode
|
eMBErrorCode
|
||||||
eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
eMBSetSlaveID( UCHAR ucSlaveID, BOOL xIsRunning,
|
||||||
|
@@ -365,6 +365,8 @@ eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
|||||||
*\brief These Modbus functions are called for user when Modbus run in Master Mode.
|
*\brief These Modbus functions are called for user when Modbus run in Master Mode.
|
||||||
*/
|
*/
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
|
eMBMasterReqReportSlaveID( UCHAR ucSndAddr, LONG lTimeOut );
|
||||||
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
|
eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
|
||||||
|
@@ -129,7 +129,7 @@ BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
|
|||||||
*/
|
*/
|
||||||
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||||
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBMasterFuncReportSlaveID},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||||
{MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
|
{MB_FUNC_READ_INPUT_REGISTER, eMBMasterFuncReadInputRegister},
|
||||||
|
@@ -192,6 +192,9 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
|
|||||||
// Calls appropriate request function to send request and waits response
|
// Calls appropriate request function to send request and waits response
|
||||||
switch(mb_command)
|
switch(mb_command)
|
||||||
{
|
{
|
||||||
|
case MB_FUNC_OTHER_REPORT_SLAVEID:
|
||||||
|
mb_error = eMBMasterReqReportSlaveID((UCHAR)mb_slave_addr, (LONG)MB_SERIAL_API_RESP_TICS );
|
||||||
|
break;
|
||||||
case MB_FUNC_READ_COILS:
|
case MB_FUNC_READ_COILS:
|
||||||
mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
mb_error = eMBMasterReqReadCoils((UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||||
(USHORT)mb_size , (LONG)MB_SERIAL_API_RESP_TICS );
|
(USHORT)mb_size , (LONG)MB_SERIAL_API_RESP_TICS );
|
||||||
@@ -216,7 +219,6 @@ static esp_err_t mbc_serial_master_send_request(mb_param_request_t* request, voi
|
|||||||
mb_error = eMBMasterReqWriteHoldingRegister( (UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
mb_error = eMBMasterReqWriteHoldingRegister( (UCHAR)mb_slave_addr, (USHORT)mb_offset,
|
||||||
*(USHORT*)data_ptr, (LONG)MB_SERIAL_API_RESP_TICS );
|
*(USHORT*)data_ptr, (LONG)MB_SERIAL_API_RESP_TICS );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MB_FUNC_WRITE_MULTIPLE_REGISTERS:
|
case MB_FUNC_WRITE_MULTIPLE_REGISTERS:
|
||||||
mb_error = eMBMasterReqWriteMultipleHoldingRegister( (UCHAR)mb_slave_addr,
|
mb_error = eMBMasterReqWriteMultipleHoldingRegister( (UCHAR)mb_slave_addr,
|
||||||
(USHORT)mb_offset, (USHORT)mb_size,
|
(USHORT)mb_offset, (USHORT)mb_size,
|
||||||
|
@@ -305,6 +305,28 @@ static void master_operation_func(void *arg)
|
|||||||
|
|
||||||
ESP_LOGI(TAG, "Start modbus test...");
|
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.
|
||||||
|
// It can be modified accordingly for other slaves.
|
||||||
|
req.slave_addr = 0x01;
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
for(uint16_t retry = 0; retry <= MASTER_MAX_RETRY && (!alarm_state); retry++) {
|
for(uint16_t retry = 0; retry <= MASTER_MAX_RETRY && (!alarm_state); retry++) {
|
||||||
// Read all found characteristics from slave(s)
|
// Read all found characteristics from slave(s)
|
||||||
for (uint16_t cid = 0; (err != ESP_ERR_NOT_FOUND) && cid < MASTER_MAX_CIDS; cid++) {
|
for (uint16_t cid = 0; (err != ESP_ERR_NOT_FOUND) && cid < MASTER_MAX_CIDS; cid++) {
|
||||||
|
Reference in New Issue
Block a user