Building a C# HMI Application with Siemens PLC Communication Using PLCSIM Advanced V3.0
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:
- Visual Studio HMI Application - Built with C# Windows Forms
- Siemens PLC 1511-PN - Running in PLCSIM Advanced V3.0
- 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
- Install PLCSIM Advanced V3.0
- The software automatically creates a virtual network adapter
- Configure the PLC project to use this virtual adapter
- Assign a valid IP address to the virtual PLC
PLC Project Configuration
Enabling External Communication
In TIA Portal, configure the PLC properties:
- Navigate to PLC → Properties → Protection
- Enable "Permit access with PUT/GET communication"
- Configure the IP address matching your simulation setup
- 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.