Managing Siemens PLC With S7.NET Library

Introduction

 
In this series of articles, we will make use of technologies that deviate somewhat from what you usually see -- web applications with ASP.NET for example, or management applications -- in short, the classic development of common applications. We will enter a completely different world, we can say that in part it has to do with the internet of things. We'll talk about industrial automation and management machine tools PLC. You may wonder: but what does all this have to do with development with Microsoft and Visual Studio? It partly does, and partly doesn't. 
 
If it is true that we speak of other areas, and true that with Visual Studio, we can safely create interfaces and applications that interact with a machine tool and/or an automation system, we'll start with what are the tools needed to create a system which will interact with a Siemens PLC. We will continue creating a project with WPF technology. In the next article, we will create the project with TIAPORTAL V13 and will perform the sample application text.
 
Necessary tools
 
Below, we see what instruments and software we need to make what we described previously. Let's start with Microsoft.
 Now, we come to the part of Siemens.
  • Totally integrated Automation (TIA Portal V13), is nothing more or less than the development IDE of Siemens with which we should create all the PLC program. On the latter, you should talk a lot, that is, as should be used, what are the principles for good logic PLC, and how to learn the languages dedicated for development. We will give some advice necessary to the creation of the project, but the invitation for good learning to access the official website where there will be all the information available, necessary for learning.
At the software level, we have everything we need. Let's see what you should have on the hardware level.
  • To best perform the sample project, the idea is to have as in my case a PLC, will make use of the CPU 1211C, the family of Siemens S7-1200 products.
     
  • A power supply that provides a 24V DC voltage and outputs current of at least 2 Amperes.
     
  • RJ45 network patch cable (uncrossed)
Link for Siemens products
 
I leave briefly the necessary link for Siemens products. From this link, you can download the trial version of TIAPORTAL V13, and for those who lack access to a PLC, can download the emulators of the CPU S7-1200 family. Install them so that you can run the emulation CPU without necessarily having a physical PLC at home. At the end to the page, you will find the files to download and install. Else, those who do not use a physical PLC should install NetToPlcSim. This to emulate a network connection between the PLC and the interface that we will create later.
 
Creating the project with Visual Studio
 
Got to this point, we just should create the sample project from Visual Studio. Once downloaded and installed Visual Studio, you are moving and we create a WPF project entitled S71200 TEST, as in the image.
 
 
Image 1: Creation of the WPF project.
 
Created the project, we should add two buttons, a text box, and a TextBlock. The buttons will be used to set to true and a false PLC variable, if one bit, that if we want to compare it to .NET and C #, it is a bool variable. The text box is used to enter the value that we want to set for the variable PLC, then the TextBlock will show the actual / set value of the PLC variable. In your project, copy the following XAML code.
  1. <Window x:Class="S71200_TEST.MainWindow"    
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.         xmlns:local="clr-namespace:S71200_TEST"    
  7.         mc:Ignorable="d"    
  8.         Title="S71200 TEST" Height="350" Width="525">    
  9.     <Grid>    
  10.         <Grid.RowDefinitions>    
  11.             <RowDefinition Height="20"/>                
  12.             <RowDefinition Height="Auto"/>    
  13.             <RowDefinition Height="Auto"/>    
  14.             <RowDefinition Height="*"/>    
  15.             <RowDefinition Height="Auto"/>                
  16.         </Grid.RowDefinitions>    
  17.         <Grid.ColumnDefinitions>    
  18.             <ColumnDefinition Width="*"/>    
  19.             <ColumnDefinition Width="*"/>    
  20.             <ColumnDefinition Width="50"/>    
  21.             <ColumnDefinition Width="Auto"/>    
  22.             <ColumnDefinition Width="*"/>    
  23.             <ColumnDefinition Width="*"/>                
  24.         </Grid.ColumnDefinitions>    
  25.         <TextBlock Grid.Row="1" Grid.Column="1" x:Name="tbkTipoVariabile" Text="IMPOSTA BIT"/>    
  26.         <TextBox Grid.Row="1" Grid.Column="2" x:Name="tbxImpostaVariabile"/>    
  27.         <TextBlock Grid.Row="2" Grid.Column="1" x:Name="tbkValoreVariabile" Text="VALORE ATTUALE"/>    
  28.         <TextBlock Grid.Row="2" Grid.Column="2" x:Name="tbkValore"/>    
  29.         <Button Grid.Row="4" Grid.Column="1" x:Name="btnSetBit" Content="SET BIT" Click="btnSetBit_Click"/>    
  30.         <Button Grid.Row="4" Grid.Column="4" x:Name="btnRESetBit" Content="SET BIT" Click="btnRESetBit_Click"/>            
  31.     </Grid>    
  32. </Window>   
After entering the code, we should have the interface, as shown below.
 
 
Image 2: Interface creation of the WPF project.
 
As mentioned, this is a simple example. As we move forward, it will enrich with all the functionality of the S7.NET library, to have at the end a functional project that we can then extend further in the future according to our needs.
 
Now, let's add, as told by NuGet, the reference to the library S7.NET. In exploring solutions, we are going to select References, right-click and select -
  • Manage NuGet packages.
 
Image 3: Research S7NET library on NuGet.
 
Sprint found the library. Now, install it by clicking on Install. After the installation, we are now ready to implement all the functionality within the test project. Let's go back to Visual Studio 2015 and access the editor of C # code. The first thing to do is to include the class MainWindow.cs S7.NET namespace, as shown in the following code.
  1. / * NameSpace third party * / using S7.Net;  
Performed the namespace import, we can write the code. First, we should open the connection to the PLC to which we want to connect. This is accomplished by creating a new instance of the S7.NET class.
  1. Plc _S71200 = new Plc (CpuType.S71200, "192.168.0.25", 0, 1);  
The previous code creates a new object S7NET, where we need to implement each of the topics requested by its manufacturer, that is,
  • CpuType, the family to which the PLC belongs. In our case, it is a series CPU S7-1200.
  • We will see how to set the IP address of the CPU by using TIAPORTAL V13 Siemens software in the next article.
  • The last parameter of the PLC station is located. Leave it to 1 for S7-1200.
Now that we have created the object of S7NET type, you must open the Ethernet connection to the PLC. The  Open () method must be implemented here, as follows.
  1. _S71200.Open ();   
These are the minimum code instructions to be able to communicate with the plc S7-1200 and to any other and that supported by the S7.NET library. Let us write code to set/reset the bit. We have defined two buttons in the interface, the first set to true and the other false reset to all, a TextBox where we will write the value to be set, a TextBlock that will display the bit status. Here is the complete C # code.
  1. using System.Windows;    
  2.     
  3. /*NameSpace di terze parti*/    
  4. using S7.Net;    
  5.     
  6. namespace S71200_TEST    
  7. {    
  8.     /// <summary>    
  9.     /// Logica di interazione per MainWindow.xaml    
  10.     /// </summary>    
  11.     public partial class MainWindow : Window    
  12.     {    
  13.         Plc _S71200 = new Plc(CpuType.S71200, "192.168.0.25", 0, 1);    
  14.     
  15.         public MainWindow()    
  16.         {    
  17.             InitializeComponent();    
  18.             /*Apro la comunicazione con il plc*/    
  19.             _S71200.Open();    
  20.         }    
  21.     
  22.         private void btnSetBit_Click(object sender, RoutedEventArgs e)    
  23.         {    
  24.             /*Se il plc è connesso*/    
  25.             if (_S71200.IsConnected)    
  26.             {    
  27.                 /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/    
  28.                 bool uscita = (bool)_S71200.Read("DB1.DBX0.0");    
  29.     
  30.                 /*Se il valore del bit e false*/    
  31.                 if (uscita.Equals(false))    
  32.                 {    
  33.                     /*Imposto a true il bit DBX0.0*/    
  34.                     _S71200.Write("DB1.DBX0.0"true);    
  35.                     /*Visualizzo lo stato attuale sull'interfaccia*/    
  36.                     tbkValore.Text = uscita.ToString();    
  37.                 }    
  38.             }    
  39.         }    
  40.     
  41.         private void btnRESetBit_Click(object sender, RoutedEventArgs e)    
  42.         {    
  43.             /*Se il plc è connesso*/    
  44.             if (_S71200.IsConnected)    
  45.             {    
  46.                 /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/    
  47.                 bool data1 = (bool)_S71200.Read("DB1.DBX0.0");    
  48.     
  49.                 /*Se il valore del bit e true*/    
  50.                 if (data1.Equals(true))    
  51.                 {    
  52.                     /*Imposto a false il bit DBX0.0*/    
  53.                     _S71200.Write("DB1.DBX0.0"false);    
  54.                     /*Visualizzo lo stato attuale sull'interfaccia*/    
  55.                     tbkValore.Text = data1.ToString();    
  56.                 }    
  57.             }    
  58.         }    
  59.     }    
  60. }   
And a good rule, before performing any operation, checks if your plc and connected with our interface, as a malfunction resulting in application crashes, because at any time or for any reason can drop the Ethernet connection.
 
Wrote the code, F5 to debug the project if everything is spelled correctly, and that we should see.
 
 
Image 4: The interface while debugging.
 

Conclusion

 
In this first part, was made a brief introduction on what and how to create an application that can interact with a PLC of the S7-1200 family through Visual Studio and S7.NET library. We created a simple interface and implemented the C # code in a WPF project. In the next article, we will see how to configure the PLC, how to program it to enable it to communicate with the S7.NET library, using the interface created in Visual Studio 2015.