When an IOT system lacks a realtime clock, it's easy to set the date on the system from PLC registers.

In this case we've configured a ModbusArray:

To read the date/time. The registers 1-7 contain the year, month, day, hours, minutes, seconds, and milliseconds.

Extract the values from the registers and execute the “date” command to set the system time. There are some more details on using that, and problems you may encounter on this page

// set the date.  To do this, call:
// sudo date -s '2023-01-12 12:03:21.706'
 
// the incoming array is one value per register as:
// yyyy, MM, dd, HH, mm, ss, SSS
year = incomingValue.get(0).getIntValue();
month = incomingValue.get(1).getIntValue();
day = incomingValue.get(2).getIntValue();
hour = incomingValue.get(3).getIntValue();
minute = incomingValue.get(4).getIntValue();
second = incomingValue.get(5).getIntValue();
millis = incomingValue.get(6).getIntValue();
 
var dateString = java.lang.String.format("%4d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis);
 
var Runtime = Java.type("java.lang.Runtime");
var Process = Java.type("java.lang.Process");
 //This runs the external program
//Consider running this in its own Thread
 
        try
        {
   
            // Running the command
            run  = Runtime.getRuntime();
            proc = run.exec(["sudo", "date", "-s", dateString]); //set the date
        }  catch (e) {             
           print("error in script: \n"+e.stack);
        }
 
output="finished";
  • setting_the_system_date_from_plc_registers.txt
  • Last modified: 2023/01/12 14:29
  • by wikiadmin