Skip to content

Commit

Permalink
Merge pull request #28 from ChrisPulman/TidyUpFixDispose
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman authored Aug 2, 2023
2 parents 9d10cf8 + 1c5c16b commit f42414e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 68 deletions.
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.1.2",
"version": "1.1.3",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
1 change: 1 addition & 0 deletions src/S7PlcRx.TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
});
Expand Down
31 changes: 3 additions & 28 deletions src/S7PlcRx/Core/S7SocketRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public int Receive(Tag tag, byte[] buffer, int size)
var r = _socket?.Receive(buffer, size, SocketFlags.None);
if (tag != null && Debugger.IsAttached)
{
var res = buffer[21] == 0xff ? Success : Failed;
var res = buffer[21] == 255 ? Success : Failed;
Debug.WriteLine($"{DateTime.Now} Read Tag: {tag.Name} value: {tag.Value} {res}");
}

Expand Down Expand Up @@ -373,57 +373,32 @@ private bool InitialiseSiemensConnection()
return false;
}

//// //1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22//
//// //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21//
byte[] bSend1 = { 3, 0, 0, 22, 17, 224, 0, 0, 0, 46, 0, 193, 2, 1, 0, 194, 2, 3, 0, 192, 1, 9 };

switch (PLCType)
{
case CpuType.S7200:
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 16;
bSend1[14] = 0;
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 16;
bSend1[18] = 0;
_dataReadLength = 480;
break;

case CpuType.S71200:
case CpuType.S7300:
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 1;
bSend1[14] = 0;
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 3;
bSend1[18] = (byte)((Rack * 2 * 16) + Slot);
_dataReadLength = 480;
break;

case CpuType.S7400:
bSend1[11] = 193;
bSend1[12] = 2;
case CpuType.S7300:
bSend1[13] = 1;
bSend1[14] = 0;
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 3;
bSend1[18] = (byte)((Rack * 2 * 16) + Slot);
_dataReadLength = 480;
break;

case CpuType.S71500:

// Private
bSend1[11] = 193;
bSend1[12] = 2;
bSend1[13] = 16;
bSend1[14] = 2;
bSend1[15] = 194;
bSend1[16] = 2;
bSend1[17] = 3;
bSend1[18] = (byte)((Rack * 2 * 16) + Slot);
_dataReadLength = 960;
Expand Down
78 changes: 39 additions & 39 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Runtime.CompilerServices;
using S7PlcRx.Core;
using S7PlcRx.Enums;
using S7PlcRx.PlcTypes;
Expand Down Expand Up @@ -56,7 +57,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
// Create an observable socket
_socketRx = new(IP, type, rack, slot);

IsConnected = _socketRx.IsConnected.Publish().RefCount();
IsConnected = _socketRx.IsConnected;

// Get the PLC connection status
_disposables.Add(IsConnected.Subscribe(x =>
Expand Down Expand Up @@ -106,7 +107,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// <value>
/// <c>true</c> if this instance is paused; otherwise, <c>false</c>.
/// </value>
public IObservable<bool> IsPaused => _paused.DistinctUntilChanged();
public IObservable<bool> IsPaused => _paused.DistinctUntilChanged().Publish().RefCount();

/// <summary>
/// Gets the ip address.
Expand All @@ -128,15 +129,15 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// Gets the last error.
/// </summary>
/// <value>The last error.</value>
public IObservable<string> LastError => _lastError.AsObservable();
public IObservable<string> LastError => _lastError.Publish().RefCount();

/// <summary>
/// Gets the last error code registered when executing a function.
/// </summary>
/// <value>
/// The last error code.
/// </value>
public IObservable<ErrorCode> LastErrorCode => _lastErrorCode.AsObservable();
public IObservable<ErrorCode> LastErrorCode => _lastErrorCode.Publish().RefCount();

/// <summary>
/// Gets the type of the PLC.
Expand Down Expand Up @@ -168,7 +169,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// <value>
/// The status.
/// </value>
public IObservable<string> Status => _status.AsObservable();
public IObservable<string> Status => _status.Publish().RefCount();

/// <summary>
/// Gets the tag list. A) Name, B) Address, C) Value.
Expand Down Expand Up @@ -205,7 +206,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// <value>
/// The read time.
/// </value>
public IObservable<long> ReadTime => _readTime.AsObservable();
public IObservable<long> ReadTime => _readTime.Publish().RefCount();

/// <summary>
/// Observes the specified variable.
Expand All @@ -215,7 +216,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// <returns>An Observable of T.</returns>
public IObservable<T?> Observe<T>(string? variable) =>
ObserveAll
.Where(t => t?.Name == variable && t?.Value.GetType() == typeof(T))
.Where(t => TagValueIsValid<T>(t, variable))
.Select(t => (T?)t?.Value)
.Retry()
.Publish()
Expand All @@ -230,7 +231,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
public async Task<T?> Value<T>(string? variable)
{
_pause = true;
var p = await _paused.Where(x => x).FirstAsync();
_ = await _paused.Where(x => x).FirstAsync();
var tag = TagList[variable!];
GetTagValue(tag);
_pause = false;
Expand Down Expand Up @@ -335,11 +336,25 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_lock.Wait();
_lock.Dispose();
_lockTagList.Wait();
_lockTagList.Dispose();
_disposables.Dispose();
try
{
_lock.Wait();
_lock.Dispose();
}
catch
{//// ignored
}

try
{
_lockTagList.Wait();
_lockTagList.Dispose();
}
catch
{//// ignored
}

_socketRx?.Dispose();
}

Expand All @@ -349,6 +364,8 @@ protected virtual void Dispose(bool disposing)

private static bool TagValueIsValid<T>(Tag? tag) => tag != null && tag.Type == typeof(T) && tag.Value.GetType() == typeof(T);

private static bool TagValueIsValid<T>(Tag? tag, string? variable) => tag?.Name == variable && tag?.Type == typeof(T) && tag.Value.GetType() == typeof(T);

private static ByteArray CreateReadDataRequestPackage(DataType dataType, int db, int startByteAdr, int count = 1)
{
// single data register = 12
Expand All @@ -369,7 +386,7 @@ private static ByteArray CreateReadDataRequestPackage(DataType dataType, int db,
package.Add(Word.ToByteArray((ushort)count));
package.Add(Word.ToByteArray((ushort)db));
package.Add((byte)dataType);
var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191
var overflow = (int)(startByteAdr * 8 / 65535U); // handles words with address bigger than 8191
package.Add((byte)overflow);
switch (dataType)
{
Expand All @@ -396,33 +413,16 @@ private static ByteArray CreateReadDataRequestPackage(DataType dataType, int db,
/// <returns>The Tag as a string.</returns>
private static string GetTagAddress(DataType dataType, int startAddress, VarType type, int offset = 1)
{
var description = string.Empty;
switch (dataType)
var description = dataType switch
{
case DataType.Input:
description = "I";
break;

case DataType.Output:
description = "O";
break;

case DataType.Memory:
description = "M";
break;

case DataType.DataBlock:
description = "DB";
break;

case DataType.Timer:
description = "T";
break;

case DataType.Counter:
description = "C";
break;
}
DataType.Input => "I",
DataType.Output => "O",
DataType.Memory => "M",
DataType.DataBlock => "DB",
DataType.Timer => "T",
DataType.Counter => "C",
_ => string.Empty,
};

description += type switch
{
Expand Down

0 comments on commit f42414e

Please sign in to comment.