> 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/communications/dual-failover-comms.md).

# Dual failover comms

Use Ethernet for DATRAN, with a radio on a serial port taking over automatically if Ethernet fails.

An XL4 Plus can talk to DATRAN over Ethernet and over a serial port. With dual failover comms, DATRAN uses Ethernet normally and switches to the serial link, usually a radio, if Ethernet stops working. A DATRAN script makes the switch, and switches back later.

{% hint style="info" %}
**Applies to:** XL4 Plus firmware v7.x with DATRAN VI. Typically the Ethernet port goes to a cable modem (ADSL or fibre) or a 3G/4G Wi-Fi modem, and the serial port to a radio modem.
{% endhint %}

![Dual comms: the XL4 Plus connects to DATRAN VI by Ethernet, and by RS232 through a pair of Tait TM8xxx radios](https://3621580088-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5hMAUl7E1cWeN5Rghg0%2Fuploads%2FhHURsG6xk9LyPRL1DTWk%2Fdiagram-dual-failover.png?alt=media)

## 1. Set up the Ethernet port

In QTech Workbench, set up the Ethernet port and the DATRAN link: the RTU address used in DATRAN (for example 13) and the TCP port DATRAN uses (for example 4001). See [Configure](https://help.qtech.co.nz/datran/datran-telemetry/datran-xl4-plus-rtu/configure.md).

## 2. Set up the serial port

Set up the serial port for the radio, for example a Tait radio. Give it the **same** RTU address as the Ethernet port. Accept the defaults unless you have a reason to change them.

## 3. Set up DATRAN

In the site's comms object, assign the site number to both the Ethernet and the serial channel of the XL4 Plus.

## 4. Add the failover script

The script goes in the DATRAN TBD file, where your system keeps general-purpose scripts. The location varies from system to system. You add the script once, then add a template of it to each site that needs dual failover comms.

1. At the **Scripts** node press the **Insert** key.
2. At **Enter the name of the node…** enter `DualComms`, press **OK**.
3. With **DualComms** node selected, press **Insert** again, select the **Scripting** tab, select **\_script\_**.
4. Paste the script below into it.
5. Select the site that is to have dual failover comms enabled.
6. Press the **Insert** key, select the **Scripting** tab, select **\_template\_**.
7. In the **Script** drop down box either type `DualComms` or select it from the drop down box list. Click **Apply Template**.

The template creates the parameters with default values. Change them in the script to change every site, or at a site's **\_template\_** node to change just that site.

| Parameter        | What it is                                                                                      | Default  |
| ---------------- | ----------------------------------------------------------------------------------------------- | -------- |
| Channel Ethernet | Name of the TCP RTU Channel comms object                                                        | Ethernet |
| Channel Serial   | Name of the serial channel comms object                                                         | Serial   |
| Poll Ethernet    | Poll interval used on Ethernet                                                                  | 1 min    |
| Poll Serial      | Poll interval used on serial                                                                    | 15 min   |
| Retry Ethernet   | How often we will try to switch back to running on Ethernet (if running on Serial). 0=never try | 2 hours  |
| Timeout Ethernet | Time delay before switching to Serial if no data being received                                 | 5 min    |
| Timeout Serial   | Time delay before switching to Ethernet if no data being received                               | 45 min   |

The template also shows: **Active** (updates each time the script runs), **ChangeOverTime** (when it last switched), **OnEthernet**, **OnSerial**, **RxTime** (when data was last received) and **TimeDiff**.

### The script

```
// Enable Dual Comms on an Ethernet channel and a Serial Channel.
// The script initialises to Ethernet on startup, it then checks for the comms object rxtime changing.
// If it exceeds a parameter it switches to the Serial port and adjusts the polling rate.
// When running on Serial it will periodically retry going back to the Ethernet channel
// (this assumes you want this to be the default channel).

// Initialise to using the ethernet port, setup sensible values if not existing
OnStart OnDo:
If Not Exists ([_template_\Poll Ethernet])
{
[_template_\Poll Ethernet] := MakeTimeSpan(1 Min);
}
If Not Exists ([_template_\Poll Serial])
{
[_template_\Poll Serial] := MakeTimeSpan(15 Min);
}
If Not Exists ([_template_\Channel Ethernet])
{
[_template_\Channel Ethernet] := "Ethernet" ;
}
If Not Exists ([_template_\Channel Serial])
{
[_template_\Channel Serial] := "Serial" ;
}
If Not Exists ([_template_\Retry Ethernet])
{
[_template_\Retry Ethernet] := MakeTimeSpan(2 Hr) ;
}
If Not Exists ([_template_\Timeout Ethernet])
{
[_template_\Timeout Ethernet] := MakeTimeSpan(5 Min) ;
}
If Not Exists ([_template_\Timeout Serial])
{
[_template_\Timeout Serial] := MakeTimeSpan(45 Min) ;
}
[_template_\Rxtime] := [_Comms_Object_\RxTime];
[_template_\OnSerial] := 0;
[_template_\OnEthernet] := 1;
[_Comms_Object_\Poll Interval] := [_template_\Poll Ethernet];
[_Comms_Object_\Channel] := [_template_\Channel Ethernet];
[_Comms_Object_] := Null;
[_template_\Active] := 0;
Poll 30 Sec;

OnPoll:
[_template_\Active] := Second(Time.Local);
[_template_\Rxtime] := [_Comms_Object_\RxTime];
[_template_\TimeDiff] := (Time.Local - [_template_\Rxtime])/1000;
If ([_template_\OnEthernet])
{
If ((Time.Local - [_template_\Rxtime]) > [_template_\Timeout Ethernet])
{
[_template_\ChangeOverTime] := Time.Local;
[_template_\OnSerial] := 1;
[_template_\OnEthernet] := 0;
[_Comms_Object_\Poll Interval] := [_template_\Poll Serial];
[_Comms_Object_\Channel] := [_template_\Channel Serial];
[_Comms_Object_] := Null;
Poll 60 Sec; //give time for serial comms object to reset
}
Else
{
Poll 1 Sec;
}
}
ElseIf ([_template_\OnSerial])
{
If ((Time.Local - [_template_\Rxtime]) > [_template_\Timeout Serial])
{
[_template_\OnSerial] := 0;
[_template_\OnEthernet] := 1;
[_Comms_Object_\Poll Interval] := [_template_\Poll Ethernet];
[_Comms_Object_\Channel] := [_template_\Channel Ethernet];
[_Comms_Object_] := Null;
Poll 30 Sec; //give time for ethernet comms object to reset
}
Else
{
Poll 1 Sec;
}
}
// When on Serial, periodically try resetting back to ethernet
If ([_template_\OnSerial] And [_template_\Retry Ethernet] != 0)
{
If ((Time.Local - [_template_\ChangeOverTime]) > [_template_\Retry Ethernet])
{
[_template_\OnSerial] := 0;
[_template_\OnEthernet] := 1;
[_Comms_Object_\Poll Interval] := [_template_\Poll Ethernet];
[_Comms_Object_\Channel] := [_template_\Channel Ethernet];
[_Comms_Object_] := Null;
Poll 30 Sec; //give time for comms object to reset
}
}
```
