Skip to content

Commit

Permalink
Merge pull request #55 from ChrisPulman/FeatureAddNonGenericsAddTag
Browse files Browse the repository at this point in the history
Add non generic Tag addition
  • Loading branch information
ChrisPulman authored Feb 29, 2024
2 parents 29f10c4 + cdd748e commit 3d55aac
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ dotnet_diagnostic.RCS1180.severity=warning
dotnet_diagnostic.RCS1190.severity=error
dotnet_diagnostic.RCS1195.severity=error
dotnet_diagnostic.RCS1214.severity=error

dotnet_diagnostic.IDE1006.severity = none

csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent

Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Nullable>enable</Nullable>
<PackageIcon>S7PlcRx.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>Compatability with Net 6 / 7 and netstandard2.0</PackageReleaseNotes>
<PackageReleaseNotes>Compatability with Net 6 / 7 / 8 and netstandard2.0</PackageReleaseNotes>
<PackageTags>S7;Siemens;PLC;S7-200;S7-300;S7-400;S7-1200;S7-1500;rx;reactive;extensions;observable;LINQ;net;netstandard</PackageTags>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
Expand Down Expand Up @@ -54,7 +54,7 @@
<ItemGroup>
<!--<Compile Update="**\*.cs" DependentUpon="I%(Filename).cs" />-->
<PackageReference Include="stylecop.analyzers" Version="1.2.0-beta.556" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="4.10.0" PrivateAssets="All" />
<PackageReference Include="Roslynator.Analyzers" Version="4.11.0" PrivateAssets="All" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ plc.Observe<double>("Tag1").Subscribe(x => Console.WriteLine($"Tag1: {x}"));
plc?.GetTag("Tag0")?.SetTagPollIng(true);
```

You can also use the `S7xxxx` static classes to setup the PLC. For example, `S7PlcRx.S71500.Create(......` to setup a S7-1500 PLC.

#### Add Tag without Generic Type
```csharp
plc.AddUpdateTagItem(typeof(double), "Tag0", "DB500.DBD0");
```

#### Add Tag with Chainable Options
```csharp
plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0").SetTagPollIng(false)
.AddUpdateTagItem(typeof(double), "Tag1", "DB500.DBD8");
```

#### Remove Tag
```csharp
plc.RemoveTagItem("Tag0");
```

#### SetPolling

Polling is enabled by default when a Tag is added. You can disable and enable polling on a Tag at any time.
Expand Down Expand Up @@ -98,6 +116,20 @@ plc.GetCpuInfo().Subscribe(info =>
This returns a `CpuInfo` string Array with the following values:
AS Name, Module Name, Copyright, Serial Number, Module Type Name, Order Code, Version.

#### Using the Watchdog
The Watchdog is a feature that writes a value to a specific address in the PLC at a set interval.
This can be used to keep the PLC alive and prevent it from going into stop mode.

The functionallity of the PLC is down to the user to implement.

The recommended way to use the Watchdog is to use a timer to decrement a value in the PLC.
If the value reaches 0 then the PLC can be stopped.
The Watchdog will always write a value back to the PLC to reset the timer and resume normal operation.
```csharp
var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "PLC_IP_ADDRESS", 0, watchDogAddress, 5, watchDogValueToWrite, watchDogInterval);
```
The Watchdog is disabled by default. To enable the Watchdog set the `watchDogAddress`, it must be an address of type DBW. The `watchDogInterval` is the time in seconds between each write and the `watchDogValueToWrite` is the value to write to the PLC.

#### Supported Data Types

- Bool - DB?.DBX?.?
Expand Down
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CP.Nuke.BuildTools" Version="1.0.38" />
<PackageReference Include="CP.Nuke.BuildTools" Version="1.0.44" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.133" />
<PackageReference Include="Nuke.Common" Version="8.0.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.10",
"rollForward": "latestMinor"
"rollForward": "latestFeature"
}
}
6 changes: 3 additions & 3 deletions src/S7PlcRx.TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Reactive.Linq;
using S7PlcRx;

var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "172.16.17.1", 0, 1, interval: 5);
plc.LastError.Subscribe(ex => Console.WriteLine(ex));
plc.Status.Subscribe(status => Console.WriteLine(status));
var plc = S71500.Create("172.16.17.1", interval: 5);
plc.LastError.Subscribe(Console.WriteLine);
plc.Status.Subscribe(Console.WriteLine);
const string StartLogging = nameof(StartLogging);
const string PlcData = nameof(PlcData);
const string TestItems = nameof(TestItems);
Expand Down
31 changes: 31 additions & 0 deletions src/S7PlcRx/Create/S71200.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace S7PlcRx
{
/// <summary>
/// S71200.
/// </summary>
public static class S71200
{
/// <summary>
/// Creates the specified ip.
/// </summary>
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack default 0.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <returns>A IRxS7 instance.</returns>
public static IRxS7 Create(string ip, short rack = 0, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 100)
{
if (rack < 0 || rack > 7)
{
throw new ArgumentOutOfRangeException(nameof(rack), "Rack must be between 0 and 7");
}

return new RxS7(Enums.CpuType.S71200, ip, rack, 1, watchDogAddress, interval, watchDogValueToWrite, watchDogInterval);
}
}
}
37 changes: 37 additions & 0 deletions src/S7PlcRx/Create/S71500.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace S7PlcRx
{
/// <summary>
/// S71500.
/// </summary>
public static class S71500
{
/// <summary>
/// Creates the specified ip.
/// </summary>
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack default 0.</param>
/// <param name="slot">The slot default 1.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <returns>A IRxS7 instance.</returns>
public static IRxS7 Create(string ip, short rack = 0, short slot = 1, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 10)
{
if (rack < 0 || rack > 7)
{
throw new ArgumentOutOfRangeException(nameof(rack), "Rack must be between 0 and 7");
}

if (slot < 1 || slot > 31)
{
throw new ArgumentOutOfRangeException(nameof(slot), "Slot must be between 1 and 31");
}

return new RxS7(Enums.CpuType.S71500, ip, rack, slot, watchDogAddress, interval, watchDogValueToWrite, watchDogInterval);
}
}
}
25 changes: 25 additions & 0 deletions src/S7PlcRx/Create/S7200.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace S7PlcRx
{
/// <summary>
/// S7200.
/// </summary>
public static class S7200
{
/// <summary>
/// Creates the specified ip.
/// </summary>
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack.</param>
/// <param name="slot">The slot.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <returns>A IRxS7 instance.</returns>
public static IRxS7 Create(string ip, short rack, short slot, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 100) =>
new RxS7(Enums.CpuType.S7200, ip, rack, slot, watchDogAddress, interval, watchDogValueToWrite, watchDogInterval);
}
}
37 changes: 37 additions & 0 deletions src/S7PlcRx/Create/S7300.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace S7PlcRx
{
/// <summary>
/// S7300.
/// </summary>
public static class S7300
{
/// <summary>
/// Creates the specified ip.
/// </summary>
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack.</param>
/// <param name="slot">The slot.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <returns>A IRxS7 instance.</returns>
public static IRxS7 Create(string ip, short rack, short slot, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 100)
{
if (rack < 0 || rack > 7)
{
throw new ArgumentOutOfRangeException(nameof(rack), "Rack must be between 0 and 7");
}

if (slot < 1 || slot > 31)
{
throw new ArgumentOutOfRangeException(nameof(slot), "Slot must be between 1 and 31");
}

return new RxS7(Enums.CpuType.S7300, ip, rack, slot, watchDogAddress, interval, watchDogValueToWrite, watchDogInterval);
}
}
}
37 changes: 37 additions & 0 deletions src/S7PlcRx/Create/S7400.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace S7PlcRx
{
/// <summary>
/// S7400.
/// </summary>
public static class S7400
{
/// <summary>
/// Creates the specified ip.
/// </summary>
/// <param name="ip">The ip.</param>
/// <param name="rack">The rack.</param>
/// <param name="slot">The slot.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
/// <param name="interval">The interval.</param>
/// <param name="watchDogValueToWrite">The watch dog value to write.</param>
/// <param name="watchDogInterval">The watch dog interval.</param>
/// <returns>A IRxS7 instance.</returns>
public static IRxS7 Create(string ip, short rack, short slot, string? watchDogAddress = null, double interval = 100, ushort watchDogValueToWrite = 4500, int watchDogInterval = 100)
{
if (rack < 0 || rack > 7)
{
throw new ArgumentOutOfRangeException(nameof(rack), "Rack must be between 0 and 7");
}

if (slot < 1 || slot > 31)
{
throw new ArgumentOutOfRangeException(nameof(slot), "Slot must be between 1 and 31");
}

return new RxS7(Enums.CpuType.S7400, ip, rack, slot, watchDogAddress, interval, watchDogValueToWrite, watchDogInterval);
}
}
}
60 changes: 60 additions & 0 deletions src/S7PlcRx/ExtensionsMixins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this IRxS7 @this, stri
return (tag, @this);
}

/// <summary>
/// Adds the update tag item.
/// </summary>
/// <param name="this">The this.</param>
/// <param name="type">The type.</param>
/// <param name="tagName">Name of the tag.</param>
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>A Tag.</returns>
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem(this IRxS7 @this, Type type, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag);
if (@this is RxS7 plc && type != null)
{
if ((type == typeof(string) || type.IsArray) && arrayLength.HasValue)
{
tag = new(tagName, address, type, arrayLength.Value);
}
else
{
tag = new(tagName, address, type);
}

plc.AddUpdateTagItem(tag);
}

return (tag, @this);
}

/// <summary>
/// Adds the update tag item.
/// </summary>
Expand Down Expand Up @@ -68,6 +97,37 @@ public static (ITag? tag, IRxS7? plc) AddUpdateTagItem<T>(this (ITag? _, IRxS7?
return (tag, @this.plc);
}

/// <summary>
/// Adds the update tag item.
/// </summary>
/// <param name="this">The this.</param>
/// <param name="type">The type.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="address">The address.</param>
/// <param name="arrayLength">Length of the array.</param>
/// <returns>
/// A Tag.
/// </returns>
public static (ITag? tag, IRxS7? plc) AddUpdateTagItem(this (ITag? _, IRxS7? plc) @this, Type type, string tagName, string address, int? arrayLength = null)
{
var tag = default(Tag);
if (@this.plc is RxS7 plc && type != null)
{
if ((type == typeof(string) || type.IsArray) && arrayLength.HasValue)
{
tag = new(tagName, address, type, arrayLength.Value);
}
else
{
tag = new(tagName, address, type);
}

plc.AddUpdateTagItem(tag);
}

return (tag, @this.plc);
}

/// <summary>
/// Sets the tag to poll for values.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
IsConnected = _socketRx.IsConnected;

// Get the PLC connection status
_disposables.Add(IsConnected.Subscribe((Action<bool>)(x =>
_disposables.Add(IsConnected.Subscribe(x =>
{
IsConnectedValue = x;
_status.OnNext($"{DateTime.Now} - PLC Connected Status: {x}");
})));
}));

if (!string.IsNullOrWhiteSpace(watchDogAddress))
{
if (!watchDogAddress.Contains("DBW"))
if (watchDogAddress?.Contains("DBW") == false)
{
throw new ArgumentException("WatchDogAddress must be a DBW address.", nameof(watchDogAddress));
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ private IObservable<Unit> WatchDogObservable() =>
}

// Setup the watchdog
this.AddUpdateTagItem<ushort>("WatchDog", WatchDogAddress).SetTagPollIng(false);
this.AddUpdateTagItem<ushort>("WatchDog", WatchDogAddress!).SetTagPollIng(false);

var tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(WatchDogWritingTime)).Retry().Subscribe(_ =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/S7PlcRx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);IDE0042;IDE1006</NoWarn>
<NoWarn>$(NoWarn);IDE0042</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 3d55aac

Please sign in to comment.