> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/modbus/modbus-master.md).

# Set up as a Modbus master

Read and write a PLC or other Modbus device from the XL4 Plus, over Modbus TCP or serial.

As a Modbus master (client), the XL4 Plus polls Modbus devices such as PLCs (programmable logic controllers), flow meters and analysers, and puts their values in its real I/O. DATRAN can then read and write them through the RTU. You set it up in QTech Workbench.

{% hint style="info" %}
**Applies to:** XL4 Plus firmware v7.x with Modbus licensed (fully featured units). This example uses Modbus TCP over Ethernet.
{% endhint %}

![The XL4 Plus as a Modbus client (master) on Ethernet, talking to a PLC Modbus server (slave), and to DATRAN VI](https://3621580088-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5hMAUl7E1cWeN5Rghg0%2Fuploads%2FUZLbBDNovPGlC4LPiHhE%2Fdiagram-modbus-master-tcp.png?alt=media)

## 1. Set up the Ethernet port

Set up the Ethernet port and the DATRAN link. In this example DATRAN also uses Ethernet. See [Configure](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/configure.md).

## 2. Add the Modbus slave

1. Select **Modbus Master**, right click, **Add Modbus Slave (Ethernet)**.
2. Configure the Modbus Slave's **Uid**.
3. Configure the Modbus Slave IP Address.
4. Leave the default TCP port as **502** unless there is a good reason otherwise.

## 3. Allocate space for the slave's data

Under **Telemetry Point Count**, set how many points of each type to allocate. This makes a slot (the Modbus range) in the RTU's data table for the slave's values. Allocate more than you need, so you can add actions later.

| Point count      | Modbus data in the slave        | The master can |
| ---------------- | ------------------------------- | -------------- |
| Digital Inputs   | Discrete inputs (10001 range)   | Read           |
| Digital Outputs  | Coils (00001 range)             | Read and write |
| Analogue Inputs  | Input registers (30001 range)   | Read           |
| Analogue Outputs | Holding registers (40001 range) | Read and write |

## 4. Add read and write actions

Actions set which data the RTU actually reads and writes. Often you only want a few values that aren't in one block, so use several actions. The data from all the actions maps into a continuous block in the RTU's data table.

1. Click on **Add Action**, by default you get a **Read Coils** action. This can be changed by clicking on the **Command** and selecting the action you require.
2. Select an **Offset**, this is the offset from the start of the Modbus range.
3. Select a **Quantity**, this is the number of contiguous values to read/write.

Note that the read/write actions must never exceed the number allocated in the Telemetry Point Count.

For example, the RTU could write the slave's coils 00001–00016, and read its discrete inputs 10001–10008 and 10024–10025.

## How the values reach DATRAN

Values the RTU reads go into its **real** points; for example, discrete inputs 10001–10008 go into real digital inputs. Values it writes come from its real outputs.

| Slave (PLC)       | Direction | Master (XL4 Plus)                              |
| ----------------- | --------- | ---------------------------------------------- |
| Coils             | Read      | Real Digital Output (Read Coils)               |
| Coils             | Write     | Real Digital Output (Write Coils)              |
| Discrete inputs   | Read      | Real Digital Inputs (Read discrete inputs)     |
| Holding registers | Read      | Real Analogue Outputs (Read holding Register)  |
| Holding registers | Write     | Real Analogue Outputs (Write holding Register) |
| Input registers   | Read      | Real Analogue Inputs (Read Input Registers)    |

* With no DLP, or a DLP without `telinp` and `telout`, the RTU copies real inputs to telemetry inputs (RAI to TAI) and telemetry outputs to real outputs (TAO to RAO) automatically. DATRAN sees the values with no extra work. A real output DATRAN hasn't set is 0.
* With a DLP that uses `telinp` and `telout`, the DLP must copy the values between real and telemetry points itself.

For example, DATRAN sends a value for analogue output 3. It goes to TAOUT3 in the RTU. The DLP copies it to RAOUT3, which the RTU writes to the slave's holding register 40001.

For more on real and telemetry points, see [Modbus](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/modbus.md).

## Check it's working

In Workbench diagnostics, turn on **DBG\_MB\_CLIENT** (Ethernet) or **DBG\_MB\_MASTER** (serial) to see the commands and any exceptions. A serial Modbus master failure shows as 8 flashes on the ER light; a Modbus TCP client failure shows as 10.
