Repository navigation
industry/nxmodbus: Add FC20 and FC21 file record support. - #3910
Merged
acassis merged 2 commits intoOct 24, 2026
Merged
Conversation
Add the Read File Record (FC20, 0x14) and Write File Record (FC21, 0x15) server functions of the Modbus Application Protocol Specification V1.1b3 (sections 6.14 and 6.15). They move whole files, such as a configuration or a firmware image, to a device that has only a Modbus link. Each function has its own option, NXMODBUS_FUNC_READ_FILE and NXMODBUS_FUNC_WRITE_FILE. Both are off by default so that the existing configurations do not change in size: on an Arm Cortex-M4 the handlers take 612 and 362 bytes of code. The data model stays with the application, as for the other function codes. A new file_cb callback in struct nxmb_callbacks_s gets one group of records (file 1 to 0xffff, record 0 to 9999, nregs 16-bit registers) and reads or writes the buffer in wire order. It returns -ENOENT for a file or record that does not exist, which becomes exception 2. The member sits before priv, so a structure initialized by position would break; nothing in the tree does that. The sub-requests are validated in full before the first callback: the reference type must be 6, the group must stay inside the 10000 records of the file, and the read response must fit in 0xf5 bytes and in the ADU. The response of a read overwrites the request, so its groups are parsed first, up to 35 (the most a request can carry), in an array of 6 bytes each on the stack, 210 bytes in all. A write is not atomic, as the specification does not make it so: a callback failure leaves the earlier groups written. Tested on the host, with the real handlers built against stub headers: the two examples of the specification byte for byte, the limits of the fields, the exceptions, 35 sub-requests in place and the largest group, also under AddressSanitizer and UBSan. Built for an Arm Cortex-M4. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
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 <danieloak@gmail.com>
acassis
approved these changes
Oct 24, 2026
cederom
approved these changes
Oct 24, 2026
cederom
left a comment
Contributor
There was a problem hiding this comment.
Great stuff thank you @daniel-p-carvalho :-)
Please also provide documentation with examples in https://nuttx.apache.org/docs/latest/applications/industry/nxmodbus/index.html :-)
Contributor
Author
|
Thanks @cederom! The documentation is in apache/nuttx#20966 and has already been approved by @xiaoxiang781216. It covers the options, the |
xiaoxiang781216
approved these changes
Oct 24, 2026
acassis
pushed a commit
to apache/nuttx
that referenced
this pull request
Oct 24, 2026
Describe the Read File Record (FC20) and Write File Record (FC21) server functions of nxmodbus, added by apache/nuttx-apps#3910: the NXMODBUS_FUNC_READ_FILE and NXMODBUS_FUNC_WRITE_FILE options, the file_cb callback, how its errors map to Modbus exceptions, the validation done before the first callback and the fact that a write is not atomic. Also describe the file records served by the nxmbserver example. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add the server side of FC20 (Read File Record) and FC21 (Write File Record) to
nxmodbus, sections 6.14 and 6.15 of the Modbus Application Protocol Specification V1.1b3. They move whole files, such as a configuration or a firmware image, to a device that only has a Modbus link.NXMODBUS_FUNC_READ_FILEandNXMODBUS_FUNC_WRITE_FILE, bothdefault nso existing configurations do not grow.file_cbinstruct nxmb_callbacks_sgets one group of records (file, record, nregs) and reads or writes the buffer in wire order.-ENOENTbecomes exception 2.examples/nxmbservergets two RAM files to exercise both functions.Impact
Only users who enable the new options are affected. On an Arm Cortex-M4 the handlers take 612 and 362 bytes of code. The new
file_cbmember sits beforeprivinstruct nxmb_callbacks_s, so a structure initialized by position would break; nothing in the tree does that. The user documentation (function code list and options) will follow in a PR tonuttx.Testing
Host: Linux x86_64,
arm-none-eabi-gcc15.2.1. Target: NUCLEO-L432KC,nxmbserveras the init task on USART2 (ST-LINK VCP), RTU 115200 8E1, nuttxmaster(846e525657) plus this PR.Client: pymodbus 3.16.1, 11 cases: both examples of the specification, 100-record reads and writes, two groups in one request, and the exceptions (file or record out of range, response over 0xf5 bytes). The handlers were also run on the host against stub headers under AddressSanitizer and UBSan.
Client output