Read Data Block of S7 300 PLC using C#

            S7Client client = new S7Client();
            int result = client.ConnectTo("192.168.0.3", 0, 2);

            if (result != 0)
            {
                Console.WriteLine(client.ErrorText(result));
            }
            else
            {
                byte[] dbBuffer = new byte[12];
                result = client.DBRead(1, 0, 12, dbBuffer);
                var var1 = S7.GetIntAt(dbBuffer, 0);
                var var2 = S7.GetRealAt(dbBuffer, 2);
                var var4 = S7.GetDWordAt(dbBuffer, 8);
                Console.WriteLine(var1);
                Console.WriteLine(var2);
                Console.WriteLine(var4);
            }

 

References
https://github.com/fbarresi/Sharp7/wiki
https://github.com/S7NetPlus/s7netplus/wiki
http://gmiru.com/article/s7comm/
https://wiki.wireshark.org/S7comm
https://plc4x.incubator.apache.org/protocols/s7/index.html

Connect Step 7 PLC-Sim to Scadas using NetToPLCsim

You should use Nettoplcsim in this way

  • restart computer
  • before starting any Siemens software, start Nettoplcsim with Admin rights, and let Nettoplcsim stop/start the Siemens service to capture the TCP port 102. Let Nettoplcsim in this state (open with no configuration)
  • start Step 7 (or TIA Portal if you want to use this), then start Plcsim and download your project
  • configure Plcsim station in Nettoplcsim, and start Nettoplcsim server

References
https://www.mesta-automation.com/nettoplcsim-how-to-connect-step-7-plc-sim-to-scadas/
http://nettoplcsim.sourceforge.net/
https://sourceforge.net/p/nettoplcsim/discussion/912717/thread/777cf7c4/?limit=25

Dependency Injection in WPF Application using Generic HostBuilder

Creating Generic HostBuilder

Nuget :

PM> Install-Package Microsoft.Extensions.Hosting -Version 3.1.2

The HostBuilder class is available from the following namespace,

using Microsoft.Extensions.Hosting;

Initialize the Host

Within Constructor of App class(file: App.xaml.cs) please add below code to build Host,

host = new HostBuilder()
           .ConfigureServices((hostContext, services) =>
           {
 
           }).Build();

DI Container

host = new HostBuilder()
          .ConfigureServices((hostContext, services) =>
          {
              //Add business services as needed
              services.AddScoped<IEmployeeViewModel, EmployeeViewModel>();
 
              //Lets Create single tone instance of Master windows
              services.AddSingleton<EmployeeWindow>();
 
          }).Build();

Setting up OnStartup and OnExit Application events

private void OnStartup(object sender, StartupEventArgs e)
        {
            var mainWindow = host.Services.GetService<EmployeeWindow>();
            mainWindow.Show();
        }
protected override async void OnExit(ExitEventArgs e)
       {
           using (host)
           {
               await host.StopAsync();
           }
 
           base.OnExit(e);
       }

App.XAML file used as below,

<Application x:Class="WPFDesktopApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Startup="OnStartup">
    <Application.Resources>
         
    </Application.Resources>
</Application>

References
https://www.thecodebuzz.com/dependency-injection-wpf-generic-hostbuilder-net-core/
https://laurentkempe.com/2019/09/03/WPF-and-dotnet-Generic-Host-with-dotnet-Core-3-0/