Skip to content

Commit

Permalink
added command line argument for plc port
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Sep 23, 2018
1 parent 913476a commit 28d586d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bld/

# Visual Studio 2015/2017 cache/options directory
.vs/
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
14 changes: 13 additions & 1 deletion SoftPlc/Services/PlcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using SoftPlc.Interfaces;
using SoftPlc.Models;
using Microsoft.Extensions.Configuration;


namespace SoftPlc.Services
{
Expand All @@ -11,11 +13,21 @@ public class PlcService : IPlcService, IDisposable
private readonly S7Server server;
private readonly bool serverRunning;
private readonly Dictionary<int, DatablockDescription> datablocks = new Dictionary<int, DatablockDescription>();
public PlcService()
public PlcService(IConfiguration configuration)
{
Console.WriteLine("Initializing plc service...");

server = new S7Server();


if(configuration.GetChildren().Any(item => item.Key.Equals("plcPort")))
{
UInt16 plcPort;
var parsed = UInt16.TryParse(configuration["plcPort"], out plcPort);
if(parsed)
server.SetParam(S7Consts.p_u16_LocalPort, ref plcPort);
}

var error = server.Start();
serverRunning = error == 0;
if (serverRunning) Console.WriteLine("plc server started!");
Expand Down
6 changes: 4 additions & 2 deletions SoftPlc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var plcService = new PlcService();
services.AddSingleton<IPlcService>(plcService);
services.AddSingleton<IPlcService, PlcService>();

// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
Expand Down Expand Up @@ -81,6 +80,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
});

app.UseMvc();

//initialize plc service on start
var plcService = app.ApplicationServices.GetService<IPlcService>();
}
}
}

0 comments on commit 28d586d

Please sign in to comment.