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.
At the software level, we have everything we need. Let's see what you should have on the hardware level.
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 -
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,
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. /*NameSpace di terze parti*/
  3. using S7.Net;
  4. namespace S71200_TEST
  5. {
  6. /// <summary>
  7. /// Logica di interazione per MainWindow.xaml
  8. /// </summary>
  9. public partial class MainWindow : Window
  10. {
  11. Plc _S71200 = new Plc(CpuType.S71200, "192.168.0.25", 0, 1);
  12. public MainWindow()
  13. {
  14. InitializeComponent();
  15. /*Apro la comunicazione con il plc*/
  16. _S71200.Open();
  17. }
  18. private void btnSetBit_Click(object sender, RoutedEventArgs e)
  19. {
  20. /*Se il plc è connesso*/
  21. if (_S71200.IsConnected)
  22. {
  23. /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/
  24. bool uscita = (bool)_S71200.Read("DB1.DBX0.0");
  25. /*Se il valore del bit e false*/
  26. if (uscita.Equals(false))
  27. {
  28. /*Imposto a true il bit DBX0.0*/
  29. _S71200.Write("DB1.DBX0.0", true);
  30. /*Visualizzo lo stato attuale sull'interfaccia*/
  31. tbkValore.Text = uscita.ToString();
  32. }
  33. }
  34. }
  35. private void btnRESetBit_Click(object sender, RoutedEventArgs e)
  36. {
  37. /*Se il plc è connesso*/
  38. if (_S71200.IsConnected)
  39. {
  40. /*Vado a leggere il valore del bit DBX0.0 sul blocco dati DB1*/
  41. bool data1 = (bool)_S71200.Read("DB1.DBX0.0");
  42. /*Se il valore del bit e true*/
  43. if (data1.Equals(true))
  44. {
  45. /*Imposto a false il bit DBX0.0*/
  46. _S71200.Write("DB1.DBX0.0", false);
  47. /*Visualizzo lo stato attuale sull'interfaccia*/
  48. tbkValore.Text = data1.ToString();
  49. }
  50. }
  51. }
  52. }
  53. }
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.