From c63a6d71140417d91e2f6509ecc2be558f9d36ac Mon Sep 17 00:00:00 2001 From: Alex Lisitsyn Date: Tue, 30 Apr 2024 15:27:36 +0800 Subject: [PATCH] fix master broadcast request issue --- freemodbus/modbus/mb_m.c | 8 ++++++-- .../modbus_controller/mbc_serial_master.c | 12 +++++++++--- idf_component.yml | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/freemodbus/modbus/mb_m.c b/freemodbus/modbus/mb_m.c index 24c51d6..df1d07e 100644 --- a/freemodbus/modbus/mb_m.c +++ b/freemodbus/modbus/mb_m.c @@ -205,7 +205,7 @@ eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM; pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM; pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired; - eMBMasterCurrentMode = MB_ASCII; + eMBMasterCurrentMode = eMode; eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity); break; @@ -220,7 +220,7 @@ eMBMasterSerialInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eP pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM; pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM; pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired; - eMBMasterCurrentMode = MB_RTU; + eMBMasterCurrentMode = eMode; eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity ); break; @@ -391,6 +391,10 @@ eMBMasterPoll( void ) break; case EV_MASTER_EXECUTE: if (xCurTransactionId == xEvent.xTransactionId) { + if ( xMBMasterRequestIsBroadcast() + && (( ucMBMasterGetCommMode() == MB_RTU ) || ( ucMBMasterGetCommMode() == MB_ASCII ) ) ) { + ucMBRcvFrame = ucMBSendFrame; + } MB_PORT_CHECK(ucMBRcvFrame, MB_EILLSTATE, "receive buffer initialization fail."); ESP_LOGD(MB_PORT_TAG, "%" PRIu64 ":EV_MASTER_EXECUTE", xEvent.xTransactionId); ucFunctionCode = ucMBRcvFrame[MB_PDU_FUNC_OFF]; diff --git a/freemodbus/serial_master/modbus_controller/mbc_serial_master.c b/freemodbus/serial_master/modbus_controller/mbc_serial_master.c index a794166..307dca2 100644 --- a/freemodbus/serial_master/modbus_controller/mbc_serial_master.c +++ b/freemodbus/serial_master/modbus_controller/mbc_serial_master.c @@ -358,6 +358,11 @@ static esp_err_t mbc_serial_master_set_request(char* name, mb_param_mode_t mode, // Compare the name of parameter with parameter key from table int comp_result = memcmp((const void*)name, (const void*)reg_ptr->param_key, (size_t)param_key_len); if (comp_result == 0) { + // Returns an error in case of broadcast read request + if (!reg_ptr->mb_slave_addr && (mode == MB_PARAM_READ)) { + error = ESP_ERR_INVALID_ARG; + break; + } // The correct line is found in the table and reg_ptr points to the found parameter description request->slave_addr = reg_ptr->mb_slave_addr; request->reg_start = reg_ptr->mb_reg_start; @@ -417,7 +422,8 @@ static esp_err_t mbc_serial_master_get_parameter(uint16_t cid, char* name, uint8 // Set the type of parameter found in the table *type = reg_info.param_type; } else { - ESP_LOGE(TAG, "%s: The cid(%u) not found in the data dictionary.", __FUNCTION__, reg_info.cid); + ESP_LOGE(TAG, "%s: The requested cid(%u) is not configured correctly. Check data dictionary for correctness.", + __FUNCTION__, (unsigned)cid); error = ESP_ERR_INVALID_ARG; } return error; @@ -465,8 +471,8 @@ static esp_err_t mbc_serial_master_set_parameter(uint16_t cid, char* name, uint8 *type = reg_info.param_type; free(pdata); } else { - ESP_LOGE(TAG, "%s: The requested cid(%u) not found in the data dictionary.", - __FUNCTION__, (unsigned)reg_info.cid); + ESP_LOGE(TAG, "%s: The requested cid(%u) is not configured correctly. Check data dictionary for correctness.", + __FUNCTION__, (unsigned)cid); error = ESP_ERR_INVALID_ARG; } return error; diff --git a/idf_component.yml b/idf_component.yml index 49f9b06..c69e18c 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,4 +1,4 @@ -version: "1.0.13" +version: "1.0.14" description: ESP-MODBUS is the official Modbus library for Espressif SoCs. url: https://github.com/espressif/esp-modbus dependencies: