# Modbus Tool

The Modbus Tool is a diagnostic utility for sending arbitrary Modbus commands to connected devices. It is useful for testing device communication and verifying register addresses.

## Web Interface

Access the tool via the web interface at:
`http://<device-ip>/modbus`

The interface allows you to:
- Select a target node address
- Choose the Modbus function (Read/Write)
- Specify register/coil addresses and quantities
- View the raw response data

> **Note:** The Modbus Tool automatically initializes nodes on first use. You do not need to pre-configure a node in the device settings to communicate with it — just enter the address and the tool handles the rest.

## API Endpoint

You can also use the Modbus Tool programmatically via the API.

```
POST /api/modbus/exec
Content-Type: application/json
```

### Request Body

The request structure depends on whether you are reading or writing.

#### Read Request

```json
{
  "node": 1,
  "action": "read",
  "type": 1,
  "channel": 100,
  "qty": 5
}
```

| Field | Type | Description |
|-------|------|-------------|
| `node` | integer | Modbus node address (1-64) |
| `action` | string | Must be `"read"` |
| `type` | integer | Register type (see table below) |
| `channel` | integer | Starting register address |
| `qty` | integer | Number of registers to read |

#### Write Request

```json
{
  "node": 1,
  "action": "write",
  "type": 0,
  "channel": 100,
  "value": 1234
}
```

| Field | Type | Description |
|-------|------|-------------|
| `node` | integer | Modbus node address (1-64) |
| `action` | string | Must be `"write"` |
| `type` | integer | Register type (see table below) |
| `channel` | integer | Register address to write to |
| `value` | integer | Value to write |

#### Register Types

| ID | Description | Modbus Function |
|----|-------------|-----------------|
| `0` | Holding Register | Read: 0x03, Write: 0x06 |
| `1` | Input Register | Read: 0x04 |
| `2` | Coil Status | Read: 0x01, Write: 0x05 |
| `3` | Discrete Input | Read: 0x02 |

### Response

The API returns a JSON object with the operation result.

#### Success Response (Read)

```json
{
  "status": "success",
  "data": [123, 456, 789, 0, 0]
}
```

#### Success Response (Write)

```json
{
  "status": "success"
}
```

#### Error Response

```json
{
  "status": "error",
  "message": "Modbus error: 0xE2",
  "code": 226
}
```

Common Modbus error codes (decimal):
- `224` (0xE0): Invalid Slave ID
- `225` (0xE1): Invalid Function
- `226` (0xE2): Response Timeout
- `227` (0xE3): Invalid CRC
