An intercept route lets the routing table take over the requests that a gateway receives, instead of following the value of an address. The route can answer the request itself, forward it to another gateway and return that gateway's answer, or let the gateway answer normally. Today only the Modbus driver supports interception.
Typical uses:
This page assumes that you know the routing table and the route parameters: see Routes. The functions used below follow the same rules as the routing functions of section 2.6 of the Routes page.
Intercept routes are an advanced feature: a wrong function can make a Modbus slave answer with errors or not at all. Test on a gateway that is not in production, and keep the functions short.
| Value route | Intercept route | |
|---|---|---|
| Executed when | the source address emits a new value or status | a client sends a request to the source gateway (read or write) |
| First argument of the Source value function | the routed value | a request object (section 4) |
| Result | a value written to the destination address | an answer returned to the client that sent the request |
| Direction | right, left or bidirectional | always right |
| Trigger column | value, status, refresh, gateway | request types: all, read, write, Modbus function codes (section 3) |
Which Modbus gateways are concerned:
The route form does not offer the intercept type. Create the route in the routing table, then change its type:
all.Changing the type back to value restores the default triggers (value, status, refresh, gateway).
For an intercept route, the Trigger cell lists the request types that the route takes over. Several choices can be ticked; all clears the others.
| Choice | Requests intercepted |
|---|---|
all (default) |
every request |
read |
function codes 1 to 4 |
write |
function codes 5, 6, 15 and 16 |
readCoils |
FC 1, coils (%MX) |
readDiscreteInputs |
FC 2, discrete inputs (%QX) |
readHoldingRegisters |
FC 3, holding registers (%M) |
readInputRegisters |
FC 4, input registers (%Q) |
writeSingleCoil |
FC 5 |
writeSingleRegister |
FC 6 |
writeMultipleCoils |
FC 15 |
writeMultipleRegisters |
FC 16 |
%M100, a list, a regular expression between / … /). The addresses must exist in the address table of the gateway. A request that covers several registers (for example a read of 10 holding registers) is intercepted as soon as one of its registers matches.gateway in Source address (with the source gateway set).The address names are computed from the request with the register offset of the gateway, so they match the names shown in the address table (%M, %MX, %Q, %QX).
The Source value function is optional. It receives the request before it is forwarded and decides what to do with it:
function filterWrites(request, src, dst, options){
// request.operation : "read" or "write"
// request.value : the values to write (write requests)
// request.modbus : { unitId, first, number, functionCode } of the original request
if(request.operation == "write" && request.modbus.unitId == 5){
return null // this route does not answer: the gateway processes the request normally
}
return request // continue: forward the request (section 5)
}
The request object prepared by the route:
| Property | Content |
|---|---|
operation |
read or write. |
value |
The values of a write request. |
address |
The address(es) to use at the destination: the destination address of the route when it is set, otherwise the address(es) of the request. |
sourceAddress |
The address(es) of the original request, as a list of names. |
gateway_id |
The id of the destination gateway, when the route has one. |
timeout |
Time allowed to the destination, in milliseconds (4000 by default). |
modbus |
The raw Modbus fields of the request: unitId, first (register number on the wire), number, functionCode. |
originalRequest |
The request as received, unchanged. |
The fourth argument (options) gives options.request (the original request), options.route (the route, read-only) and options.context (an object kept between executions of the route).
What the function may return:
| Returned value | Effect |
|---|---|
null or nothing |
This route does not answer. If no other intercept route answers, the gateway processes the request normally (local registers of the slave). |
An object with an operation property |
A request, forwarded as described in section 5. You can change address, gateway_id, value or the modbus fields (for example request.modbus.unitId) before returning it. |
Any other value (number, array, object without operation) |
A value to write at the destination, as if the request were a write of this value. Add isValue: true to an object that has an operation property to have it treated as a value. |
gateway_id in the request it returns.How the request is forwarded:
destination driver not ready).The Destination value function is optional. It receives the answer of the destination, or a list of answers when several are received. Each answer is an object:
| Property | Content |
|---|---|
value |
The values read (read requests). |
written |
true when a write succeeded. |
error |
The error returned by the destination. |
address, gateway_id |
Where the answer comes from. |
_request |
The request that was forwarded. |
It returns the answer to send back to the client:
{ value : [ … ] } for a read: one number per register (or per coil) requested;{ written : true } for a write;ILLEGAL_FUNCTION, ILLEGAL_DATA_ADDRESS, ILLEGAL_DATA_VALUE, SLAVE_DEVICE_FAILURE, SLAVE_BUSY_DEVICE, GATEWAY_PATH_UNAVAILABLE, GATEWAY_TARGET_TIMEOUT, …) is returned as that exception; any other text is returned as SLAVE_DEVICE_FAILURE.Without a Destination value function, the first answer is returned as it is.
When the Modbus slave answers a client with values coming from a route, it also stores them in its local registers: the address table of the slave shows the last values served.
| Parameter | Behaviour on an intercept route |
|---|---|
| Disable delay | Works as a rate limit, per address: a request received less than Disable delay after the previous one on the same address gets no answer from the route, so the gateway processes it normally. |
nullMode |
No effect. |
options.onWrite(), options.disabled |
Not available in the functions of an intercept route. |
| Active | An inactive intercept route does not intercept anything. |
A SCADA reads the RTU devices through the gateway, with their real answers and their real errors.
| Column | Value |
|---|---|
| Type | intercept |
| Trigger | all |
| Source gateway | scada |
| Source address | gateway |
| Dest. Gateway | rtu |
| Dest. Address | (empty) |
| Source value / Destination value | (empty) |
Each request received on port 502 is sent to the RS-485 line with the same unit ID and the same registers, and the answer of the device is returned to the SCADA. When the RTU master reports a Modbus exception by its name (for example ILLEGAL_DATA_ADDRESS), the SCADA receives the same exception; other errors (timeout, device offline) are returned as SLAVE_DEVICE_FAILURE. When the RTU gateway is not connected, the SCADA receives SLAVE_DEVICE_FAILURE.
If no answer comes back within the internal delay of the slave (about 55 seconds), the slave answers from its local registers.
The register %M100 of the slave gateway scada returns ten times the value of the internal address $setpoint, at the moment it is read.
| Column | Value |
|---|---|
| Type | intercept |
| Trigger | readHoldingRegisters |
| Source gateway | scada |
| Source address | %M100 (the address must exist in the table of scada) |
| Dest. Gateway | (empty) |
| Destination value | the function below |
function answerSetpoint(responses, src, dst, options){
var a = this.getAddress("$setpoint")
if(!a || a.value === null || a.value === undefined){
return "SLAVE_DEVICE_FAILURE" // no value yet: Modbus exception 04
}
var n = options.request.modbus.number // number of registers requested
var values = []
for(var i = 0; i < n; i++) values.push(i == 0 ? Math.round(a.value * 10) : 0)
return { value : values }
}
There is no destination gateway, so nothing is forwarded: the function builds the answer. this is the gateways API, as in every routing function.
| Symptom | What to check |
|---|---|
| The gateway answers as if the route did not exist | The route is inactive, the Trigger does not include this request type, the source address does not exist in the table of the gateway (use the gateway keyword), or a function returned null. |
The client receives SLAVE_DEVICE_FAILURE |
The destination gateway is not connected, a function threw an error, or a function returned a text that is not a Modbus exception name. |
| Writes from the client do not reach the destination | Check the destination address, and that the destination driver accepts writes on it. A write to an input register (%Q) or a discrete input (%QX) of a Modbus device is refused. |
For developers: every step of an intercept route emits the gateways event routeIntercept with the route id, the step (request, forward, response or error) and its data. A client of the gateways API can subscribe to it to follow the requests. The general method for routes is on Troubleshooting routes.
The functions of an intercept route run on the device with the rights of the gateway software, like every routing function. Only paste code from a source you trust.
this in the functions, events.