Skip to content

modbus: add nxmodbus#3459

Open
raiden00pl wants to merge 1 commit intoapache:masterfrom
raiden00pl:nxmodbus
Open

modbus: add nxmodbus#3459
raiden00pl wants to merge 1 commit intoapache:masterfrom
raiden00pl:nxmodbus

Conversation

@raiden00pl
Copy link
Copy Markdown
Member

Summary

NxModbus is a lightweight Modbus protocol stack implementation for NuttX RTOS.

This commit adds:

  • nxmodbus stack
  • nxmbserver - Modbus Server (Slave) example
  • nxmbclient - Modbus Client (Master) tool

Supported Modbus transports:

  • ASCII
  • RTU over serial port
  • TCP
  • RAW (ADU) for custom transport implementations

It takes some concepts from FreeModBus and some from Zephyr Modbus implementation.

Impact

new modbus stack

Testing

simulator for RTU, qemu-intel64 for TCP, nucleo-c071rb for RTU.

response time on nucleo-c071rb is 10-20ms so it fixes my main issue with freemodbus #3037:
image

@raiden00pl
Copy link
Copy Markdown
Member Author

here two scripts in python that can be used for testing:

dummy_master.py
dummy_slave.py

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 17, 2026

Whoah! Amazing work BIG THANK YOU @raiden00pl !! =)

Can we put the python test scripts under nuttx/tools/modbus? These are very handy for testing and I created something like that myself too. Would be nice to have them in the repo :-)

Very popular GUI open-source modbus client application is https://sourceforge.net/projects/qmodmaster/.

NxModbus is a lightweight Modbus protocol stack implementation for NuttX RTOS.

This commit adds:
- nxmodbus stack
- nxmbserver - Modbus Server (Slave) example
- nxmbclient - Modbus Client (Master) tool

Supported Modbus transports:
- ASCII
- RTU over serial port
- TCP
- RAW (ADU) for custom transport implementations

Signed-off-by: raiden00pl <raiden00@railab.me>
Copy link
Copy Markdown
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raiden00pl Amazing work! Kudos!


pthread_mutex_lock(&ctx->lock);

ctx->server_id_len = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix alignment, just one space or put all at the same column

*
* Description:
* Build a Modbus exception response in ctx->adu in place.
*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include the Input Parameters

* Description:
* Handle FC08 Diagnostics request. Only sub-function 0x0000 (Return Query
* Data) is supported — it echoes the request data back unchanged.
*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include Input Parameter and Return type

* Private Data
****************************************************************************/

static const uint16_t g_crc16_table[256] =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think CRC implementation could come from include/nuttx/crc16.h or a new header file there

Copy link
Copy Markdown
Contributor

@masc2008 masc2008 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is a great feature!

nxmb_util_put_u16_be(&ctx->adu.data[0], addr);
nxmb_util_put_u16_be(&ctx->adu.data[2], count);
ctx->adu.data[4] = nbytes;
memcpy(&ctx->adu.data[5], buf, nbytes);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to do a sanity check.
in adu structure: data[NXMB_ADU_DATA_MAX]; this is an array that size can be configured.
it can overflow ctx->adu.data[] if "count > NXMB_ADU_DATA_MAX".
similar check needed nxmb_write_holdings and nxmb_readwrite_holdings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] New Modbus implementation for NuttX [BUG] Modbus RTU slave response time is too high

5 participants