Skip to content

shobeirimajid/PLC-Communication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PLC Communication (S7-1500)

To connect to a Siemens S7-1500 PLC using C#, you can utilize the S7.Net library, which is a popular and easy-to-use .NET library for Siemens S7 PLC communication.

getting started

Install the S7.NetPlus Library

You can open your C# project in Visual Studio.

Open the NuGet Package Manager Console and run the following command: Install-Package S7.NetPlus

Create a New C# Project

If you don't have a project yet, create a new Console App (.NET Core or .NET Framework) project.

Write Code to Connect to the PLC

  • Add the necessary using directives at the top of your file.

  • Create an instance of the Plc class.

  • Connect to the PLC using the Open() method.

  • Read and write data as needed.

  • Close the connection when done.

Sample Code

Here is an example code snippet to demonstrate this in C#

using System;
using S7.Net;  // Make sure to add this using directive

namespace PLCConnectionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Replace with your PLC's IP address, CPU type, and rack/slot numbers
            string plcIp = "192.168.0.1";
            CpuType cpuType = CpuType.S71500;
            short rack = 0;  // Default rack number
            short slot = 1;  // Default slot number

            // Create a new instance of the Plc class
            Plc plc = new Plc(cpuType, plcIp, rack, slot);

            try
            {
                // Open a connection to the PLC
                plc.Open();

                if (plc.IsConnected)
                {
                    Console.WriteLine("Connected to the PLC.");

                    // Example: Read a value from DB1, starting at byte 0, of type Int16 (word)
                    object db1Value = plc.Read("DB1.DBW0");
                    Console.WriteLine("DB1.DBW0 value: " + db1Value);

                    // Example: Write a value to DB1, starting at byte 0, of type Int16 (word)
                    plc.Write("DB1.DBW0", (short)1234);
                    Console.WriteLine("Wrote value 1234 to DB1.DBW0.");
                }
                else
                {
                    Console.WriteLine("Failed to connect to the PLC.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
            finally
            {
                // Always close the connection when done
                plc.Close();
                Console.WriteLine("Connection to PLC closed.");
            }

            Console.ReadLine();
        }
    }
}

Explanation:

  1. Plc Class: Represents the connection to the PLC.
  2. Open Method: Establishes the connection to the PLC.
  3. IsConnected Property: Checks if the connection is established.
  4. Read Method: Reads data from the PLC. You need to specify the data block and address.
  5. Write Method: Writes data to the PLC. You need to specify the data block and address, and the value to write.
  6. Close Method: Closes the connection to the PLC.

Notes

  1. Ensure the IP address, CPU type, rack, and slot numbers are correctly set for your PLC configuration.
  2. Proper exception handling is included to manage connection errors.
  3. Always close the PLC connection to free resources.
  4. This example should help you get started with connecting and communicating with a Siemens S7-1500 PLC using C#.
  5. The thing is that on the PLC system, the length of the string is always reserved in memory, so you just need to explicitly read the first 2 bytes in the DB containing your string as an unsigned 16-bit integer. It stores the current actual length of your string.

References

https://github.com/S7NetPlus/s7netplus

https://github.com/S7NetPlus/s7netplus/wiki

https://www.nuget.org/packages/S7netplus/

https://github.com/S7NetPlus/s7netplus/wiki

https://www.ad.siemens.com.cn/club/bbs/upload/file/20181129/6367911382823920496914596.pdf

https://www.youtube.com/playlist?list=PLRCEJ0bGSS1ZlU34IXsIKS62IHH5FcgU1

https://gist.github.com/rapha-dev/f3cd66f69e6d141d2842008335db912c

S7NetPlus/s7netplus#105

https://github.com/S7NetPlus/s7netplus/blob/9fd515280a4fe29ad41ce3d26eacdfd7866aeeb4/S7.Net.UnitTest/S7NetTests.cs#L211

https://github.com/S7NetPlus/s7netplus/blob/9fd515280a4fe29ad41ce3d26eacdfd7866aeeb4/S7.Net.UnitTest/S7NetTests.cs#L211

https://github.com/S7NetPlus/s7netplus/blob/9fd515280a4fe29ad41ce3d26eacdfd7866aeeb4/S7.Net.UnitTest/Helpers/TestLongStruct.cs#L11

https://snap7.sourceforge.net/sharp7.html

https://www.mesta-automation.com/how-to-write-a-siemens-s7-plc-driver-with-c-and-sharp7/

https://snap7.sourceforge.net/

https://sourceforge.net/projects/snap7/files/Sharp7/

Releases

No releases published

Packages

No packages published