|
| 1 | +====================================== |
| 2 | +``nxmbserver`` NxModbus Server Example |
| 3 | +====================================== |
| 4 | + |
| 5 | +The ``nxmbserver`` example demonstrates how to create a Modbus server (slave) |
| 6 | +using the NxModbus protocol stack. It supports RTU, ASCII, and TCP transports |
| 7 | +with simulated register data. |
| 8 | + |
| 9 | +Register Map |
| 10 | +============ |
| 11 | + |
| 12 | +The example server provides the following simulated registers: |
| 13 | + |
| 14 | +Coils (read/write, FC01/FC05/FC15): |
| 15 | + |
| 16 | +- Address range: 1-100 |
| 17 | +- Initial state: All zeros |
| 18 | +- Writable via FC05 (write single) and FC15 (write multiple) |
| 19 | + |
| 20 | +Discrete Inputs (read-only, FC02): |
| 21 | + |
| 22 | +- Address range: 1-100 |
| 23 | +- Initial state: All zeros |
| 24 | +- Read-only (cannot be modified by client) |
| 25 | + |
| 26 | +Input Registers (read-only, FC04): |
| 27 | + |
| 28 | +- Address range: 1-100 |
| 29 | +- Initial values: ``register[i] = i * 10`` (register 1 = 10, register 2 = 20, |
| 30 | + register 10 = 100) |
| 31 | + |
| 32 | +Holding Registers (read/write, FC03/FC06/FC16/FC23): |
| 33 | + |
| 34 | +- Address range: 1-100 |
| 35 | +- Initial values: ``register[i] = i * 100`` (register 1 = 100, register 2 = 200, |
| 36 | + register 10 = 1000) |
| 37 | +- Writable via FC06 (write single), FC16 (write multiple), FC23 (read/write) |
| 38 | + |
| 39 | +Command-Line Options |
| 40 | +==================== |
| 41 | + |
| 42 | +Transport Selection (required): |
| 43 | + |
| 44 | +- ``-t TYPE`` - Transport type: ``rtu``, ``ascii``, or ``tcp`` |
| 45 | + |
| 46 | +Serial Transport Options (RTU/ASCII): |
| 47 | + |
| 48 | +- ``-d DEVICE`` - Serial device path (e.g., ``/dev/ttyS1``) |
| 49 | +- ``-b BAUD`` - Baud rate (default: 115200) |
| 50 | +- ``-p PARITY`` - Parity: ``none``, ``even``, or ``odd`` (default: none) |
| 51 | + |
| 52 | +TCP Transport Options: |
| 53 | + |
| 54 | +- ``-a ADDR`` - Bind address (default: 0.0.0.0 - all interfaces) |
| 55 | +- ``-P PORT`` - TCP port (default: 502) |
| 56 | + |
| 57 | +Modbus Options: |
| 58 | + |
| 59 | +- ``-u UNIT`` - Unit ID / slave address (default: 1) |
| 60 | + |
| 61 | +Usage Examples |
| 62 | +============== |
| 63 | + |
| 64 | +RTU Server on Serial Port:: |
| 65 | + |
| 66 | + nsh> nxmbserver -t rtu -d /dev/ttyS1 -b 115200 |
| 67 | + Starting Modbus RTU server on /dev/ttyS1 (baud=115200, unit=1) |
| 68 | + Server running. Press Ctrl+C to stop. |
| 69 | + Register map: |
| 70 | + Coils: 1-100 (read/write) |
| 71 | + Discrete: 1-100 (read-only) |
| 72 | + Input regs: 1-100 (read-only, value=addr*10) |
| 73 | + Holding regs: 1-100 (read/write, initial=addr*100) |
| 74 | + |
| 75 | +TCP Server on Default Port:: |
| 76 | + |
| 77 | + nsh> nxmbserver -t tcp -P 502 |
| 78 | + Starting Modbus TCP server on port 502 (unit=1) |
| 79 | + Server running. Press Ctrl+C to stop. |
| 80 | + Register map: |
| 81 | + Coils: 1-100 (read/write) |
| 82 | + Discrete: 1-100 (read-only) |
| 83 | + Input regs: 1-100 (read-only, value=addr*10) |
| 84 | + Holding regs: 1-100 (read/write, initial=addr*100) |
| 85 | + |
| 86 | +ASCII Server with Even Parity:: |
| 87 | + |
| 88 | + nsh> nxmbserver -t ascii -d /dev/ttyS1 -b 9600 -p even -u 5 |
| 89 | + Starting Modbus ASCII server on /dev/ttyS1 (baud=9600, unit=5) |
| 90 | + Server running. Press Ctrl+C to stop. |
| 91 | + ... |
| 92 | + |
| 93 | +Testing with nxmbclient |
| 94 | +======================= |
| 95 | + |
| 96 | +The server can be tested using the ``nxmbclient`` tool: |
| 97 | + |
| 98 | +Read Input Registers (initial values):: |
| 99 | + |
| 100 | + nsh> nxmbclient -t tcp -h 127.0.0.1 read-input 1 5 |
| 101 | + Read 5 input registers from address 1: |
| 102 | + [1]: 10 |
| 103 | + [2]: 20 |
| 104 | + [3]: 30 |
| 105 | + [4]: 40 |
| 106 | + [5]: 50 |
| 107 | + |
| 108 | +Read Holding Registers (initial values):: |
| 109 | + |
| 110 | + nsh> nxmbclient -t tcp -h 127.0.0.1 read-holding 1 5 |
| 111 | + Read 5 holding registers from address 1: |
| 112 | + [1]: 100 |
| 113 | + [2]: 200 |
| 114 | + [3]: 300 |
| 115 | + [4]: 400 |
| 116 | + [5]: 500 |
| 117 | + |
| 118 | +Write and Read Back Holding Register:: |
| 119 | + |
| 120 | + nsh> nxmbclient -t tcp -h 127.0.0.1 write-holding 10 9999 |
| 121 | + Wrote holding register at address 10: 9999 |
| 122 | + |
| 123 | + nsh> nxmbclient -t tcp -h 127.0.0.1 read-holding 10 1 |
| 124 | + Read 1 holding register from address 10: |
| 125 | + [10]: 9999 |
| 126 | + |
| 127 | +Configuration |
| 128 | +============= |
| 129 | + |
| 130 | +Enable the example in your NuttX configuration:: |
| 131 | + |
| 132 | + CONFIG_EXAMPLES_NXMBSERVER=y |
| 133 | + CONFIG_NXMODBUS=y |
| 134 | + CONFIG_NXMODBUS_RTU=y # For RTU support |
| 135 | + CONFIG_NXMODBUS_ASCII=y # For ASCII support |
| 136 | + CONFIG_NXMODBUS_TCP=y # For TCP support |
| 137 | + |
| 138 | +Kconfig Options: |
| 139 | + |
| 140 | +- ``CONFIG_EXAMPLES_NXMBSERVER`` - Enable the nxmbserver example |
| 141 | +- ``CONFIG_EXAMPLES_NXMBSERVER_PROGNAME`` - Program name (default: "nxmbserver") |
| 142 | +- ``CONFIG_EXAMPLES_NXMBSERVER_PRIORITY`` - Task priority (default: 100) |
| 143 | +- ``CONFIG_EXAMPLES_NXMBSERVER_STACKSIZE`` - Stack size (default: DEFAULT_TASK_STACKSIZE) |
| 144 | + |
| 145 | +See Also |
| 146 | +======== |
| 147 | + |
| 148 | +- :doc:`/applications/industry/nxmodbus/index` - NxModbus protocol stack |
| 149 | +- :doc:`/applications/system/nxmbclient/index` - NxModbus client tool |
0 commit comments