Skip to content

Commit

Permalink
Use collection Initialisers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Dec 14, 2023
1 parent 04f5046 commit 79a33bc
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ dotnet_diagnostic.SA1006.severity = error
dotnet_diagnostic.SA1007.severity = error
dotnet_diagnostic.SA1008.severity = error
dotnet_diagnostic.SA1009.severity = error
dotnet_diagnostic.SA1010.severity = error
dotnet_diagnostic.SA1010.severity = none
dotnet_diagnostic.SA1011.severity = error
dotnet_diagnostic.SA1012.severity = error
dotnet_diagnostic.SA1013.severity = error
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "7.0.400"
"version": "8.0.10"
}
}
1 change: 1 addition & 0 deletions src/S7PlcRx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ProjectConfig", "ProjectCon
..\.gitignore = ..\.gitignore
..\.github\dependabot.yml = ..\.github\dependabot.yml
..\Directory.Build.props = ..\Directory.Build.props
..\global.json = ..\global.json
..\LICENSE = ..\LICENSE
..\README.md = ..\README.md
..\Images\S7PlcRx.ico = ..\Images\S7PlcRx.ico
Expand Down
8 changes: 4 additions & 4 deletions src/S7PlcRx/Core/S7SocketRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ public int Send(Tag tag, byte[] buffer, int size)
internal (byte[] data, ushort size) GetSZLData(ushort szlArea, ushort index = 0)
{
//// 0, 1, 2, 3 , 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19, 20,21, 22, 23,24,25, 26,27,28,29,30,31,32
byte[] s7_SZL1 = { 3, 0, 0, 33, 2, 240, 128, 50, 7, 0, 0, 5, 0, 0, 8, 0, 8, 0, 1, 18, 4, 17, 68, 1, 0, 255, 9, 0, 4, 0, 0, 0, 0 };
byte[] s7_SZL2 = { 3, 0, 0, 33, 2, 240, 128, 50, 7, 0, 0, 6, 0, 0, 12, 0, 4, 0, 1, 18, 8, 18, 68, 1, 1, 0, 0, 0, 0, 10, 0, 0, 0 };
byte[] s7_SZL1 = [3, 0, 0, 33, 2, 240, 128, 50, 7, 0, 0, 5, 0, 0, 8, 0, 8, 0, 1, 18, 4, 17, 68, 1, 0, 255, 9, 0, 4, 0, 0, 0, 0];
byte[] s7_SZL2 = [3, 0, 0, 33, 2, 240, 128, 50, 7, 0, 0, 6, 0, 0, 12, 0, 4, 0, 1, 18, 8, 18, 68, 1, 1, 0, 0, 0, 0, 10, 0, 0, 0];
const int size = 1024;
var data = new byte[size];
var resultData = new byte[size];
Expand Down Expand Up @@ -507,7 +507,7 @@ private async Task<bool> InitialiseSiemensConnectionAsync()
}

//// //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 };
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)
{
Expand Down Expand Up @@ -558,7 +558,7 @@ private async Task<bool> InitialiseSiemensConnectionAsync()
}

//// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25//
byte[] bsend2 = { 3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 4, 0, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 30 };
byte[] bsend2 = [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 4, 0, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 30];

// (23,24) PDU Length Requested = HI-LO Here Default 480 bytes
Word.ToByteArray(DataReadLength, bsend2, 23);
Expand Down
6 changes: 3 additions & 3 deletions src/S7PlcRx/PlcTypes/ByteArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace S7PlcRx.PlcTypes;

internal class ByteArray
{
private List<byte> _list = new();
private List<byte> _list;

/// <summary>
/// Initializes a new instance of the <see cref="ByteArray"/> class.
/// </summary>
public ByteArray() => _list = new();
public ByteArray() => _list = [];

/// <summary>
/// Initializes a new instance of the <see cref="ByteArray"/> class.
Expand Down Expand Up @@ -45,5 +45,5 @@ internal class ByteArray
/// <summary>
/// Clears this instance.
/// </summary>
public void Clear() => _list = new();
public void Clear() => _list = [];
}
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static void FromBytes(object? sourceClass, Type classType, byte[] bytes)
}

// hier auswerten
property.SetValue(sourceClass, LReal.FromByteArray(new byte[] { bytes[(int)numBytes], bytes[(int)numBytes + 1], bytes[(int)numBytes + 2], bytes[(int)numBytes + 3] }), null);
property.SetValue(sourceClass, LReal.FromByteArray([bytes[(int)numBytes], bytes[(int)numBytes + 1], bytes[(int)numBytes + 2], bytes[(int)numBytes + 3]]), null);
numBytes += 4;
break;

Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static ushort[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 2; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/DInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static int[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 4; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/DWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static uint[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 4; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/Int.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static short[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 2; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/LReal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static double[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes!.Length / 4; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
6 changes: 3 additions & 3 deletions src/S7PlcRx/PlcTypes/Real.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static float FromByteArray(byte[] bytes)
if (BitConverter.IsLittleEndian)
{
// create deep copy of the array and reverse
bytes = new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] };
bytes = [bytes[3], bytes[2], bytes[1], bytes[0]];
}

return BitConverter.ToSingle(bytes, 0);
Expand All @@ -39,7 +39,7 @@ public static byte[] ToByteArray(float value)
}

// create deep copy of the array and reverse
return new byte[] { bytes[3], bytes[2], bytes[1], bytes[0] };
return [bytes[3], bytes[2], bytes[1], bytes[0]];
}

/// <summary>
Expand Down Expand Up @@ -67,7 +67,7 @@ public static float[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 4; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++], bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
6 changes: 3 additions & 3 deletions src/S7PlcRx/PlcTypes/Struct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ internal static class Struct
}

// Evaluating here
info.SetValue(structValue, LReal.FromByteArray(new byte[]
{
info.SetValue(structValue, LReal.FromByteArray(
[
bytes[(int)numBytes],
bytes[(int)numBytes + 1],
bytes[(int)numBytes + 2],
bytes[(int)numBytes + 3]
}));
]));
numBytes += 4;
break;

Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static double[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 2; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/S7PlcRx/PlcTypes/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static ushort[] ToArray(byte[] bytes)
var counter = 0;
for (var cnt = 0; cnt < bytes.Length / 2; cnt++)
{
values[cnt] = FromByteArray(new byte[] { bytes[counter++], bytes[counter++] });
values[cnt] = FromByteArray([bytes[counter++], bytes[counter++]]);
}

return values;
Expand Down
59 changes: 33 additions & 26 deletions src/S7PlcRx/RxS7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ namespace S7PlcRx;
public class RxS7 : IRxS7
{
private readonly S7SocketRx _socketRx;
private readonly ISubject<Tag?> _dataRead = new Subject<Tag?>();
private readonly Subject<Tag?> _dataRead = new();
private readonly CompositeDisposable _disposables = new();
private readonly ISubject<string> _lastError = new Subject<string>();
private readonly ISubject<ErrorCode> _lastErrorCode = new Subject<ErrorCode>();
private readonly ISubject<PLCRequest> _pLCRequestSubject = new Subject<PLCRequest>();
private readonly ISubject<string> _status = new Subject<string>();
private readonly ISubject<long> _readTime = new Subject<long>();
private readonly Subject<string> _lastError = new Subject<string>();
private readonly Subject<ErrorCode> _lastErrorCode = new Subject<ErrorCode>();
private readonly Subject<PLCRequest> _pLCRequestSubject = new Subject<PLCRequest>();
private readonly Subject<string> _status = new Subject<string>();
private readonly Subject<long> _readTime = new Subject<long>();
private readonly SemaphoreSlim _lock = new(1);
private readonly SemaphoreSlim _lockTagList = new(1);
private readonly object _socketLock = new();
private readonly Stopwatch _stopwatch = new();
private readonly ISubject<bool> _paused = new Subject<bool>();
private readonly Subject<bool> _paused = new Subject<bool>();
private bool _pause;

/// <summary>
Expand Down Expand Up @@ -175,7 +175,7 @@ public RxS7(CpuType type, string ip, short rack, short slot, string? watchDogAdd
/// Gets the tag list. A) Name, B) Address, C) Value.
/// </summary>
/// <value>The tag list.</value>
public Tags TagList { get; } = new();
public Tags TagList { get; } = [];

/// <summary>
/// Gets the watch dog address.
Expand Down Expand Up @@ -305,7 +305,7 @@ public IObservable<string[]> GetCpuInfo() =>
$"V2: {orderCode.data[orderCode.size - 2]}", // Version 2
$"V3: {orderCode.data[orderCode.size - 1]}", // Version 3
};
obs.OnNext(l.ToArray());
obs.OnNext([.. l]);
obs.OnCompleted();
}
else
Expand Down Expand Up @@ -417,7 +417,14 @@ protected virtual void Dispose(bool disposing)
{//// ignored
}

_dataRead?.Dispose();
_lastError?.Dispose();
_socketRx?.Dispose();
_lastErrorCode?.Dispose();
_paused?.Dispose();
_pLCRequestSubject?.Dispose();
_status?.Dispose();
_readTime?.Dispose();
}

IsDisposed = true;
Expand All @@ -432,7 +439,7 @@ private static ByteArray CreateReadDataRequestPackage(DataType dataType, int db,
{
// single data register = 12
var package = new ByteArray(12);
package.Add(new byte[] { 18, 10, 16 });
package.Add([18, 10, 16]);
switch (dataType)
{
case DataType.Timer:
Expand Down Expand Up @@ -469,15 +476,15 @@ private static ByteArray ReadHeaderPackage(int amount = 1)
{
// header size = 19 bytes
var package = new ByteArray(19);
package.Add(new byte[] { 3, 0, 0 });
package.Add([3, 0, 0]);

// complete package size
package.Add((byte)(19 + (12 * amount)));
package.Add(new byte[] { 2, 240, 128, 50, 1, 0, 0, 0, 0 });
package.Add([2, 240, 128, 50, 1, 0, 0, 0, 0]);

// data part size
package.Add(Word.ToByteArray((ushort)(2 + (amount * 12))));
package.Add(new byte[] { 0, 0, 4 });
package.Add([0, 0, 4]);

// amount of requests
package.Add((byte)amount);
Expand Down Expand Up @@ -551,20 +558,20 @@ private bool WriteBytes(Tag tag, DataType dataType, int db, int startByteAdr, by
var packageSize = 35 + value.Length;
var package = new ByteArray(packageSize);

package.Add(new byte[] { 3, 0, 0 });
package.Add([3, 0, 0]);
package.Add((byte)packageSize);
package.Add(new byte[] { 2, 240, 128, 50, 1, 0, 0 });
package.Add([2, 240, 128, 50, 1, 0, 0]);
package.Add(Word.ToByteArray((ushort)(varCount - 1)));
package.Add(new byte[] { 0, 14 });
package.Add([0, 14]);
package.Add(Word.ToByteArray((ushort)(varCount + 4)));
package.Add(new byte[] { 5, 1, 18, 10, 16, 2 });
package.Add([5, 1, 18, 10, 16, 2]);
package.Add(Word.ToByteArray((ushort)varCount));
package.Add(Word.ToByteArray((ushort)db));
package.Add((byte)dataType);
var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191
package.Add((byte)overflow);
package.Add(Word.ToByteArray((ushort)(startByteAdr * 8)));
package.Add(new byte[] { 0, 4 });
package.Add([0, 4]);
package.Add(Word.ToByteArray((ushort)(varCount * 8)));

// now join the header and the data
Expand Down Expand Up @@ -777,7 +784,7 @@ private void GetTagValue(Tag? tag)
switch (correctVariable!.Substring(0, 2))
{
case "DB":
var strings = correctVariable.Split(new char[] { '.' });
var strings = correctVariable.Split(['.']);
if (strings.Length < 2)
{
throw new Exception();
Expand Down Expand Up @@ -1163,15 +1170,15 @@ private byte[] ReadMultipleBytes(Tag tag, DataType dataType, int db, int startBy

if (bytes == null)
{
return Array.Empty<byte>();
return [];
}

resultBytes.AddRange(bytes);
numBytes -= maxToRead;
index += maxToRead;
}

return resultBytes.ToArray();
return [.. resultBytes];
}

private IObservable<Unit> TagReaderObservable(double interval) =>
Expand Down Expand Up @@ -1211,7 +1218,7 @@ private IObservable<Unit> TagReaderObservable(double interval) =>
catch (Exception ex)
{
_lastError.OnNext(ex.Message);
_status.OnNext($"{tag.Name} could not be read from {tag.Address}. Error: " + ex.ToString());
_status.OnNext($"{tag.Name} could not be read from {tag.Address}. Error: " + ex);
}
}

Expand Down Expand Up @@ -1288,7 +1295,7 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?
{
case "Boolean":
case "Byte":
package = new byte[] { (byte)(value ?? Convert.ChangeType(tag.NewValue, typeof(byte)))! };
package = [(byte)(value ?? Convert.ChangeType(tag.NewValue, typeof(byte)))!];
break;

case "Int16":
Expand All @@ -1303,7 +1310,7 @@ private bool Write(Tag tag, DataType dataType, int db, int startByteAdr, object?

var parsed = ushort.Parse(value.ToString()!);
var vOut = BitConverter.GetBytes(parsed);
package = new byte[] { vOut[1], vOut[0] };
package = [vOut[1], vOut[0]];
break;

case "ushort":
Expand Down Expand Up @@ -1391,7 +1398,7 @@ private bool WriteMultipleBytes(Tag tag, List<byte> bytes, int db, int startByte
{
var maxToWrite = Math.Min(bytes.Count, 200);
var part = bytes.ToList().GetRange(0, maxToWrite);
errCode = WriteBytes(tag, DataType.DataBlock, db, index, part.ToArray());
errCode = WriteBytes(tag, DataType.DataBlock, db, index, [.. part]);
bytes.RemoveRange(0, maxToWrite);
index += maxToWrite;
if (!errCode)
Expand Down Expand Up @@ -1432,7 +1439,7 @@ private bool WriteString(Tag? tag)
switch (tagAddress.Substring(0, 2))
{
case "DB":
var strings = tagAddress.Split(new char[] { '.' });
var strings = tagAddress.Split(['.']);
if (strings.Length < 2)
{
throw new Exception();
Expand Down

0 comments on commit 79a33bc

Please sign in to comment.