A .NET Library for Connecting Siemens PLC Devices: A Blessing for Automation

Article Introduction

A .NET Library for Connecting Siemens PLC Devices: A Blessing for Automation

Dear engineers, are you struggling with how to conveniently connect to Siemens PLCs? Today, I would like to recommend a powerful and practical .NET library—s7netplus. This library is specifically designed for connecting to Siemens PLCs, allowing you to easily achieve PLC communication and control in a .NET environment. S7Net Plus is a continuation of the work done by Juergen1969 on the S7.Net project. Since S7.Net has stopped updating, the author of S7Net Plus, who is engaged in automation work, saw areas for improvement in the codebase. As the author of S7.Net did not respond to requests for code submissions from the S7Net Plus author, the S7Net Plus author decided to continue development and updates on GitHub based on S7.Net.

Why Choose s7netplus?

  1. 1. Ease of Use: s7netplus provides a simple and easy-to-use API, making communication with Siemens PLCs in a .NET environment effortless.

  2. 2. Efficient and Stable: Verified by numerous real-world projects, s7netplus demonstrates efficient and stable performance, ensuring reliable communication.

  3. 3. Rich Functionality: In addition to basic communication functions, s7netplus also offers advanced features such as reading and writing DB areas and controlling PLC hardware.

  4. 4. Support for Multiple Communication Protocols: Beyond standard communication protocols, s7netplus supports various Siemens proprietary communication protocols to meet diverse complex needs.

Supported Models

S7.Net is compatible with S7-200, S7-300, S7-400, S7-1200, S7-1500

How to Use s7netplus?

Using s7netplus is very simple; just follow these steps:

  1. 1. Install the s7netplus Library: Install the s7netplus library via the NuGet package manager for convenience.

  2. 2. Create a Connection: Use the API provided by s7netplus to create a connection to the PLC.

  3. 3. Read and Write Data: Use the functions provided by s7netplus to perform read and write operations on PLC data.

  4. 4. Control the PLC: Utilize the advanced features of s7netplus to control the PLC.

Code Demonstration

Creating a PLC Instance

To create an instance of the driver you need to use this constructor:
public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
// Cpu: this specify what CPU you are connecting to. The supported CPU are:
public enum CpuType {
 S7200 = 0,
 S7300 = 10,
 S7400 = 20,
 S71200 = 30,
 S71500 = 40,
}
// Ip: this contains the IP address of the CPU of external Ethernet card
// Rack: this contains the rack of the plc, that you can find in hardware configuration in Step7
// Slot: this is the slot of the CPU, that you can find in hardware configuration in Step7
Example:
This code creates a Plc object for a S7-300 plc at the IP address 127.0.0.1, that it’s localhost, for a plc that 
it’s in rack 0 and a cpu that it’s in slot 2 of the hardware configuration:
Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);

Establishing and Releasing Connections

Connecting to the PLC
public ErrorCode Open()
For example this line of code open the connection: 
plc.Open();
Disconnecting from the PLC
public void Close()
For example this closes the connection:
plc.Close();

Reading and Writing Data


public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)
public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)

public enum DataType
{
 Input = 129,
 Output = 130,
 Memory = 131,
 DataBlock = 132,
 Timer = 29,
 Counter = 28
}

Example:
This method reads the first 200 bytes of DB1:
var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200);
Example with recursion:
private List<byte> ReadMultipleBytes(int numBytes, int db, int startByteAdr = 0)
{
 List<byte> resultBytes = new List<byte>();
 int index = startByteAdr;
 while (numBytes > 0)
 {
 var maxToRead = (int)Math.Min(numBytes, 200);
 byte[] bytes = ReadBytes (DataType.DataBlock, db, index, (int)maxToRead);
 if (bytes == null)
 return new List<byte>();
 resultBytes.AddRange(bytes);
 numBytes -= maxToRead;
 index += maxToRead;
 }
 return resultBytes;
}

By using the s7netplus library, automation engineers can communicate and control Siemens PLCs more conveniently. Whether you are a beginner or a seasoned engineer, you will greatly benefit from it.

Code Structure

A .NET Library for Connecting Siemens PLC Devices: A Blessing for Automation

Source Code Address

https://github.com/S7NetPlus/s7netplus

Recommended Reading

  1. 1. Two IoT Star Projects Developed with C#

  2. 2. Bilibili Live Recording Tool Developed with C#

  3. 3. Three Highly Rated WMS Systems Developed with .Net

  4. 4. A Desktop Tool for Controlling Android Devices Written in C#

  5. 5. A Free and Open-Source IoT Device Communication Protocol Client Developed in C#

  6. 6. A Blessing for the Industrial Control Industry: A Desktop Tool Developed in C# to Enhance Debugging Efficiency

Feel free to scan the QR code below to add me on WeChat, and note Name—Profession to obtain the source code link and join the group for discussions.

A .NET Library for Connecting Siemens PLC Devices: A Blessing for Automation

Copyright Statement: This article is sourced from network materials collected or contributed by netizens, and the copyright belongs to the copyright owner. If there is any infringement, please contact the editor for deletion.

Leave a Comment