11399 questions

13582 answers

21280 comments

31964 members

+1 vote
37 views 0 comments
by
We have a requirement to aquire modbus data via modbus master every 15 minutes but we need the 15 minute intervals to align such that the readings are 00:00:00, 00:15:00, 00:30:00, etc. is there a mechanism to have the modbus_tcp_master to align itself to these intervals instead of using the device start time as the first interval?

1 Answer

0 votes
by

Hello,

Thank you for contacting Teltonika Crowd Support.

An option would be to ensure that you synchronize clocks on all devices that are participating in the Modbus data acquisition process using Network Time Protocol (NTP), then use crontab to allow commands (Modbus data acquisition commands) to be automatically executed at the predefined period.

Below are configuration steps that could be used to achieve the desired outcome:

1. Configure the Teltonika device as a NTP Client and ensure that the NTP Server used by the Teltonika device is the same as the one configured on all the other devices that are participating in the Modbus data acquisition process. Refer to the link below for further details:
https://wiki.teltonika-networks.com/view/RUT955_NTP

2. Configure crontab file using "crontab -e" command to add commands to be executed at required times. Refer to the link below for further details: https://wiki.teltonika-networks.com/view/Crontabs

For example an entry to restart the NTP client could be executed every day at 12:00am by adding the following entry to the crontab file
0 0 * * * /etc/init.d/ntpclient restart (Restart NTP Client to force the router's clock to synchronize with the clock of predefined NTP server)

Additional entries could be added to the crontab file to read Modbus TCP slave data.
The command below could be used to read Modbus TCP slave data.
# modbus_tcp_test IP PORT TIMEOUT SLAVE_ID FUNCTION REG_ADDRESS REG_COUNT/VALUE(S) DATA_TYPE NO_BRACKETS

Where IP - Modbus TCP Slave IP address, PORT - Modbus TCP Slave Port, TIMEOUT - Maximum response wait time, SLAVE_ID - Modbus Slave ID, FUNCTION - Modbus function used for the request, REG_ADDRESS - Modbus Slave's register address, REG_COUNT/VALUE - Number of registers to be read from/ the value of to written into the register, DATA_TYPE - Defines how data will be stored and NO_BRACKETS - Removes the starting and ending brackets from the request (only for read requests).

To write the read Modbus TCP slave data to a specific file, use the command below
#echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt"

Crontab could be used to ensure that the above command is executed periodically, allowing automatic Modbus TCP Slave data to be read from a Modbus TCP Slave and written to a file. In the example command provided below, the data will be written every 15 minutes. To add the crontab entry use the "crontab -e" command and add the entry.
*/15* * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file every 15 minutes.

Crontab could also be used so that the router should read data every X:15++ minute, and lines should be similar to these
0 0 * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file at 12:00PM
15 0 * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file at 12:15PM
30 0 * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file at 12:30PM
45 0 * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file at 12:45PM
0 13 * * *  echo "`modbus_tcp_test 192.168.1.106 502 5 1 3 1 5 16bit_int_hi_first 0`" | tee -a  /tmp/modbus_tcp.txt >/dev/null 2>&1 #746c74 - reads Modbus data and writes to file at 13:00PM

The above crontab entries illustrate reading Modbus data from a Modbus TCP Slave every 15 minutes, everyday starting at 12:00pm and ending at 1:00pm. This could be adjusted to meet your requirements.

You could set the command execution period to the desired one, by making use of the tool provided in the link below:
https://crontab.cronhub.io/

Warm regards

Wesley

Best answer