Share via


Manage PLC with S7.NET Library part one

Introduction

In this series of articles, we will make use of technologies that deviate somewhat 'from what you usually see, that 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 to do with the internet of things, the internet of things and some not. We'll talk about industrial automation and management machine tools plc and not. You may wonder: but what does all this with the development, with Microsoft and Visual Studio? Partly true and partly not. If it is true that we speak of other areas, and true that we can safely with Visual Studio, 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 go to interact with a Siemens plc. We continue creating a project with WPF technology. In the next article, it created the project with TIAPORTAL V13, and will perform the sample application text.

Necessary tools

Below, we see what needs to be as instruments and software to make as described previously. Let's start with Microsoft.

  •  Microsoft Visual Studio 2015       
  •  S7 net, library for communication with the Siemens plc, available on NuGet. This and all the part Microsoft. Now we come to the part of Siemens.
  • Totally integrated Automation(TIA Portal V13), it would be nothing more nor less, the development IDE 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 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 necessary for learning.

At the software level, we have everything you need, let's see what you should have on a hardware level.

  • ?   To best perform the sample project, the ideal 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 output current of at least 2 amperes.
  • ?  RJ45 network patch cable (uncrossed)

 

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, for those who does not use a physical plc, and 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 button 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 the variable plc, then the TextBlock, will show the actual / set value of the PLC variable. We copy in our project the following XAML code.

<Window x:Class="S71200_TEST.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:local="clr-namespace:S71200_TEST"
 mc:Ignorable="d"
 Title="S71200 TEST" Height="350" Width="525">
 <Grid>
 <Grid.RowDefinitions>
 <RowDefinition Height="20"/>  
 <RowDefinition Height="Auto"/>
 <RowDefinition Height="Auto"/>
 <RowDefinition Height="*"/>
 <RowDefinition Height="Auto"/>  
 </Grid.RowDefinitions>
 
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="*"/>
 <ColumnDefinition Width="*"/>
 <ColumnDefinition Width="50"/>
 <ColumnDefinition Width="Auto"/>
 <ColumnDefinition Width="*"/>
 <ColumnDefinition Width="*"/>  
 </Grid.ColumnDefinitions>
 
 <TextBlock Grid.Row="1" Grid.Column="1" x:Name="tbkTipoVariabile" Text="IMPOSTA BIT"/>
 <TextBox Grid.Row="1" Grid.Column="2" x:Name="tbxImpostaVariabile"/>
 
 <TextBlock Grid.Row="2" Grid.Column="1" x:Name="tbkValoreVariabile" Text="VALORE ATTUALE"/>
 <TextBlock Grid.Row="2" Grid.Column="2" x:Name="tbkValore"/>
 
 <Button Grid.Row="4" Grid.Column="1" x:Name="btnSetBit" Content="SET BIT" Click="btnSetBit_Click"/>
 <Button Grid.Row="4" Grid.Column="4" x:Name="btnRESetBit" Content="SET BIT" Click="btnRESetBit_Click"/>  
 </Grid>
</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 S7 NET library, to have at the end a functional project that we can then extend it 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.

Under S7NET we type, and here NuGet finds for us the extent that we asked.

[

](resources/5518.IMMAGINE-3.PNG)

 

image 3: Research S7NET library on NuGet.

 

Sprint found the library, the install by clicking with the mouse on the button 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 we access the editor of C # code. The first thing to do, and to include in the class MainWindow.cs S7.NET namespace, as shown in the following code.

 

/ * 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 S7NET class.

 

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 belongs the plc, in our case it is a series CPU S7-1200.
  • The IP address of the CPU, we will see how to go to set it in the next article, making use of TIAPORTAL V13 Siemens software.
  • The third parameter, and the rack is placed where the CPU, the default is zero, because in our case a single PLC station, the rack can be only one.
  • The last parameter of the PLC station location, to leave 1 for S7-1200.

Now that we have created the object of S7NET type, you must open the Ethernet connection to the plc, to do what must be implemented the Open () method as follows.

_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 S7NET 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.

using System.Windows;
  
/*NameSpace di terze parti*/
using S7.Net;
  
namespace S71200_TEST
{
 /// <summary>
 /// Logica di interazione per MainWindow.xaml
 /// </summary>
 public partial  class MainWindow : Window
 {
 Plc _S71200 = new  Plc(CpuType.S71200, "192.168.0.25", 0, 1);
  
 public MainWindow()
 {
 InitializeComponent();
 /*Apro la comunicazione con il plc*/
 _S71200.Open();
 }
  
 private void  btnSetBit_Click(object sender, RoutedEventArgs e)
 {
 /*Se il plc ? connesso*/
 if (_S71200.IsConnected)
 {
 /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/
 bool uscita = (bool)_S71200.Read("DB1.DBX0.0");
  
 /*Se il valore del bit e false*/
 if (uscita.Equals(false))
 {
 /*Imposto a true il bit DBX0.0*/
 _S71200.Write("DB1.DBX0.0", true);
 /*Visualizzo lo stato attuale sull'interfaccia*/
 tbkValore.Text = uscita.ToString();
 }
 }
 }
  
 private void  btnRESetBit_Click(object sender, RoutedEventArgs e)
 {
 /*Se il plc ? connesso*/
 if (_S71200.IsConnected)
 {
 /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/
 bool data1 = (bool)_S71200.Read("DB1.DBX0.0");
  
 /*Se il valore del bit e true*/
 if (data1.Equals(true))
 {
 /*Imposto a false il bit DBX0.0*/
 _S71200.Write("DB1.DBX0.0", false);
 /*Visualizzo lo stato attuale sull'interfaccia*/
 tbkValore.Text = data1.ToString();
 }
 }
 }
 }
}

  

And a good rule, before performing any operation, check 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 spelt correctly, and that we should see.

 

picture 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 through Visual Studio and S7NET library interact with a plc of the S7-1200 family Siemens, 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 put it able to communicate with the library S7NET with the interface created with Visual Studio 2015.