kapiya / s7_client

Siemens S7 Client Library For .Net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Siemens S7 Client Library For .Net

Nuget Total Downloads

Overview

s7_client is a simple and fast .Net library which communicates with Siemens S7 devices using Siemens S7 Protocol.

s7client is the synchronous version of async_s7_client and it is designed to be used with Timers and BackgroundWorkers.

PLC Settings For Data Access

  • S7 300, S7 400: Default settings allow full access for these cpus.
  • S7 1200, S7 1500: Only global data blocks can be accessed. The optimized block access option must be turned off. The access level must be full and connection mechanism must allow GET/PUT.

Supported Data Types

Data Type Description
ushort Unsigned 16-bit integer
uint Unsigned 32-bit integer
float 32-bit floating point number

Installation

Download the latest release from here and add reference to your project or run the following command in Nuget Package Manager Console.

PM> Install-Package s7_client

Constructor

  • S7Client(string ipAddress, ushort port, ushort rack, ushort slot)

    • Parameters:
      • ipAddress: Ip address of the remote device.
      • port: Port of the remote device. The default port is 102 for Siemens S7 Protocol.
      • rack: Rack number of the remote device.
      • slot : Slot number of the remote device.

      Rack and slot numbers for supported Cpus:

      Cpu Rack Slot Description
      S7 300 0 2 Always
      S7 400 - - Follow the hardware configuration
      S7 1200 0 0 Or 0, 1
      S7 1500 0 0 Or 0, 1
    • Example:
      S7Client s7Client = new S7Client("192.168.0.1", 102, 0, 0);

Properties

  • Busy:

    Gets a bool value indicating whether the S7Client is running a task.

  • Connected:

    Gets a bool value indicating whether the S7Client is connected to a remote device.

  • PduLength:

    Gets a ushort value indicating the process data unit length of the remote device.

Methods

  • Connect()

    Connects to the remote device. Returns Connected property.

    • Example:
      private void buttonConnect_Click(object sender, EventArgs e) {
          try {
              //connect to remote device
              bool result = s7Client.Connect();
      
              //print result
              Debug.WriteLine(result.ToString());
          }
          catch(S7ClientException s7ClientException) {
              Debug.WriteLine(s7ClientException.ToString());
          }
      }
    • Exceptions:

      Throws only S7ClientException.

      Exception Code Exception Message
      1 Hostname or port is not valid.
      2 Tcp connection failed.
      3 Network stream failed.
      4 Iso connection failed.
      5 Pdu negotiation failed.
      9 S7Client is busy.
  • Close()

    Disposes the tcp client instance and requests that the underlying tcp connection be closed. Returns a bool indicating whether the task is successfully completed.

    • Example:
      private void buttonClose_Click(object sender, EventArgs e) {
          try {
              //close s7 client
              bool result = s7Client.Close();
      
              //print result
              Debug.WriteLine(result.ToString());
          }
          catch(S7ClientException s7ClientException) {
              Debug.WriteLine(s7ClientException.ToString());
          }
      }
    • Exceptions:

      Throws only S7ClientException.

      Exception Code Exception Message
      9 S7Client is busy.
  • Read(ushort dataBlockNumber, uint startingAddress, ushort byteCount)

    Reads data from the remote device's specified data block. Returns a byte array indicating each data starting from the first data.

    • Parameters:
      • dataBlockNumber: Number of the data block.
      • startingAddress: Starting address of the data.
      • byteCount: Byte count of the data.
    • Example:
      private void buttonRead_Click(object sender, EventArgs e) {
          try {
              //read ten bytes from data block 1, starting from address 0. Ten bytes include 
              //one ushort (16-bit unsigned integer), one uint (32-bit unsigned integer) and 
              //one float (32-bit floating point number)
              byte[] readBytes = s7Client.Read(1, 0, 10);
      
              //print ushort
              ushort _ushort = s7Client.GetUShort(readBytes, 0);
              Debug.WriteLine($"ushort : {_ushort.ToString()}");
      
              //print uint
              uint _uint = s7Client.GetUInt(readBytes, 2);
              Debug.WriteLine($"uint : {_uint.ToString()}");
      
              //print float
              float _float = s7Client.GetFloat(readBytes, 6);
              Debug.WriteLine($"float : {_float.ToString()}");
          }
          catch(S7ClientException s7ClientException) {
              Debug.WriteLine(s7ClientException.ToString());
          }
      }
    • Exceptions:

      Throws only S7ClientException.

      Exception Code Exception Message
      6 Byte count is out of range.
      7 Reading failed.
      9 S7Client is busy.
      10 S7Client is not connected to a remote device.
  • Write(ushort dataBlockNumber, uint startingAddress, byte[] bytesToWrite)

    Writes data to the remote device's specified data block. Returns a bool indicating whether the task is successfully completed.

    • Parameters:
      • dataBlockNumber: Number of the data block.
      • startingAddress: Starting address of the data.
      • bytesToWrite: Bytes of data.
    • Example:
      private void buttonWrite_Click(object sender, EventArgs e) {
          try {
              //write ten bytes to data block 1, starting from address 0. Ten bytes include 
              //one ushort (16-bit unsigned integer), one uint (32-bit unsigned integer) and 
              //one float (32-bit floating point number)
              byte[] bytesToWrite = new byte[10];
      
              //bytes of ushort
              ushort _ushort = 1000;
              s7Client.SetUshort(bytesToWrite, 0, _ushort);
      
              //bytes of uint
              uint _uint = 100000;
              s7Client.SetUInt(bytesToWrite, 2, _uint);
      
              //bytes of float
              float _float = 3.14f;
              s7Client.SetFloat(bytesToWrite, 6, _float);
      
              bool result = s7Client.Write(1, 0, bytesToWrite);
      
              //print result
              Debug.WriteLine(result.ToString());
          }
          catch(S7ClientException s7ClientException) {
              Debug.WriteLine(s7ClientException.ToString());
          }
      }
    • Exceptions:

      Throws only S7ClientException.

      Exception Code Exception Message
      6 Byte count is out of range.
      8 Writing failed.
      9 S7Client is busy.
      10 S7Client is not connected to a remote device.

Exceptions

All S7ClientException codes and messages.

Exception Code Exception Message
1 Hostname or port is not valid.
2 Tcp connection failed.
3 Network stream failed.
4 Iso connection failed.
5 Pdu negotiation failed.
6 Byte count is out of range.
7 Reading failed.
8 Writing failed.
9 S7Client is busy.
10 S7Client is not connected to a remote device.

Tests

  • Test 01:

    Cpu Model CPU Version Test Date
    S7 1200 1214C V4.2 27.03.2019
    Function Name Result
    Read Reads 222 bytes in under 10 milliseconds.
    Write Writes 204 bytes in under 10 milliseconds.

References

  • Snap7 : This code implements a small amount of S7 protocol which is completely covered in Snap7. Snap7 is a great open source project created by Dave Nardella. You can visit Snap7 Homepage for more information.

About

Siemens S7 Client Library For .Net

License:GNU General Public License v3.0


Languages

Language:C# 100.0%