ETport templates with multiple devices

There are two ways to use the template system:

  1. Use one template per device and set the “post_addr_X” and “post_file_X” advanced settings (under System > Advanced Configuration Page -- screenshot below) to the address of the respective devices (the “post_addr_X” variable is a hexadecimal string field corresponding to the two digit hex address of each Modbus device address that is to use that template).

  2. Combine all devices into one template and address each device/variable within the template by using the $X function (where “X” is the Modbus device address in question).  For this to work, the “post_addr_X” setting must be set to all zeros (“0000000000000000000000000000000000000000000000000000000000000000”)

Normally, the template will fail if any device does not respond, or if it responds with an exception (ie: illegal register, etc).  However, by using the nofail() function, it will prevent the template from failing, and will fill the variable with a text string which indicates the error.

It is possible to format the template in any format you wish (it is a textfile), however we find that JSON is most common.  There are some function parameters which assist to format the output as valid JSON.  There is no way to skip reading a missing device, because the only way to identify a missing device is to try to read it in the first place.

Below is an example template and response showing a valid response, exception response, and missing device response, all in one template.  (reminder, post_addr_X must be set to all zeros for this to work).

 Template:

$nofail(){
   "device1_Good_Data":        $1.ushort(0x85,  format="json")
   "device1_Invalid_Register": $1.ushort(0xFFF, format="json")
   "device2_Missing":          $2.ushort(0x50,  format="json")
}

 Resulting Post:

{
   "device1_Good_Data":        12345
   "device1_Invalid_Register": "error: Reading 0fff - 0fff Modbus exception response received: Illegal Data Address (ELKOR_ERROR_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS) [ ModbusExceptionResponseException ]"
   "device2_Missing":          "error: No MB Response [ TimeoutException ]"
}


Note: you can use a utility like PostMan (https://www.postman.com/) to the /template URL of the ETport (ie:
http://192.168.1.100/template) to send the “template” and view the response (screenshot attached).  This speeds up development.