Connecting .NET to a Siemens S7-1200 PLC is an important process for controlling and monitoring devices in industrial environments. To make this connection, you need to use Siemens libraries or available third-party libraries. Currently we can use the Sharp7 library or S7Net on Microsoft’s NuGet Package Manager.
Sharp7 library with Siemens
Here is a rough guide to connect .NET to Siemens S7-1200 using TIA Portal and Sharp7 library (a popular C# library for connecting to Siemens PLC devices):
Step 1: Prepare
- Install TIA Portal: Make sure you have installed the TIA Portal (Totally Integrated Automation) version suitable for PLC S7-1200.
- Install Sharp7: Use NuGet Package Manager in Visual Studio to install the Sharp7 library. To do this, open Visual Studio, select Tools -> NuGet Package Manager -> Package Manager Console, then enter the following command:
Install-Package Sharp7
Step 2: Configure PLC S7-1200
- Open the TIA Portal and create a new project for your PLC S7-1200.
- Add the variables (tags) needed to read and write data from the PLC.
- Configure the network connection for the PLC S7-1200 (Ethernet or PROFIBUS).
Step 3: Write .NET code
Use Sharp7 library to make connection and communication with PLC S7-1200. Here is a simple example to read a value from a variable on a PLC:
using System;
using S7.Net;
class Program
{
static void Main()
{
// IP address of PLC S7-1200 and connection port
string ipAddress = "192.168.0.100";
int rack = 0;
int slot = 1;
int port = 102;
// Create PLC object
Plc plc = new Plc(CpuType.S71200, ipAddress, rack, slot, port);
try
{
// Open connection with PLC
plc.Open();
// Read value from DB1.DBD2 variable (example)
var result = plc.Read("DB1.DBD2");
// Check if read is successful
if (result.IsSuccess)
{
float value = S7.Net.Types.DWord.FromByteArray(result.Value);
Console.WriteLine($"Value from PLC: {value}");
}
else
{
Console.WriteLine("Error reading from PLC: " + result.Error);
}
}
catch (Exception ex)
{
Console.WriteLine("Error connecting to PLC: " + ex.Message);
}
finally
{
// Close connection
plc.Close();
}
Console.ReadLine();
}
}
Note that the reference addresses and data types of the variable must be correctly configured in the PLC and respectively used in the .NET code.
Note: Make sure that you have checked the network connection and security configuration to ensure safety and security during the connection and communication between .NET and PLC S7-1200.
S7Net library with Siemens
To connect .NET to Siemens S7-1200 PLC using the S7.Net library, you need to install this library in your project. The S7.Net library allows communication with Siemens PLCs via TCP/IP connection. Here are the basic instructions for connecting and accessing data from an S7-1200 PLC using the S7.Net library:
Step 1: Install S7.Net
First, install the S7.Net library into your project. This can be done using the NuGet Package Manager in Visual Studio. Open the Package Manager Console and enter the following command:
Install-Package S7.Net
Bước 2: Write .NET code
After successfully installing the library, you can start writing .NET code to connect and access data from the S7-1200 PLC. Here is a simple example to read a value from a variable on a PLC:
using System;
using S7.Net;
class Program
{
static void Main()
{
// IP address of PLC S7-1200
string ipAddress = "192.168.0.100";
// Create PLC object
Plc plc = new Plc(CpuType.S71200, ipAddress, 0, 1);
try
{
// Open connection with PLC
plc.Open();
// Read value from variable DB1.DBD10 (example)
int dbNumber = 1;
int startByteAdr = 10;
int length = 4;
byte[] data = new byte[length];
plc.ReadBytes(DataType.DataBlock, dbNumber, startByteAdr, length, data);
// Convert data from byte array to desired data type (eg int, float, ...)
float value = S7.Net.Types.DWord.FromByteArray(data);
Console.WriteLine($"Value from PLC: {value}");
}
catch (Exception ex)
{
Console.WriteLine("Error connecting to PLC: " + ex.Message);
}
finally
{
// Close connection
plc.Close();
}
Console.ReadLine();
}
}
Note that you need to configure the variable’s address (DB number, startByteAdr) and data type (length) to match your PLC. Also, make sure that the IP address of the PLC S7-1200 is set correctly.
Above are two simple examples to help you get started with .NET connection to PLC S7-1200. The S7.Net library provides more features to perform more complex tasks like writing data, reading multiple variables at once and much more. You can find more information in the documentation of both libraries.
Read more video: Link
COMMUNICATION SIMENS S7-1200 PLC TO PYTHON WITH PYTHON-SNAP7