> 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-rtu/modbus-and-ethernet.md).

# Modbus and Ethernet

Set up the XL4 RTU as a Modbus master or slave over RS232 or Ethernet, and connect its Ethernet port.

The DATRAN XL4 RTU Modbus interface is a powerful feature that opens up a huge range of possibilities to interface many types of third party equipment, or “smart devices” into new or existing DATRAN RTU installations. Typical uses are to integrate equipment such as PLCs, chlorine treatment plants, touch screen displays, motor drives, UV treatment reactors and pump station controllers. Any of these devices can be connected to a DATRAN XL4 RTU to provide integration opportunities via RS232 or Ethernet connections. The DATRAN RTU is capable of either Modbus Master or Modbus Slave operation. Both Modbus master and slave support are optional extras that require an unlock code, purchased from QTech.

{% hint style="info" %}
**Applies to:** DATRAN XL4 RTU, firmware v6.04 or later recommended, and QTech Workbench v1.6 or later. From the application notes DATRAN XL4 RTU Modbus Interface v1.2 and DATRAN XL4 RTU Ethernet Interface v1. For the XL4 Plus, see [Modbus](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/modbus.md).
{% endhint %}

## Turn on Modbus

Typically XL4 RTUs are ordered with the MODBUS option specified. Existing XL4 RTU can however have MODBUS enabled in the field. Obtain an “Option Code” from QTech. The Option Code is keyed against the RTUs serial number, so have that ready. Enter the Option Code in the **ESN Options** section of Workbench.

## Modbus support

| Item              | XL4 RTU                                                                                                                                                                                             |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Ports             | RS232 & Ethernet. The RS485 port on the XL4 RTU cannot be used for MODBUS.                                                                                                                          |
| Protocols         | Modbus RTU, Modbus ASCII, Modbus TCP                                                                                                                                                                |
| Modbus TCP port   | Modbus TCP default TCP port is 502 but this can be reconfigured if needed.                                                                                                                          |
| RS232 baud rate   | Up to 19,200 bps                                                                                                                                                                                    |
| Port settings     | The port settings should specify 8 data bits and either parity with one stop bit or no parity and two stop bits. Both the XL4 and the Modbus device it is connected to must have the same settings. |
| Broadcast address | The XL4 does not accept commands which are sent to the MODBUS broadcast address of 255.                                                                                                             |
| Devices           | The XL4 RTU can only have one connected MODBUS device of each type (master or slave) on one physical port at a time.                                                                                |

For example if the RTU is configured as a MODBUS slave using the Ethernet port, it can only have one MODBUS master connected to that port, and the RS232 ports then can’t be configured to have a different MODBUS Master connected.

{% hint style="warning" %}
**Compatibility note: port settings and parity.** As of XL4 firmware v6.08, a no parity and one stop bit option was introduced. We don’t recommend this being used as it could lead to long term compatibility issues when devices are replaced. We recommend using parity and one stop bit or no parity and two stop bits. Previous Q03 powered RTUs (QRTU and eXcel) allowed a setting of no parity and one stop bit (non-compliant). When doing a Q03 (QRTU and eXcel) to XL4 upgrade it is important to check the port settings. This may involve approaching the PLC/Modbus supplier or system integrator.
{% endhint %}

### Function codes

| Function                 | Code | RTU as slave | RTU as master |
| ------------------------ | ---- | ------------ | ------------- |
| Read coils               | 0x01 | Yes          |               |
| Read discrete inputs     | 0x02 | Yes          | Yes           |
| Read holding registers   | 0x03 | Yes          |               |
| Read input registers     | 0x04 | Yes          | Yes           |
| Write single coil        | 0x05 | Yes          |               |
| Write single register    | 0x06 | Yes          |               |
| Write multiple coils     | 0x0F | Yes          | Yes           |
| Write multiple registers | 0x10 | Yes          | Yes           |

### Data mapping

Note that the mapping is different depending on whether the RTU is a MODBUS Master or a MODBUS Slave.

| Modbus data type  | Modbus addresses | Alternative names | XL4 as slave | XL4 as master |
| ----------------- | ---------------- | ----------------- | ------------ | ------------- |
| Coils             | 00001–00250      | %M0000–%M0249     | RDI          | RDO           |
| Discrete inputs   | 10001–10250      | %I0000–%I0249     | RDO          | RDI           |
| Input registers   | 30001–30250      | %IW0000–%IW0249   | RAO          | RAI           |
| Holding registers | 40001–40250      | %MW0000–%MW0249   | RAI          | RAO           |

RDI – real digital input, RDO – real digital output, RAI – real analogue input (word), RAO – real analogue output (word). The storage locations of the 4 data blocks in the RTU (RDI, RDO, RAI, and RAO) are not overlapping. Each block is accessed with different MODBUS commands.

* **XL4 as master:** if the XL4 RTU is configured as a MODBUS Master, the data contained in the response to the READ DISCRETE INPUTS command will be copied by the XL4 RTU into its Real Digital Inputs memory. The data is then available to the DATRAN Base station as Telemetry Digital Inputs.
* **XL4 as slave:** if the XL4 RTU is configured as a MODBUS Slave, and it receives a READ DISCRETE INPUTS command, the response will contain the values of its Real Digital Outputs data. This is slightly non-intuitive, but is implemented this way to allow the RTU to “push” data values to the master via its DLP. Basically, the XL4 RTU sees the MODBUS Master as a virtual IPB expansion module, and normally IPB expansion modules are slaves of the RTU, but in this case the expansion module is actually a master.

## Set up the XL4 as a Modbus slave

### Over RS232

1. Select which physical port (A or B) is to be connected to the MODBUS Master device.
2. Click on the **Port Function** field and select **Modbus (RTU as Slave)**.
3. Edit the Baud Rate, Parity, Modbus Address, and Protocol values as needed.
4. Enter values for the number of I/O points. These allocate how much data the RTU will make available to the MODBUS Master device.

### Over Ethernet (Modbus TCP)

1. If you want to use MODBUS TCP communications, then edit the settings under the **Ethernet** port section instead of the RS232 port section. Most likely you will want to change the Modbus Protocol value to **TCP**. It is possible for the XL4 to use MODBUS RTU communications over TCP, but that would only be recommended for a scenario such as connecting via a RS232-TCP bridge to a device that only supported the Modbus RTU protocol.
2. Give the XL4 an IP Address, Net Mask, and Gateway in the section labelled **Onboard Ethernet**. Also specify which TCP Port the RTU should listen on (normally use port 502).

## Set up the XL4 as a Modbus master

### Over RS232

1. Select which physical port (A or B) is to be connected to the MODBUS Slave device(s).
2. Click on the **Port Function** field and select **Modbus (RTU as Master)**. Edit the Port settings values as needed. Select the MODBUS Protocol type as needed.
3. The **Modbus Poll Period** value specifies how often the RTU will poll each connected slave.
4. Next, go to the **Input/Output** section and click on the **Add Module** button. Enter the Modbus address of the slave in the **Unique Module Address** box and select the Module Type to be **QF5-Modbus Slave Loop**.
5. Now enter values for how much of each type of I/O the RTU is to communicate with the slave.

### Over Ethernet (Modbus TCP)

1. To configure the RTU as a MODBUS TCP Master, edit the Ethernet port's **Port Function** value to be **Modbus (RTU as Master)**.
2. Specify the RTU's IP Address, Net Mask, and Gateway values in the **Onboard Ethernet** section.
3. Specify the IP Address of the MODBUS Slave device, and which TCP Port to connect to (normally Port 502), in the **Remote Ethernet** section. In the current manifestation, only one slave can be configured (there is no way to specify more than one Remote IP Address).
4. Then add a QF5-Slave Loop device as described in the previous section.

## I/O mapping

The RTU will map I/O from the MODBUS interface as a continuation of its own I/O table. The order that I/O from different modules appears in the data table is determined by the DATRAN IPB address of each module, with the RTU's native (onboard) I/O always appearing first.

* When the RTU is configured as a MODBUS Slave, the MODBUS Interface is assigned a virtual IPB address of 242.
* When the RTU is a MODBUS master, each slave is mapped into the I/O table according to its MODBUS address.

Below is an example of how the digital inputs are mapped for a site consisting of an XL4 RTU with a Q23 expansion module. The XL4 + Q23 combination is configured as a MODBUS slave with 16 MODBUS digital inputs.

| I/O                    | DATRAN reference |
| ---------------------- | ---------------- |
| XL4 digital inputs 1–8 | RDI 1–8          |
| Q23 digital inputs 1–8 | RDI 9–16         |
| Modbus %M0000–%M0015   | RDI 17–32        |

## RS232 cable for Modbus

The XL4 RTU does not have a shielded RJ45 female connector as required by the specification, and the pin out of the XL4 RJ45 RS232 port differs from the specification. The pinout of an RS232 port cable suitable for the XL4 RTU is as shown below. QTech supply a 1m length RJ45-D9 serial cable with each RTU supplied for MODBUS comms. Given the diversity of usage in the field, we can’t guarantee the connectivity will suit your application. Please terminate the cable to suit, having confirmed with the manufacturer of the device you are interfacing to.

| RJ45 pin (XL4) | XL4 signal | D9 female pin | Device signal |
| -------------- | ---------- | ------------- | ------------- |
| 2              | RTS        | 8             | CTS           |
| 3              | CTS        | 7             | RTS           |
| 4              | GND        | 5             | GND           |
| 5              | RX         | 3             | TX            |
| 6              | TX         | 2             | RX            |
| 7              | DCD        | 3             | CD            |

All other pins are not connected.

## Ethernet port

The DATRAN XL4 RTU has an Ethernet interface as standard, to maximise connectivity options. The Ethernet interface can be used for communications from the XL4 RTU to the DATRAN VI SCADA base station via ADSL or any internet connection providing communication options from anywhere in the world. Ethernet also provides an alternative to RS232 for interfacing to other devices, typically PLCs, pump controllers or on-site HMIs. The communication protocols supported are DATRAN base comms (Qcomms), MODBUS (RTU as master or slave) and DNP3 (RTU as slave).

### Connect it

* **Through a LAN:** to connect the XL4 to a device by Ethernet normally both the XL4 and the device would be connected to a switch or hub by standard LAN cables. This is the normal LAN situation such as you would find when interfacing to a PLC in a water treatment plant.
* **Directly to one device:** if you want to directly connect a device that is not on a LAN, such as a local HMI you can use a patch cable. This will need to be a crossover cable as you will be connecting Data Terminal Equipment (DTE) to DTE. Note that crossover patch cables are the exception and may have distinctive “CROSSOVER” labels on them. It is recommended to purchase LAN cables rather than manufacture your own.

Ethernet cable has a maximum length of 100m but can be extended by using a repeating device such as a switch or hub. Currently the XL4 can only communicate with one device over the Ethernet port.

### Ethernet settings

In Workbench, open **Communications** > **Ethernet**.

| Setting            | What to enter                                                                                                                                                                                                                                                                                               |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| IP Address         | A static IP address that the RTU will respond to. Usually this is in the same subnet as the device you want to connect to.                                                                                                                                                                                  |
| Netmask            | An IPv4 mask used to divide an IP address into subnets. Typically the subnet will be 255.255.255.0                                                                                                                                                                                                          |
| Gateway            | The IP address of the gateway server for the network. Typically this does not need to be configured when talking to local devices. The gateway is the route to the WAN or internet, this will usually be a router or firewall server. If your RTU and device are on the same subnet this entry is not used. |
| TCP Port           | The TCP port that the RTU will respond to, must be in the range 1 to 65000.                                                                                                                                                                                                                                 |
| Remote IP Address  | If the RTU is operating as a Modbus Master then this is the IP address of the remote slave (currently this is limited to only one slave)                                                                                                                                                                    |
| Remote TCP Port    | If the RTU is Modbus Master, then this is the TCP port that the remote slave is listening on.                                                                                                                                                                                                               |
| Port Functionality | What the Ethernet port is being used for.                                                                                                                                                                                                                                                                   |

Note that when the Ethernet port is configured for Datran Serial Base Comms the Remote Ethernet settings are not used (they will be greyed out). This is because the RTU is a slave. When DATRAN polls the RTU, it will use the incoming IP address as the address to respond to for the poll and for any change of state (COS) transmissions.

Read this in conjunction with the Cellular IP data and Modbus user notes as there is important set up data in there which is not repeated here. See [Downloads](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-rtu/downloads.md).
