diff -r b3b6991f1cb6 -r 367eb26972b1 modbus/mb_runtime.c --- a/modbus/mb_runtime.c Fri Dec 25 17:11:38 2020 +0000 +++ b/modbus/mb_runtime.c Thu Jan 28 14:17:40 2021 +0000 @@ -198,12 +198,36 @@ } -static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) - {return __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);} -static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) - {return __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);} -static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) - {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); } +static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { + int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes); + + if (res >= 0) + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_read_req_counter++; + + return res; +} + +static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { + int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); + + if (res >= 0) + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_read_req_counter++; + + return res; +} + +static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { + int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); + + if (res >= 0) + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_write_req_counter++; + + return res; +} + @@ -212,6 +236,9 @@ if ((start_addr + word_count) > MEM_AREA_SIZE) return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_read_req_counter++; + /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ memcpy(/* dest */ (void *)data_words, /* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), @@ -226,6 +253,9 @@ if ((start_addr + word_count) > MEM_AREA_SIZE) return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_read_req_counter++; + /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ memcpy(/* dest */ (void *)data_words, /* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), @@ -241,6 +271,9 @@ if ((start_addr + word_count) > MEM_AREA_SIZE) return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ + /* update the counter of Modbus requests we have processed. */ + ((server_mem_t *)mem_map)->flag_write_req_counter++; + /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. * It is not therefore safe to cast it to an u16 data type. * The following code cannot be used. memcpy() is used instead. @@ -525,6 +558,8 @@ // -1 --> modbus node created!; no thread created // >=0 --> modbus node created!; thread created! server_nodes[index].mb_nd = -2; + server_nodes[index].mem_area.flag_write_req_counter = 0; + server_nodes[index].mem_area.flag_read_req_counter = 0; /* see comment in mb_runtime.h to understad why we need to initialize these entries */ switch (server_nodes[index].node_address.naf) { case naf_tcp: