How to reboot PLC remotely

We shall discuss a few devices and Services automatically on this page.

//Rebooting PLC by code
PROGRAM MAIN
VAR
reStartPLC : BOOL := TRUE;
trigBoolean : BOOL:= FALSE;
myTimer : TON;
myTrig : R_TRIG;
fnReboot : NT_Reboot;
fnTCReStart : TC_Restart;
sysERR              : BOOL;
sysERRID            : UDINT;
bOldData            : BOOL;
myTestTrig          : R_TRIG;
logger              : DEBUG_LOGGER;
systemInfo          : PlcAppSystemInfo;
test1               : BOOL := TRUE;
test2               : BOOL := FALSE;
END_VAR
VAR PERSISTENT
longCounter : UDINT;
END_VAR

 

 

//Body of the MAIN
//bOldData:= TwinCAT_SystemInfoVarList._AppInfo.OldBootData;
myTestTrig(CLK:=test1);
IF myTestTrig.Q  THEN
longCounter:= 87654;
test1:= FALSE;
test2 := bOldData;
bOldData:= TwinCAT_SystemInfoVarList._AppInfo.OldBootData;
//bOldData:= _AppInfo.OldBootData;
END_IF
myTimer(IN:= TRUE, PT:= T#600S);
IF myTimer.Q  THEN
myTimer(IN:=FALSE);
trigBoolean := TRUE;
ELSE
trigBoolean := FALSE;
END_IF
myTrig(CLK:=trigBoolean);
IF myTrig.Q  THEN
longCounter := longCounter +1;
IF reStartPLC THEN
reStartPLC:= FALSE;
IF NOT fnTCReStart.BUSY THEN
fnTCReStart(NETID:='10.10.8.5.1.1',  RESTART:= trigBoolean,TMOUT:= T#5S);
    END_IF
sysERR:= fnTCReStart.ERR;
sysERRID:= fnTCReStart.ERRID;
ELSE
reStartPLC:= TRUE;
IF NOT fnReboot.BUSY THEN
fnReboot(NETID:='10.10.8.5.1.1', DELAY := 1, START:= trigBoolean,TMOUT:= T#5S);
    END_IF
sysERR:= fnReboot.ERR;
sysERRID:= fnReboot.ERRID;
END_IF
END_IF
fnReboot(START:= FALSE);
fnTCReStart(RESTART:= FALSE);

See Also

Rebooting by PC way

 

public bool RemoteShutdown(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.  
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information  
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //shutdown  
}
return true;
}
catch (Exception)
{
return false;
}
}

 

 

// Remote Reboot

public bool RemoteReboot(string userName, string password, string ip)
{
try
{
ConnectionOptions op = new ConnectionOptions();
op.Username = userName;
op.Password = password;
// Make a connection to a remote computer.  
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", op);
scope.Connect();
//Query system for Operating System information  
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("Reboot", null); //shutdown  
}
return true;
}
catch (Exception)
{
return false;
}
}

Raspberry Pi

=> Create a bat file with following content

@echo off

 

plink -batch -no-antispoof root@10.10.15.111 -pw root /sbin/reboot

 

#plink -batch root@10.10.15.111 -pw root /sbin/reboot # try without antispoof also

 

Execute the following code from a C# application

 

try
{
    System.Diagnostics.Process proc2 = new System.Diagnostics.Process();
    proc2.EnableRaisingEvents = false;
    proc2.StartInfo.FileName = @"C:\Test\RebootPi\bin\Debug\netcoreapp3.1\restartPi.bat";
    proc2.Start();
    Thread.Sleep(100);
}
catch (Exception ex)
{
   ;
}

Another way to restart Pi by using SSH.NET

https://sshnet.github.io/SSH.NET/index.html

using (var client = new SshClient("10.10.25.100", "root", "secret_password"))
{
client.Connect();
SshCommand cmd = client.RunCommand("path to reboot command");
}

 

Restarting HMI Server

Call the following method from a JavaScript function and the HMI Server will be restarted. The actual working sample can be found on the page https://www.hemelix.com/scada-hmi/twincat-hmi/alarms-and-events/

 

function RestartHMIServer() {
    return new Promise((resolve, reject) => {
        TcHmi.Server.readSymbol('Restart',  function (data) {
            if (data.error === TcHmi.Errors.NONE) {
                resolve(data.value);
            } else {
                console.log(`error occurred in RestartHMIServer error = $ {data.error}`);
                reject(data.error);
            }
        });
    });
}

Tips:

01. If you scan the network and you don’t find the devices (couplers I/O cards etc), consider restarting the PLC, sometimes it helps

References:

Download the sample from the link given above.

Next, let’s try to understand the PLC data type at https://www.hemelix.com/plc/twincat-persistent-data/

Ask questions related to Hemelix sample code and design at Google group https://groups.google.com/g/hemelix