Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a C# HMI Application with Siemens PLC Communication Using PLCSIM Advanced V3.0

Tech 1

This guide covers the complete workflow for developing a Windows-based HMI application in Visual Studio using C#, communicating with a Siemens PLC 1511-PN through PLCSIM Advanced V3.0 simulation.

System Architecture Overview

The implementation consists of three primary components:

  1. Visual Studio HMI Application - Built with C# Windows Forms
  2. Siemens PLC 1511-PN - Running in PLCSIM Advanced V3.0
  3. Communication Layer - Using the S7.NET library

Required Software Components

  • Visual Studio (2022 or later recommended)
  • TIA Portal V15 or later
  • PLCSIM Advanced V3.0
  • S7.NET library from NuGet Package Manager

Setting Up C# Windows Forms HMI in Visual Studio

Project Creation

Open Visual Studio and create a new Windows Forms project:

File → New → Project → Windows Forms App (.NET Framework)

Windows Forms provides a drag-and-drop designer for rapid UI development, similar to WinCC but with full .NET framework capabilities.

Adding Communication Library

Install S7.NET through NuGet Package Manager:

Install-Package S7netplus

Basic Communication Implementation

using S7.Net;

public class PlcCommunication
{
    private PlcS7Client _plcClient;
    private string _ipAddress;
    private int _rack;
    private int _slot;
    
    public PlcCommunication(string ip, int rack = 0, int slot = 1)
    {
        _ipAddress = ip;
        _rack = rack;
        _slot = slot;
    }
    
    public void Connect()
    {
        _plcClient = new PlcS7Client(CpuType.S71200, _ipAddress, (short)_rack, (short)_slot);
        _plcClient.Open();
    }
    
    public void WriteData(string dataBlock, int offset, object value)
    {
        if (_plcClient.IsConnected)
        {
            _plcClient.Write(dataBlock, offset, value);
        }
    }
    
    public object ReadData(string dataBlock, int offset, VarType type, int count)
    {
        if (_plcClient.IsConnected)
        {
            return _plcClient.Read(dataBlock, offset, type, count);
        }
        return null;
    }
    
    public void Disconnect()
    {
        _plcClient?.Close();
    }
}

Configuring PLCSIM Advanced V3.0

Important Considerations

PLCSIM Advanced differs significantly from standard PLCSIM:

  • Creates a virtual network adapter
  • Enables communication with external applications beyond TIA Portal
  • Supports 1500 series and 200 Smart PLC families
  • For 1200 series PLCs, additional software NetToPLCSIM-S7 is required

Network Adapter Setup

  1. Install PLCSIM Advanced V3.0
  2. The software automatically creates a virtual network adapter
  3. Configure the PLC project to use this virtual adapter
  4. Assign a valid IP address to the virtual PLC

PLC Project Configuration

Enabling External Communication

In TIA Portal, configure the PLC properties:

  1. Navigate to PLC → Properties → Protection
  2. Enable "Permit access with PUT/GET communication"
  3. Configure the IP address matching your simulation setup
  4. Download the project to the simulated PLC

Data Block Structure

// Define data blocks in PLC
// DB1 - Control Signals
struct ControlSignals
{
    bool StartCommand;      // DBO.0
    bool StopCommand;      // DB0.1
    bool EmergencyStop;    // DB0.2
    bool AutoMode;          // DB0.3
}

// DB2 - Status Signals  
struct StatusSignals
{
    bool MotorRunning;     // DB1.0
    bool FaultIndicator;   // DB1.1
    int ProductionCount;   // DBW2
    real Temperature;     // DBD4
}

Implementing HMI Controls

Button Event Handling

public partial class MainForm : Form
{
    private PlcCommunication _plc;
    
    public MainForm()
    {
        InitializeComponent();
        InitializePlc();
    }
    
    private void InitializePlc()
    {
        _plc = new PlcCommunication("192.168.1.100");
        _plc.Connect();
    }
    
    private void btnStart_Click(object sender, EventArgs e)
    {
        _plc.WriteData("DB1", 0, true);
    }
    
    private void btnStop_Click(object sender, EventArgs e)
    {
        _plc.WriteData("DB1", 0, false);
    }
    
    private void timerStatus_Tick(object sender, EventArgs e)
    {
        // Read PLC status periodically
        var status = _plc.ReadData("DB2", 0, VarType.Bit, 1);
        UpdateDisplay(status);
    }
    
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        _plc?.Disconnect();
        base.OnFormClosing(e);
    }
}

Form Navigation

For multi-window HMI applications, implement form navigation using the FormManager pattern:

public void NavigateToForm(Form targetForm)
{
    this.Hide();
    targetForm.Show();
}

Troubleshooting Comon Issues

Connection Problems

Isue Solution
Cannot connect to PLC Verify IP address and ensure PLC is in RUN mode
Communication timeout Check firewall settings and network adapter configuration
Unable to read/write data Confirm "Permit access with PUT/GET" is enabled

Simulation Issues

  • Ensure PLCSIM Advanced is running before starting the HMI application
  • Verify the virtual network adapter is properly installed
  • Check that the PLC IP address matches the configuration in code

Summary

This implementation provides a complete foundation for building HMI applications with Siemens PLCs. The key components include:

  • Windows Forms for UI development
  • S7.NET libray for TCP/IP communication
  • PLCSIM Advanced for hardware-independent testing
  • Proper PLC configuration for external access

The architecture allows for scalable expansion with additional PLCs, more complex control logic, and integration with databases such as MySQL for data logging and historical analysis.

Tags: C#

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.