<img src="../../../../business/v2/link/images/top/modbus.png" alt="Modbus RTU device access" loading="lazy"/>

## Page Guide

- **Use Cases**: Suitable for Modbus RTU gateways, direct devices, sub-device polling collection, and register read/write scenarios.
- **Prerequisites**: A gateway or direct device product has been created with the communication protocol set to ModbusRtu, and the transport protocol (MQTT or TCP) selected according to the site.
- **Operation Path**: Create the product topology → configure Thing Model registers → create devices → configure polling tasks → bring devices online for collection.
- **Acceptance Criteria**: Polling tasks can be delivered, register data can be reported, and the active collection and collect-all functions return normal results.

The configuration path for connecting Modbus RTU devices to FastBee is: create gateway and sub-device products (or a direct device product) with the communication protocol set to ModbusRtu and establish the topology, define register points in the Thing Model, then create polling tasks for the gateway or direct device; once the device is online, the platform delivers collection commands on the configured schedule and parses the register data. This page covers the platform-side configuration steps and the Modbus protocol frame details; for a solution that uses the FastBee ARM edge gateway for protocol conversion and acquisition on the field bus, see the [FastBee ARM Gateway Overview](https://www.fastbee.cn/doc/en/gateway.md).

## 1. Create products and polling tasks

Create a gateway product and a sub-device product first, then establish the topology relationship between the gateway and sub-device. For the topology process, see the [Gateway and sub-device](https://www.fastbee.cn/doc/en/business/v2/link/sub-device.md) document.

After binding, the topology relationship is shown below:

<img src="../../../../business/v2/link/images/modbus/img15.png" style="border:1px solid #ccc;border-radius:1px;" alt="1. Create products and polling tasks" loading="lazy" />

Next, create polling tasks for the sub-device. The following example uses sub-device 01.

### 1.1 Open the polling task page

Open **Sub-device -> Device details -> Polling task**.

::: tip
Note: polling tasks can be configured for gateway devices and direct devices; sub-devices do not support them.
:::

<img src="../../../../business/v2/link/images/modbus/img16.png" style="border:1px solid #ccc;border-radius:1px;" alt="1.1 Open the polling task page" loading="lazy" />

### 1.2 Add a polling task

<img src="../../../../business/v2/link/images/modbus/img17.png" style="border:1px solid #ccc;border-radius:1px;" alt="1.2 Add a polling task" loading="lazy" />

Supported function codes:

<img src="../../../../business/v2/link/images/modbus/img20.png" style="border:1px solid #ccc;border-radius:1px;" alt="1.2 Add a polling task" loading="lazy" />

Polling interval examples:

| Interval |
| --- |
| 1 minute |
| 3 minutes |
| 5 minutes |
| 10 minutes |
| 20 minutes |
| 30 minutes |
| ... |

The task list is shown below:

<img src="../../../../business/v2/link/images/modbus/img18.png" style="border:1px solid #ccc;border-radius:1px;" alt="1.2 Add a polling task" loading="lazy" />

After the polling task is created, polling commands are automatically delivered when the gateway device comes online.

### 1.3 Active collection and collect all

**Active collection** immediately sends a read command and reads the register value corresponding to the selected thing model.

**Collect all** sends all polling commands and reads all configured register points.

<img src="../../../../business/v2/link/images/modbus/img19.png" style="border:1px solid #ccc;border-radius:1px;" alt="1.3 Active collection and collect all" loading="lazy" />

## 2. Modbus protocol

### 2.1 Modbus RTU frame

A Modbus RTU message is one complete data frame. In essence, it is a complete sequence of instruction bytes.

![image](https://www.fastbee.cn/doc/assets/modbus20.DO8NAIGu.png)

<img src="../../../../business/v2/link/images/modbus/modbus21.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" alt="2.1 Modbus RTU frame" loading="lazy" />

Frame fields:

* **Slave address**: each slave has a unique one-byte address. The full range is `0-255`; the common valid slave range is `1-247`. Address `255` can be used as a broadcast address.
* **Function code**: one byte. It defines what the instruction does, such as reading data or writing data.
* **Data**: varies by function code. For a read instruction, it usually contains the start address and the number of registers to read.
* **CRC**: cyclic redundancy check used to detect transmission errors.

### 2.2 Modbus function codes

Modbus assigns a function code to each supported operation.

| Code | Meaning |
| --- | --- |
| 01H | Read coils |
| 02H | Read discrete inputs |
| 03H | Read holding registers |
| 04H | Read input registers |
| 05H | Write single coil |
| 06H | Write single register |
| 0FH | Write multiple coils |
| 10H | Write multiple registers |

### 2.3 Master reads data from slave

![image](https://www.fastbee.cn/doc/assets/modbus23.B-Hk6zwn.png)

Example request:

```text
01 03 00 01 00 01 D5 CA
```

Field explanation:

* `0x01`: slave address.
* `0x03`: function code for reading holding registers.
* `0x00 0x01`: start register address. Reading starts from `0x0001`.
* `0x00 0x01`: number of registers to read.
* `0xD5 0xCA`: CRC.

Example response:

| Slave address | Function code | Byte count | Byte 1 | Byte 2 | CRC |
| --- | --- | --- | --- | --- | --- |
| 0x01 | 0x03 | 0x02 | 0x00 | 0x17 | 0xF8 0x4A |

Response explanation:

* `0x01`: slave address.
* `0x03`: read function code.
* `0x02`: returned byte count is 2.
* `0x00 0x17`: register value.
* `0xF8 0x4A`: CRC.

### 2.4 Master writes data to slave

![image](https://www.fastbee.cn/doc/assets/modbus22.krfu9eFn.png)

Example request:

```text
01 06 00 01 00 17 98 04
```

Field explanation:

* `0x01`: slave address.
* `0x06`: function code for writing a single register.
* `0x00 0x01`: target register address.
* `0x00 0x17`: value to write.
* `0x98 0x04`: CRC.

The slave returns the same frame after a successful write.

```text
0106000100179804
```

| 0x01 | 06 | 00 01 | 00 17 | 98 04 |
| --- | --- | --- | --- | --- |
| Slave address | Function code | Data address | Data | CRC |

### 2.5 Read command field example

```text
0103020017F84A
```

| 0x01 | 03 | 00 01 | 00 01 | D5 CA |
| --- | --- | --- | --- | --- |
| Slave address | Function code | Data address | Number of values | CRC |

### 2.6 Message example

Read registers `40005` and `40006`, assuming the slave address is `1`.

Downstream message:

```text
01 03 00 04 00 02 85 ca
```

| Slave address | Function code | Start register address | Number of registers | CRC |
| --- | --- | --- | --- | --- |
| 01 | 03 | 00 04 | 00 02 | 85 ca |

Upstream message:

```text
01 03 04 00 00 00 00 fa 33
```

| Slave address | Function code | Returned byte count | Register 40005 data | Register 40006 data | CRC |
| --- | --- | --- | --- | --- | --- |
| 01 | 03 | 04 | 00 00 | 00 00 | fa 33 |

### 2.7 Common Modbus RTU function codes

| Data type | Read function code | Write function code | Object type |
| --- | --- | --- | --- |
| Discrete input | 02 | - | Single bit |
| Coil status | 01 | 05, 15 | Single bit |
| Input register | 04 | - | 16-bit word |
| Holding register | 03 | 06, 16 | 16-bit word |

### 2.8 Register start addresses

| Data type | Parameter address / register number |
| --- | --- |
| Discrete input | 00001-0FFFF |
| Coil status | 10001-1FFFF |
| Input register | 30001-3FFFF |
| Holding register | 40001-4FFFF |

### 2.9 Platform configuration acceptance

Modbus RTU access acceptance should cover not only serial communication, but also correct product, register, and polling task configuration on the platform.

| Acceptance Item | Normal Behavior |
| --- | --- |
| Product type | The product uses a Modbus-related communication protocol |
| Device type | Direct or gateway type matches the field link |
| Slave address | Consistent with the field instrument or PLC address |
| Register address | Address, function code, data type, and scaling factor are correct |
| Collection mode | Cloud polling is enabled when active cloud collection is required |
| Polling task | The interval is reasonable and the task is enabled |
| Data display | Parsed values are visible in device data collection or running status |
| Command delivery | Write-coil or write-register commands are answered by the device |

## 3. FAQ

### No data can be read

First check the serial port, baud rate, data bits, stop bits, parity, slave address, function code, and register start address.

### Data values are incorrect

Check the byte order, data type, scaling factor, decimal places, signed/unsigned setting, and whether the register number and the protocol address are being confused.

### The platform does not collect actively

Check whether a polling task is configured on the product or device side, whether cloud polling is selected as the collection mode, and whether the task is enabled.

### Old data is still read after configuration changes

Check whether the Modbus-related cache in Redis needs to be cleared, whether the device or service needs a restart, and whether the product release status is updated.

### Data from multiple slaves is mixed up

Check whether slave addresses are duplicated, whether sub-device addresses are unique, and whether the gateway-to-sub-product binding is correct.

## 4. Secondary Development Entry Points

| Type | Path or API | Description |
| --- | --- | --- |
| Frontend pages | `vue3/src/views/iot/product`, `vue3/src/views/iot/device` | Modbus configuration, polling tasks, and device collection |
| Backend APIs | `/api/modbus`, `/modbus/config`, `/modbus/job` | Modbus configuration and tasks |
| Product Modbus | `/productModbus/gateway`, `/productModbus/job` | Product-level gateway and polling configuration |
| Protocol module | `springboot/fastbee-protocol` | Modbus message parsing |
| Service module | `springboot/fastbee-server` | TCP/serial related service capabilities |
