This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 6f4e0d5f816f22adb5079156ef54d58255228ccf Author: Daniel P. Carvalho <[email protected]> AuthorDate: Wed Oct 21 21:14:39 2026 -0300 examples/nxmbserver: Add file records to the server. Add two files of 100 records each, kept in RAM and served through the file_cb callback, so that the FC20 and FC21 functions of nxmodbus can be exercised. They are filled with a known pattern: record n of file 1 is n, and record n of file 2 is 0x1000 + n. A file or a record out of range returns -ENOENT, which the stack turns into exception 2. The functions are enabled with NXMODBUS_FUNC_READ_FILE and NXMODBUS_FUNC_WRITE_FILE. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Daniel P. Carvalho <[email protected]> --- examples/nxmbserver/nxmbserver_main.c | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/examples/nxmbserver/nxmbserver_main.c b/examples/nxmbserver/nxmbserver_main.c index 1dfe376e1..fe43644c4 100644 --- a/examples/nxmbserver/nxmbserver_main.c +++ b/examples/nxmbserver/nxmbserver_main.c @@ -47,6 +47,8 @@ #define NUM_DISCRETE 100 #define NUM_INPUT_REGS 100 #define NUM_HOLDING_REGS 100 +#define NUM_FILES 2 +#define NUM_RECORDS 100 /**************************************************************************** * Private Types @@ -69,6 +71,7 @@ struct server_data_s uint8_t discrete[NUM_DISCRETE / 8 + 1]; uint16_t input_regs[NUM_INPUT_REGS]; uint16_t holding_regs[NUM_HOLDING_REGS]; + uint16_t files[NUM_FILES][NUM_RECORDS]; }; /* Modbus server configuration */ @@ -323,6 +326,45 @@ static int holding_callback(FAR uint8_t *buf, uint16_t addr, uint16_t count, return OK; } +/**************************************************************************** + * Name: file_callback + * + * Description: + * Files 1 to NUM_FILES, with NUM_RECORDS records each, kept in RAM. + * + ****************************************************************************/ + +static int file_callback(FAR uint8_t *buf, uint16_t file, uint16_t record, + uint16_t nregs, enum nxmb_regmode_e mode, + FAR void *priv) +{ + FAR uint16_t *records; + uint16_t i; + + if (file == 0 || file > NUM_FILES || record + nregs > NUM_RECORDS) + { + return -ENOENT; + } + + records = &g_data.files[file - 1][record]; + + for (i = 0; i < nregs; i++) + { + if (mode == NXMB_REG_READ) + { + buf[i * 2] = (uint8_t)(records[i] >> 8); + buf[i * 2 + 1] = (uint8_t)(records[i] & 0xff); + } + else + { + records[i] = (uint16_t)(buf[i * 2] << 8) | + (uint16_t)buf[i * 2 + 1]; + } + } + + return OK; +} + /**************************************************************************** * Name: init_data ****************************************************************************/ @@ -342,6 +384,12 @@ static void init_data(void) { g_data.holding_regs[i] = i * 100; } + + for (i = 0; i < NUM_RECORDS; i++) + { + g_data.files[0][i] = i; + g_data.files[1][i] = 0x1000 + i; + } } /**************************************************************************** @@ -458,6 +506,7 @@ int main(int argc, FAR char *argv[]) callbacks.discrete_cb = discrete_callback; callbacks.input_cb = input_callback; callbacks.holding_cb = holding_callback; + callbacks.file_cb = file_callback; memset(&mb_config, 0, sizeof(mb_config)); mb_config.unit_id = config.unit_id;
