diff --git a/Sharp7.Rx.Tests/VariableAddressTests/MatchesType.cs b/Sharp7.Rx.Tests/VariableAddressTests/MatchesType.cs index abaeae5..0feffb3 100644 --- a/Sharp7.Rx.Tests/VariableAddressTests/MatchesType.cs +++ b/Sharp7.Rx.Tests/VariableAddressTests/MatchesType.cs @@ -61,10 +61,10 @@ public class MatchesType // Explicitly remove some valid combinations .Where(tc => !( - (tc.Type == typeof(string) && tc.Address == "DB99.Byte5") || - (tc.Type == typeof(string) && tc.Address == "DB99.Byte5.4") || - (tc.Type == typeof(byte[]) && tc.Address == "DB99.Byte5") - )) + (tc.Type == typeof(string) && tc.Address == "DB99.Byte5") || + (tc.Type == typeof(string) && tc.Address == "DB99.Byte5.4") || + (tc.Type == typeof(byte[]) && tc.Address == "DB99.Byte5") + )) ; } diff --git a/Sharp7.Rx.Tests/VariableNameParserTests.cs b/Sharp7.Rx.Tests/VariableNameParserTests.cs index 577f7d0..4ecf1c5 100644 --- a/Sharp7.Rx.Tests/VariableNameParserTests.cs +++ b/Sharp7.Rx.Tests/VariableNameParserTests.cs @@ -20,7 +20,6 @@ internal class VariableNameParserTests [TestCase("DB506.Bit216.8", TestName = "Bit to high")] [TestCase("DB506.String216", TestName = "String without Length")] [TestCase("DB506.WString216", TestName = "WString without Length")] - [TestCase("DB506.Int216.1", TestName = "Int with Length")] [TestCase("DB506.UInt216.1", TestName = "UInt with Length")] [TestCase("DB506.DInt216.1", TestName = "DInt with Length")] @@ -29,7 +28,6 @@ internal class VariableNameParserTests [TestCase("DB506.ULInt216.1", TestName = "ULInt with Length")] [TestCase("DB506.Real216.1", TestName = "LReal with Length")] [TestCase("DB506.LReal216.1", TestName = "LReal with Length")] - [TestCase("DB506.xx216", TestName = "Invalid type")] [TestCase("DB506.216", TestName = "No type")] [TestCase("DB506.Int216.", TestName = "Trailing dot")] @@ -49,39 +47,39 @@ internal class VariableNameParserTests public static IEnumerable ValidTestCases() { - yield return new TestCase("DB506.Bit216.2", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Bit = 2, Type = DbType.Bit}); + yield return new TestCase("DB506.Bit216.2", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Bit, Start: 216, Length: 1, Bit: 2)); - yield return new TestCase("DB506.String216.10", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 10, Type = DbType.String}); - yield return new TestCase("DB506.WString216.10", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 10, Type = DbType.WString}); + yield return new TestCase("DB506.String216.10", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.String, Start: 216, Length: 10)); + yield return new TestCase("DB506.WString216.10", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.WString, Start: 216, Length: 10)); - yield return new TestCase("DB506.Byte216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Type = DbType.Byte}); - yield return new TestCase("DB506.Byte216.100", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 100, Type = DbType.Byte}); - yield return new TestCase("DB506.Int216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int}); - yield return new TestCase("DB506.UInt216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.UInt}); - yield return new TestCase("DB506.DInt216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.DInt}); - yield return new TestCase("DB506.UDInt216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.UDInt}); - yield return new TestCase("DB506.LInt216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.LInt}); - yield return new TestCase("DB506.ULInt216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt}); + yield return new TestCase("DB506.Byte216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Byte, Start: 216, Length: 1)); + yield return new TestCase("DB506.Byte216.100", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Byte, Start: 216, Length: 100)); + yield return new TestCase("DB506.Int216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Int, Start: 216, Length: 2)); + yield return new TestCase("DB506.UInt216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.UInt, Start: 216, Length: 2)); + yield return new TestCase("DB506.DInt216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.DInt, Start: 216, Length: 4)); + yield return new TestCase("DB506.UDInt216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.UDInt, Start: 216, Length: 4)); + yield return new TestCase("DB506.LInt216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.LInt, Start: 216, Length: 8)); + yield return new TestCase("DB506.ULInt216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.ULInt, Start: 216, Length: 8)); - yield return new TestCase("DB506.Real216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.Single}); - yield return new TestCase("DB506.LReal216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.Double}); + yield return new TestCase("DB506.Real216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Single, Start: 216, Length: 4)); + yield return new TestCase("DB506.LReal216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Double, Start: 216, Length: 8)); // Legacy - yield return new TestCase("DB13.DBX3.1", new VariableAddress {Operand = Operand.Db, DbNr = 13, Start = 3, Length = 1, Bit = 1, Type = DbType.Bit}); - yield return new TestCase("Db403.X5.2", new VariableAddress {Operand = Operand.Db, DbNr = 403, Start = 5, Length = 1, Bit = 2, Type = DbType.Bit}); - yield return new TestCase("DB55DBX23.6", new VariableAddress {Operand = Operand.Db, DbNr = 55, Start = 23, Length = 1, Bit = 6, Type = DbType.Bit}); - yield return new TestCase("DB1.S255.20", new VariableAddress {Operand = Operand.Db, DbNr = 1, Start = 255, Length = 20, Type = DbType.String}); - yield return new TestCase("DB5.String887.20", new VariableAddress {Operand = Operand.Db, DbNr = 5, Start = 887, Length = 20, Type = DbType.String}); - yield return new TestCase("DB506.B216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Type = DbType.Byte}); - yield return new TestCase("DB506.DBB216.5", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 5, Type = DbType.Byte}); - yield return new TestCase("DB506.D216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.Single}); - yield return new TestCase("DB506.DINT216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.DInt}); - yield return new TestCase("DB506.INT216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int}); - yield return new TestCase("DB506.DBW216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int}); - yield return new TestCase("DB506.DUL216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt}); - yield return new TestCase("DB506.DULINT216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt}); - yield return new TestCase("DB506.DULONG216", new VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt}); + yield return new TestCase("DB13.DBX3.1", new VariableAddress(Operand: Operand.Db, DbNo: 13, Type: DbType.Bit, Start: 3, Length: 1, Bit: 1)); + yield return new TestCase("Db403.X5.2", new VariableAddress(Operand: Operand.Db, DbNo: 403, Type: DbType.Bit, Start: 5, Length: 1, Bit: 2)); + yield return new TestCase("DB55DBX23.6", new VariableAddress(Operand: Operand.Db, DbNo: 55, Type: DbType.Bit, Start: 23, Length: 1, Bit: 6)); + yield return new TestCase("DB1.S255.20", new VariableAddress(Operand: Operand.Db, DbNo: 1, Type: DbType.String, Start: 255, Length: 20)); + yield return new TestCase("DB5.String887.20", new VariableAddress(Operand: Operand.Db, DbNo: 5, Type: DbType.String, Start: 887, Length: 20)); + yield return new TestCase("DB506.B216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Byte, Start: 216, Length: 1)); + yield return new TestCase("DB506.DBB216.5", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Byte, Start: 216, Length: 5)); + yield return new TestCase("DB506.D216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Single, Start: 216, Length: 4)); + yield return new TestCase("DB506.DINT216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.DInt, Start: 216, Length: 4)); + yield return new TestCase("DB506.INT216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Int, Start: 216, Length: 2)); + yield return new TestCase("DB506.DBW216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.Int, Start: 216, Length: 2)); + yield return new TestCase("DB506.DUL216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.ULInt, Start: 216, Length: 8)); + yield return new TestCase("DB506.DULINT216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.ULInt, Start: 216, Length: 8)); + yield return new TestCase("DB506.DULONG216", new VariableAddress(Operand: Operand.Db, DbNo: 506, Type: DbType.ULInt, Start: 216, Length: 8)); } public record TestCase(string Input, VariableAddress Expected) diff --git a/Sharp7.Rx/AssemblyInfo.cs b/Sharp7.Rx/AssemblyInfo.cs index e92998f..d9d730c 100644 --- a/Sharp7.Rx/AssemblyInfo.cs +++ b/Sharp7.Rx/AssemblyInfo.cs @@ -1,3 +1,3 @@ using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo("Sharp7.Rx.Tests")] \ No newline at end of file +[assembly: InternalsVisibleTo("Sharp7.Rx.Tests")] diff --git a/Sharp7.Rx/Extensions/OperandExtensions.cs b/Sharp7.Rx/Extensions/OperandExtensions.cs index c78cfa5..b2c6744 100644 --- a/Sharp7.Rx/Extensions/OperandExtensions.cs +++ b/Sharp7.Rx/Extensions/OperandExtensions.cs @@ -13,5 +13,4 @@ internal static class OperandExtensions Operand.Db => S7Area.DB, _ => throw new ArgumentOutOfRangeException(nameof(operand), operand, null) }; - } diff --git a/Sharp7.Rx/Extensions/PlcExtensions.cs b/Sharp7.Rx/Extensions/PlcExtensions.cs index b1f1cd2..af2738d 100644 --- a/Sharp7.Rx/Extensions/PlcExtensions.cs +++ b/Sharp7.Rx/Extensions/PlcExtensions.cs @@ -9,7 +9,8 @@ namespace Sharp7.Rx.Extensions; public static class PlcExtensions { - public static IObservable CreateDatatransferWithHandshake(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func> readData, bool initialTransfer) + public static IObservable CreateDatatransferWithHandshake(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func> readData, + bool initialTransfer) { return Observable.Create(async observer => { diff --git a/Sharp7.Rx/Extensions/S7VariableExtensions.cs b/Sharp7.Rx/Extensions/S7VariableExtensions.cs index ae04de3..fa1c87f 100644 --- a/Sharp7.Rx/Extensions/S7VariableExtensions.cs +++ b/Sharp7.Rx/Extensions/S7VariableExtensions.cs @@ -7,17 +7,17 @@ internal static class VariableAddressExtensions private static readonly Dictionary> supportedTypeMap = new() { {typeof(bool), a => a.Type == DbType.Bit}, - {typeof(string), a => a.Type is DbType.String or DbType.WString or DbType.Byte }, - {typeof(byte), a => a.Type==DbType.Byte && a.Length == 1}, - {typeof(short), a => a.Type==DbType.Int}, - {typeof(ushort), a => a.Type==DbType.UInt}, - {typeof(int), a => a.Type==DbType.DInt}, - {typeof(uint), a => a.Type==DbType.UDInt}, - {typeof(long), a => a.Type==DbType.LInt}, - {typeof(ulong), a => a.Type==DbType.ULInt}, - {typeof(float), a => a.Type==DbType.Single}, - {typeof(double), a => a.Type==DbType.Double}, - {typeof(byte[]), a => a.Type==DbType.Byte}, + {typeof(string), a => a.Type is DbType.String or DbType.WString or DbType.Byte}, + {typeof(byte), a => a.Type == DbType.Byte && a.Length == 1}, + {typeof(short), a => a.Type == DbType.Int}, + {typeof(ushort), a => a.Type == DbType.UInt}, + {typeof(int), a => a.Type == DbType.DInt}, + {typeof(uint), a => a.Type == DbType.UDInt}, + {typeof(long), a => a.Type == DbType.LInt}, + {typeof(ulong), a => a.Type == DbType.ULInt}, + {typeof(float), a => a.Type == DbType.Single}, + {typeof(double), a => a.Type == DbType.Double}, + {typeof(byte[]), a => a.Type == DbType.Byte}, }; public static bool MatchesType(this VariableAddress address, Type type) => diff --git a/Sharp7.Rx/Sharp7Connector.cs b/Sharp7.Rx/Sharp7Connector.cs index 9e4ceca..f33c55b 100644 --- a/Sharp7.Rx/Sharp7Connector.cs +++ b/Sharp7.Rx/Sharp7Connector.cs @@ -98,7 +98,7 @@ internal class Sharp7Connector : IS7Connector .Select(x => { var buffer = new byte[x.Address.Length]; - s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, x.Address.DbNr, x.Address.Start, x.Address.Length, ref buffer); + s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, x.Address.DbNo, x.Address.Start, x.Address.Length, ref buffer); return new {x.VariableName, Buffer = buffer}; }) .ToArray(); diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index 8eff549..a50d24d 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -118,7 +118,7 @@ public class Sharp7Plc : IPlc { var address = ParseAndVerify(variableName, typeof(TValue)); - var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.BufferLength, address.DbNr, token); + var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.BufferLength, address.DbNo, token); return ValueConverter.ReadFromBuffer(data, address); } @@ -155,7 +155,7 @@ public class Sharp7Plc : IPlc // Special handling for bools, which are written on a by-bit basis. Writing a complete byte would // overwrite other bits within this byte. - await s7Connector.WriteBit(address.Operand, address.Start, address.Bit!.Value, (bool) (object) value, address.DbNr, token); + await s7Connector.WriteBit(address.Operand, address.Start, address.Bit!.Value, (bool) (object) value, address.DbNo, token); } else { @@ -163,7 +163,7 @@ public class Sharp7Plc : IPlc var bytes = new byte[address.BufferLength]; ValueConverter.WriteToBuffer(bytes, value, address); - await s7Connector.WriteBytes(address.Operand, address.Start, bytes, address.DbNr, token); + await s7Connector.WriteBytes(address.Operand, address.Start, bytes, address.DbNo, token); } } diff --git a/Sharp7.Rx/VariableAddress.cs b/Sharp7.Rx/VariableAddress.cs index 7e116e7..8405916 100644 --- a/Sharp7.Rx/VariableAddress.cs +++ b/Sharp7.Rx/VariableAddress.cs @@ -4,14 +4,14 @@ using Sharp7.Rx.Enums; namespace Sharp7.Rx; [NoReorder] -internal class VariableAddress +internal record VariableAddress(Operand Operand, ushort DbNo, DbType Type, ushort Start, ushort Length, byte? Bit = null) { - public Operand Operand { get; set; } - public ushort DbNr { get; set; } - public ushort Start { get; set; } - public ushort Length { get; set; } - public byte? Bit { get; set; } - public DbType Type { get; set; } + public Operand Operand { get; } = Operand; + public ushort DbNo { get; } = DbNo; + public ushort Start { get; } = Start; + public ushort Length { get; } = Length; + public byte? Bit { get; } = Bit; + public DbType Type { get; } = Type; public ushort BufferLength => Type switch { @@ -23,10 +23,10 @@ internal class VariableAddress public override string ToString() => Type switch { - DbType.Bit => $"{Operand}{DbNr}.{Type}{Start}.{Bit}", - DbType.String => $"{Operand}{DbNr}.{Type}{Start}.{Length}", - DbType.WString => $"{Operand}{DbNr}.{Type}{Start}.{Length}", - DbType.Byte => Length == 1 ? $"{Operand}{DbNr}.{Type}{Start}" : $"{Operand}{DbNr}.{Type}{Start}.{Length}", - _ => $"{Operand}{DbNr}.{Type}{Start}", + DbType.Bit => $"{Operand}{DbNo}.{Type}{Start}.{Bit}", + DbType.String => $"{Operand}{DbNo}.{Type}{Start}.{Length}", + DbType.WString => $"{Operand}{DbNo}.{Type}{Start}.{Length}", + DbType.Byte => Length == 1 ? $"{Operand}{DbNo}.{Type}{Start}" : $"{Operand}{DbNo}.{Type}{Start}.{Length}", + _ => $"{Operand}{DbNo}.{Type}{Start}", }; } diff --git a/Sharp7.Rx/VariableNameParser.cs b/Sharp7.Rx/VariableNameParser.cs index 8c2bef7..734c33e 100644 --- a/Sharp7.Rx/VariableNameParser.cs +++ b/Sharp7.Rx/VariableNameParser.cs @@ -111,15 +111,7 @@ internal class VariableNameParser : IVariableNameParser byte? bit = type == DbType.Bit ? GetBit() : null; - var s7VariableAddress = new VariableAddress - { - Operand = operand, - DbNr = dbNr, - Start = start, - Type = type, - Length = length, - Bit = bit - }; + var s7VariableAddress = new VariableAddress(Operand: operand, DbNo: dbNr, Type: type, Start: start, Length: length, Bit: bit); return s7VariableAddress;