WinCC Advanced provides a way to monitor the heartbeat of the HMI through an area pointer called coordination word. PLC can monitor this life bit and find out when the HMI gets disconnected.
This blog shows you how to monitor the HMI life bit on a Siemens PLC.
Create a data block variable of type Word called wCoordinationWord and open HMI connections in Portal. After you select the HMI connection that is linked to your PLC, select the area pointer tab.
Enable the Coordination area pointer by checking the Active checkbox and link the PLC tag 'wCoordinationWord' that you just created.
Now we need to monitor bit 2 of the coordination word which is the life bit. The life bit toggles approx once every second.
To extract this bit from the coordination word I use the function fcWordToBits from the Siemens Open library.
If the HMI is down or not connected to the PLC, the HMI won't be able to change this bit. If the value of the bit doesn't change for two seconds, you can pause your running batch or trigger an HMI alarm, though the alarm would only show up on the HMI when the HMI is connected back to the PLC.