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

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