From: aleks Date: Wed, 10 Apr 2019 15:45:22 +0000 (+0200) Subject: freemodbus: fix incorrectly set coils read event X-Git-Tag: v4.0-beta1~400^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eebbced5b63fae7f42227ce5902bedbf1c9509bf;p=esp-idf freemodbus: fix incorrectly set coils read event incorrect set of the MB_EVENT_COILS_WR event is changed while read coils in mbcontroller.c file added read/write handling for appropriate registers in freemodbus.c example file Closes https://github.com/espressif/esp-idf/issues/3289 --- diff --git a/components/freemodbus/modbus_controller/mbcontroller.c b/components/freemodbus/modbus_controller/mbcontroller.c index 2ca06bef9a..b8447b44b9 100644 --- a/components/freemodbus/modbus_controller/mbcontroller.c +++ b/components/freemodbus/modbus_controller/mbcontroller.c @@ -422,8 +422,8 @@ eMBErrorCode eMBRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress, usCoils--; } // Send an event to notify application task about event - (void)send_param_access_notification(MB_EVENT_COILS_WR); - (void)send_param_info(MB_EVENT_COILS_WR, (uint16_t)usAddress, + (void)send_param_access_notification(MB_EVENT_COILS_RD); + (void)send_param_info(MB_EVENT_COILS_RD, (uint16_t)usAddress, (uint8_t*)(pucCoilsDataBuf), (uint16_t)usNCoils); break; case MB_REG_WRITE: diff --git a/examples/protocols/modbus_slave/main/freemodbus.c b/examples/protocols/modbus_slave/main/freemodbus.c index fe0232bf2e..b7d4c566ab 100644 --- a/examples/protocols/modbus_slave/main/freemodbus.c +++ b/examples/protocols/modbus_slave/main/freemodbus.c @@ -24,6 +24,13 @@ #define MB_PAR_INFO_GET_TOUT (10) // Timeout for get parameter info #define MB_CHAN_DATA_MAX_VAL (10) #define MB_CHAN_DATA_OFFSET (0.01f) +#define MB_READ_MASK (MB_EVENT_INPUT_REG_RD \ + | MB_EVENT_HOLDING_REG_RD \ + | MB_EVENT_DISCRETE_RD \ + | MB_EVENT_COILS_RD) +#define MB_WRITE_MASK (MB_EVENT_HOLDING_REG_WR \ + | MB_EVENT_COILS_WR) +#define MB_READ_WRITE_MASK (MB_READ_MASK | MB_WRITE_MASK) static const char *TAG = "MODBUS_SLAVE_APP"; @@ -124,18 +131,16 @@ void app_main() // 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.data_chan0 < MB_CHAN_DATA_MAX_VAL;){ + for(;holding_reg_params.data_chan0 < MB_CHAN_DATA_MAX_VAL;) { // Check for read/write events of Modbus master for certain events - mb_event_group_t event = mbcontroller_check_event((MB_EVENT_HOLDING_REG_WR - | MB_EVENT_INPUT_REG_RD - | MB_EVENT_HOLDING_REG_RD - | MB_EVENT_DISCRETE_RD - | MB_EVENT_COILS_RD)); + mb_event_group_t event = mbcontroller_check_event(MB_READ_WRITE_MASK); + const char* rw_str = (event & MB_READ_MASK) ? "READ" : "WRITE"; // Filter events and process them accordingly - if((event & MB_EVENT_HOLDING_REG_WR) || (event & MB_EVENT_HOLDING_REG_RD)) { + if(event & (MB_EVENT_HOLDING_REG_WR | MB_EVENT_HOLDING_REG_RD)) { // Get parameter information from parameter queue ESP_ERROR_CHECK(mbcontroller_get_param_info(®_info, MB_PAR_INFO_GET_TOUT)); - printf("HOLDING READ/WRITE: time_stamp(us):%u, mb_addr:%u, type:%u, st_address:0x%.4x, size:%u\r\n", + printf("HOLDING %s: time_stamp(us):%u, mb_addr:%u, type:%u, st_address:0x%.4x, size:%u\r\n", + rw_str, (uint32_t)reg_info.time_stamp, (uint32_t)reg_info.mb_offset, (uint32_t)reg_info.type, @@ -161,9 +166,10 @@ void app_main() (uint32_t)reg_info.type, (uint32_t)reg_info.address, (uint32_t)reg_info.size); - } else if (event & MB_EVENT_COILS_RD) { + } else if (event & (MB_EVENT_COILS_RD | MB_EVENT_COILS_WR)) { ESP_ERROR_CHECK(mbcontroller_get_param_info(®_info, MB_PAR_INFO_GET_TOUT)); - printf("COILS READ: time_stamp(us):%u, mb_addr:%u, type:%u, st_address:0x%.4x, size:%u\r\n", + printf("COILS %s: time_stamp(us):%u, mb_addr:%u, type:%u, st_address:0x%.4x, size:%u\r\n", + rw_str, (uint32_t)reg_info.time_stamp, (uint32_t)reg_info.mb_offset, (uint32_t)reg_info.type,