Skip to content

Commit

Permalink
Merge pull request #31 from ChrisPulman/ChangeGetCpuInfoToObservable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman authored Aug 22, 2023
2 parents 6f334cd + 6c4c511 commit 6e8a12f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 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.2.0",
"version": "1.2.1",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
14 changes: 14 additions & 0 deletions src/S7PlcRx/IRxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public interface IRxS7 : ICancelable
/// </value>
IObservable<bool> IsConnected { get; }

/// <summary>
/// Gets a value indicating whether this instance is connected.
/// </summary>
/// <value>
/// <c>true</c> if this instance is connected; otherwise, <c>false</c>.
/// </value>
bool IsConnectedValue { get; }

/// <summary>
/// Gets the last error.
/// </summary>
Expand Down Expand Up @@ -154,4 +162,10 @@ public interface IRxS7 : ICancelable
/// <param name="variable">The variable.</param>
/// <param name="value">The value.</param>
void Value<T>(string? variable, T? value);

/// <summary>
/// Gets the cpu information. AS Name, Module Name, Copyright, Serial Number, Module Type Name, Order Code, Version.
/// </summary>
/// <returns>A string Array.</returns>
IObservable<string[]> GetCpuInfo();
}
113 changes: 73 additions & 40 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class RxS7 : IRxS7
private readonly object _socketLock = new();
private readonly Stopwatch _stopwatch = new();
private readonly ISubject<bool> _paused = new Subject<bool>();
private bool _isConnected;
private bool _pause;

/// <summary>
Expand All @@ -59,11 +58,11 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
IsConnected = _socketRx.IsConnected;

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

if (!string.IsNullOrWhiteSpace(watchDogAddress))
{
Expand Down Expand Up @@ -118,6 +117,14 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// </value>
public IObservable<bool> IsConnected { get; }

/// <summary>
/// Gets a value indicating whether this instance is connected.
/// </summary>
/// <value>
/// <c>true</c> if this instance is connected; otherwise, <c>false</c>.
/// </value>
public bool IsConnectedValue { get; private set; }

/// <summary>
/// Gets the last error.
/// </summary>
Expand Down Expand Up @@ -262,43 +269,61 @@ public void Dispose()
/// <returns>
/// A String Array.
/// </returns>
public async Task<string[]> GetCpuInfo()
{
errorCpuData:
var cpuData = _socketRx.GetSZLData(28);
if (cpuData.data.Length == 0)
public IObservable<string[]> GetCpuInfo() =>
Observable.Create<string[]>(obs =>
{
await Task.Delay(100).ConfigureAwait(true);
goto errorCpuData;
}
var d = new CompositeDisposable();
d.Add(IsConnected.Where(x => x).Take(1).Subscribe(async _ =>
{
errorCpuData:
var cpuData = _socketRx.GetSZLData(28);
if (cpuData.data.Length == 0 && !d.IsDisposed)
{
await Task.Delay(10).ConfigureAwait(true);
goto errorCpuData;
}

errororder:
var orderCode = _socketRx.GetSZLData(17);
if (orderCode.data.Length == 0)
{
await Task.Delay(100).ConfigureAwait(true);
goto errororder;
}
errororder:
var orderCode = _socketRx.GetSZLData(17);
if (orderCode.data.Length == 0 && !d.IsDisposed)
{
await Task.Delay(10).ConfigureAwait(true);
goto errororder;
}

if (cpuData.data.Length > 0 && orderCode.data.Length > 0)
{
var l = new List<string>
{
PlcTypes.String.FromByteArray(cpuData.data, 2, 24).Replace("\0", string.Empty), // AS Name
PlcTypes.String.FromByteArray(cpuData.data, 36, 24).Replace("\0", string.Empty), // Module Name
PlcTypes.String.FromByteArray(cpuData.data, 104, 26).Replace("\0", string.Empty), // Copyright
PlcTypes.String.FromByteArray(cpuData.data, 138, 24).Replace("\0", string.Empty), // Serial Number
PlcTypes.String.FromByteArray(cpuData.data, 172, 32).Replace("\0", string.Empty), // Module Type Name
PlcTypes.String.FromByteArray(orderCode.data, 2, 20).Replace("\0", string.Empty), // Order Code
$"V1: {orderCode.data[orderCode.size - 3]}", // Version 1
$"V2: {orderCode.data[orderCode.size - 2]}", // Version 2
$"V3: {orderCode.data[orderCode.size - 1]}", // Version 3
};
return l.ToArray();
}
if (cpuData.data.Length >= 204 && orderCode.data.Length >= 25)
{
var l = new List<string>
{
PlcTypes.String.FromByteArray(cpuData.data, 2, 24).Replace("\0", string.Empty), // AS Name
PlcTypes.String.FromByteArray(cpuData.data, 36, 24).Replace("\0", string.Empty), // Module Name
PlcTypes.String.FromByteArray(cpuData.data, 104, 26).Replace("\0", string.Empty), // Copyright
PlcTypes.String.FromByteArray(cpuData.data, 138, 24).Replace("\0", string.Empty), // Serial Number
PlcTypes.String.FromByteArray(cpuData.data, 172, 32).Replace("\0", string.Empty), // Module Type Name
PlcTypes.String.FromByteArray(orderCode.data, 2, 20).Replace("\0", string.Empty), // Order Code
$"V1: {orderCode.data[orderCode.size - 3]}", // Version 1
$"V2: {orderCode.data[orderCode.size - 2]}", // Version 2
$"V3: {orderCode.data[orderCode.size - 1]}", // Version 3
};
obs.OnNext(l.ToArray());
obs.OnCompleted();
}
else
{
if (cpuData.data.Length < 204)
{
goto errorCpuData;
}

return Array.Empty<string>();
}
if (orderCode.data.Length < 25)
{
goto errororder;
}
}
}));

return d;
});

/// <summary>
/// Writes a C# class to a DB in the PLC.
Expand Down Expand Up @@ -1075,7 +1100,7 @@ private IObservable<Unit> TagReaderObservable(double interval) =>
var tim = Observable.Interval(TimeSpan.FromMilliseconds(interval))
.Subscribe(async _ =>
{
if (_isConnected)
if (IsConnectedValue)
{
var tagList = TagList.ToList().Where(t => !t.DoNotPoll);
if (!tagList.Any() || _pause)
Expand All @@ -1096,7 +1121,7 @@ private IObservable<Unit> TagReaderObservable(double interval) =>

try
{
while (!_isConnected)
while (!IsConnectedValue)
{
await Task.Delay(10);
}
Expand Down Expand Up @@ -1135,7 +1160,7 @@ private IObservable<Unit> WatchDogObservable() =>
AddUpdateTagItem(wd);
var tim = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(WatchDogWritingTime)).Retry().Subscribe(_ =>
{
if (_isConnected)
if (IsConnectedValue)
{
wd.Value = WatchDogValueToWrite;
_pLCRequestSubject.OnNext(new PLCRequest(PLCRequestType.Write, wd));
Expand Down Expand Up @@ -1212,6 +1237,10 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
package = DWord.ToByteArray((uint)Convert.ChangeType(tag.NewValue, typeof(uint))!);
break;

case "Single":
package = Real.ToByteArray((float)tag.NewValue!);
break;

case "Double":
package = LReal.ToByteArray((double)tag.NewValue!);
break;
Expand Down Expand Up @@ -1246,6 +1275,10 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
package = DWord.ToByteArray((uint[])Convert.ChangeType(tag.NewValue, typeof(uint[]))!);
break;

case "Single[]":
package = Real.ToByteArray((float[])tag.NewValue!);
break;

case "Double[]":
package = LReal.ToByteArray((double[])tag.NewValue!);
break;
Expand Down

0 comments on commit 6e8a12f

Please sign in to comment.