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:
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:
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:
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:
- Create a Modbus device configuration in Services → Modbus → Modbus TCP client page. For example:
- Create a request to write to a single or multiple registers:
- Press Test under Request configuration testing section
-
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,