How to reboot PLC remotely

//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);

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;
}
}

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:

=> https://stackoverflow.com/questions/16129395/remote-shutdown-pc-using-c-sharp-windows-form

 

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