Skip to content

Commit

Permalink
Add Interval for Observers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed May 19, 2023
1 parent 0db95a0 commit 87f60d9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
10 changes: 6 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<LangVersion>preview</LangVersion>
<Configuration>$(TargetFramework)</Configuration>
<Company>ChrisPulman</Company>
<NoWarn>CS1591</NoWarn>
<NoWarn>CS1591;RCS1198;CA2208</NoWarn>
<Nullable>enable</Nullable>
<PackageIcon>S7PlcRx.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Configurations>Debug;Release;PreRelease</Configurations>
<PackageReleaseNotes>Compatability with Net 6 and netstandard2.0</PackageReleaseNotes>
<PackageReleaseNotes>Compatability with Net 6 / 7 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 @@ -48,6 +49,7 @@
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)images\S7PlcRx.png" Pack="true" PackagePath="\"/>
<None Include="$(MSBuildThisFileDirectory)LICENSE" Pack="true" PackagePath="LICENSE" />
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<ItemGroup Condition="'$(IsTestProject)' == 'true'">
Expand All @@ -66,9 +68,9 @@

<ItemGroup>
<!--<Compile Update="**\*.cs" DependentUpon="I%(Filename).cs" />-->
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.119" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.132" PrivateAssets="all" />
<PackageReference Include="stylecop.analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0" PrivateAssets="All" />
<PackageReference Include="Roslynator.Analyzers" Version="4.3.0" PrivateAssets="All" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Link="stylecop.json" />
</ItemGroup>
</Project>
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,43 @@

# S7PlcRx
S7 PLC Communications Library

## Introduction
S7PlcRx is a library that provides a simple interface to communicate with Siemens S7 PLCs.

## Features
- Read and Write to PLC
- Read and Write to PLC with Subscription


## Getting Started
### Installation
S7PlcRx is available on [NuGet](https://www.nuget.org/packages/S7PlcRx/).

#### Package Manager
```powershell
Install-Package S7PlcRx
```

#### .NET CLI
```powershell
dotnet add package S7PlcRx
```

### Usage
#### Setup Tags and Observe values in PLC
```csharp
using S7PlcRx;

var plc = new RxS7(S7PlcRx.Enums.CpuType.S71500, "", 0, 1);
plc.AddUpdateTagItem<double>("Tag0", "DB500.DBD0");
plc.AddUpdateTagItem<double>("Tag1", "DB500.DBD8");

plc.Observe<double>("Tag0").Subscribe(x => Console.WriteLine($"Tag0: {x}"));
plc.Observe<double>("Tag1").Subscribe(x => Console.WriteLine($"Tag1: {x}"));
```

#### Write to PLC
```csharp
plc.Value<double>("Tag0", 1.0);
```
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0.2",
"version": "1.0.3",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
13 changes: 7 additions & 6 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RxS7 : IRxS7
private readonly ISubject<PLCRequest> _pLCRequestSubject = new Subject<PLCRequest>();
private readonly ISubject<Unit> _restartReadCycle = new Subject<Unit>();
private readonly ISubject<string> _status = new Subject<string>();
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1);
private readonly SemaphoreSlim _lock = new(1);
private bool _isConnected;

/// <summary>
Expand All @@ -36,7 +36,8 @@ public class RxS7 : IRxS7
/// <param name="rack">The rack.</param>
/// <param name="slot">The slot.</param>
/// <param name="watchDogAddress">The watch dog address.</param>
public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAddress = null)
/// <param name="interval">The interval to observe on.</param>
public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAddress = null, double interval = 100)
{
PLCType = type;
IP = ip;
Expand Down Expand Up @@ -66,7 +67,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
_disposables.Add(WatchDogObservable().Subscribe());
}

_disposables.Add(TagReaderObservable().Subscribe());
_disposables.Add(TagReaderObservable(interval).Subscribe());

_disposables.Add(_pLCRequestSubject.Subscribe(request =>
{
Expand Down Expand Up @@ -265,7 +266,7 @@ internal void AddUpdateTagItem(Tag tag)
}

_lock.Wait();
if (TagList[tag.Name!] is Tag tagExists)
if (TagList[tag!.Name!] is Tag tagExists)
{
tagExists.Name = tag.Name;
tagExists.Value = tag.Value;
Expand Down Expand Up @@ -936,12 +937,12 @@ private List<byte> ReadMultipleBytes(Tag tag, int numBytes, int db, int startByt
return resultBytes;
}

private IObservable<Unit> TagReaderObservable() =>
private IObservable<Unit> TagReaderObservable(double interval) =>
Observable.Create<Unit>(__ =>
{
var isReading = false;
var tim = _restartReadCycle
.Throttle(TimeSpan.FromMilliseconds(100))
.Throttle(TimeSpan.FromMilliseconds(interval))
.StartWith(default(Unit))
.Subscribe(async _ =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/S7PlcRx.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions src/S7PlcRx/TagAddressOutOfRangeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ namespace S7PlcRx
/// <seealso cref="System.ArgumentOutOfRangeException" />
[Serializable]
#pragma warning disable RCS1194 // Implement exception constructors.
#pragma warning disable CA1032 // Implement standard exception constructors
public class TagAddressOutOfRangeException : ArgumentOutOfRangeException
#pragma warning restore CA1032 // Implement standard exception constructors
#pragma warning restore RCS1194 // Implement exception constructors.
{
/// <summary>
/// Initializes a new instance of the <see cref="TagAddressOutOfRangeException"/> class.
/// </summary>
/// <param name="tag">The Tag that caused the exception.</param>
public TagAddressOutOfRangeException(Tag tag)
public TagAddressOutOfRangeException(Tag? tag)
: base(nameof(tag.Address))
{
}
Expand Down

0 comments on commit 87f60d9

Please sign in to comment.