Convert VariableAddress to record

This commit is contained in:
Peter Butzhammer
2024-02-09 12:46:45 +01:00
parent 996706df2f
commit 56b300b3ab
10 changed files with 62 additions and 72 deletions

View File

@@ -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")
))
;
}

View File

@@ -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<TestCase> 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)

View File

@@ -1,3 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Sharp7.Rx.Tests")]
[assembly: InternalsVisibleTo("Sharp7.Rx.Tests")]

View File

@@ -13,5 +13,4 @@ internal static class OperandExtensions
Operand.Db => S7Area.DB,
_ => throw new ArgumentOutOfRangeException(nameof(operand), operand, null)
};
}

View File

@@ -9,7 +9,8 @@ namespace Sharp7.Rx.Extensions;
public static class PlcExtensions
{
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData, bool initialTransfer)
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData,
bool initialTransfer)
{
return Observable.Create<TReturn>(async observer =>
{

View File

@@ -7,17 +7,17 @@ internal static class VariableAddressExtensions
private static readonly Dictionary<Type, Func<VariableAddress, bool>> 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) =>

View File

@@ -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();

View File

@@ -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<TValue>(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);
}
}

View File

@@ -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}",
};
}

View File

@@ -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;