TRB 140 and PLC data exchange

hello,
I’m new with the teltonika ecosystem and I am working on a project in which I’m using a delta DVP-12SE11T controller that is communicating with the TRB140 over modus TCP
I set the PLC and the TRB140 in order to control the system via SMS and I could receive some events in my phone ( using event reporting and alarm configuration in the modus section) but there is two things I couldn’t know how to set them:

  1. How to get a register value from the PLC and send it via SMS
  2. How to set a specific value to the PLC

I’ll be grateful if someone could help me with it

Thanks

Hello,

The following was tested on TRB1_R_00.07.11.3 firmware version:

  • How to get a register value from the PLC and send it via SMS
    With the help of a script.

Below is an example of a CLI/SSH command that reads the value of one register of the register number 1:

ubus call modbus_client.rpc tcp.test '{"id":1,"timeout":10,"function":3,"first_reg":1,"reg_count":"1","data_type":"16bit_int_hi_first","no_brackets":1,"ip":"192.168.2.206","port":"502","delay":0}' | jsonfilter -e '@.result'

You will need to modify some of the following values according to your setup:

  • id - Modbus server ID number
  • timeout - Time period for waiting of the TCP device response in seconds
  • function - Modbus function code for the request
  • first_reg - First register/ start register number
  • reg_count - Register count/values
  • data_type - Data type that will be used for storing the response data (only for read requests). Some available data types:
    • “8bit_int”
    • “8bit_uint”
    • “16bit_int_hi_first”
    • “16bit_uint_hi_first”
    • “16bit_int_low_first”
    • “16bit_uint_low_first”
    • “32bit_int1234”
    • “32bit_uint1234”
    • “32bit_float1234”
    • “32bit_float4321”
    • “hex”
    • “ascii”
  • no_brackets - Adds/removes the starting and ending brackets from the request (only for read requests)
  • ip - IP address or hostname of the server device
  • port - Server device Port
  • delay - Wait in milliseconds after connection initialization

For example:
image

You can then store this value in some variable and send via SMS using the command:
gsmctl -S -s "<phone number> $register_value"

In case the number to send the value to changes, I’ll keep it as an argument that can be provided outside the script.

To create the script, log into the router via SSH and execute the command:

  • vi /etc/script.sh

The script itself could look as follows:

#!/bin/sh

number=$1

value=$(ubus call modbus_client.rpc tcp.test '{"id":1,"timeout":10,"function":3,"first_reg":1,"reg_count":"1","data_type":"16bit_int_hi_first","no_brackets":1,"ip":"192.168.2.206","port":"502","delay":0}' | jsonfilter -e '@.result')

# Send the data via SMS 
gsmctl -S -s "$number $value"

You can store it in some location in the router, for example, /etc directory.
Once the script is created do not forget to make it executable with the following command:

  • chmod +x /etc/script.sh

For testing, if you have your modbus server connected to the device and can read wanted registers, execute the script with:

  • sh /etc/script.sh <phone number>
    • Replace “<phone number” with proper number

Now that you have the script, how frequently do you want to execute it? You can:

  • Execute it manually by logging via SSH whenever you need to know the value;

  • Execute the script periodically:

  • How to set a specific value to the PLC

    • You can do it via WebUI:

    • Then there is the CLI approach. Simply modify the members function (6 for single, 16 for multiple registers), and reg_count (which in this case represents the value to be written to the register(s)) according to your needs in the command below:

      • ubus call modbus_client.rpc tcp.test '{"id":1,"timeout":10,"function":6,"first_reg":1,"reg_count":"15","data_type":"16bit_int_hi_first","no_brackets":1,"ip":"192.168.2.206","port":"502","delay":0}'

Best regards,

1 Like

Hello,

Thanks for your interaction @Sigismundus

So to log into the router via SSH to create the script do I need to use the prompt command to do so?

And by the way the firmware version is TRB1_R_00.07.06.4

Hello,

What do you mean by prompt command?

You can find how to log into the router’s SSH here: Command Line Interfaces - Teltonika Networks Wiki

There is also a more detailed tutorial here on how to create a script: User Scripts examples - Teltonika Networks Wiki

Best regards,

1 Like

hi,

i meant whether i work on the webui or an other terminal like you mentioned in this link " Command Line Interfaces - Teltonika Networks Wiki "

thanks a lot for helping

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.