Remove "S7" from some class names

This commit is contained in:
Peter Butzhammer
2024-02-09 12:33:24 +01:00
parent dd0af70262
commit 4701a224a7
17 changed files with 142 additions and 142 deletions

View File

@@ -1,91 +0,0 @@
using DeepEqual.Syntax;
using NUnit.Framework;
using Sharp7.Rx.Enums;
using Shouldly;
namespace Sharp7.Rx.Tests;
[TestFixture]
internal class S7VariableNameParserTests
{
[TestCaseSource(nameof(ValidTestCases))]
public void Run(TestCase tc)
{
var parser = new S7VariableNameParser();
var resp = parser.Parse(tc.Input);
resp.ShouldDeepEqual(tc.Expected);
}
[TestCase("DB506.Bit216", TestName = "Bit without Bit")]
[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")]
[TestCase("DB506.UDInt216.1", TestName = "UDInt with Length")]
[TestCase("DB506.LInt216.1", TestName = "LInt with Length")]
[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")]
[TestCase("x506.Int216", TestName = "Wrong type")]
[TestCase("506.Int216", TestName = "No type")]
[TestCase("", TestName = "empty")]
[TestCase(" ", TestName = "space")]
[TestCase(" DB506.Int216", TestName = "leading space")]
[TestCase("DB506.Int216 ", TestName = "trailing space")]
[TestCase("DB.Int216 ", TestName = "No db")]
[TestCase("DB5061234.Int216.1", TestName = "DB too large")]
public void Invalid(string? input)
{
var parser = new S7VariableNameParser();
Should.Throw<InvalidS7AddressException>(() => parser.Parse(input));
}
public static IEnumerable<TestCase> ValidTestCases()
{
yield return new TestCase("DB506.Bit216.2", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Bit = 2, Type = DbType.Bit});
yield return new TestCase("DB506.String216.10", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 10, Type = DbType.String});
yield return new TestCase("DB506.WString216.10", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 10, Type = DbType.WString});
yield return new TestCase("DB506.Byte216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Type = DbType.Byte});
yield return new TestCase("DB506.Byte216.100", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 100, Type = DbType.Byte});
yield return new TestCase("DB506.Int216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int});
yield return new TestCase("DB506.UInt216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.UInt});
yield return new TestCase("DB506.DInt216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.DInt});
yield return new TestCase("DB506.UDInt216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.UDInt});
yield return new TestCase("DB506.LInt216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.LInt});
yield return new TestCase("DB506.ULInt216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt});
yield return new TestCase("DB506.Real216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.Single});
yield return new TestCase("DB506.LReal216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.Double});
// Legacy
yield return new TestCase("DB13.DBX3.1", new S7VariableAddress {Operand = Operand.Db, DbNr = 13, Start = 3, Length = 1, Bit = 1, Type = DbType.Bit});
yield return new TestCase("Db403.X5.2", new S7VariableAddress {Operand = Operand.Db, DbNr = 403, Start = 5, Length = 1, Bit = 2, Type = DbType.Bit});
yield return new TestCase("DB55DBX23.6", new S7VariableAddress {Operand = Operand.Db, DbNr = 55, Start = 23, Length = 1, Bit = 6, Type = DbType.Bit});
yield return new TestCase("DB1.S255.20", new S7VariableAddress {Operand = Operand.Db, DbNr = 1, Start = 255, Length = 20, Type = DbType.String});
yield return new TestCase("DB5.String887.20", new S7VariableAddress {Operand = Operand.Db, DbNr = 5, Start = 887, Length = 20, Type = DbType.String});
yield return new TestCase("DB506.B216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Type = DbType.Byte});
yield return new TestCase("DB506.DBB216.5", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 5, Type = DbType.Byte});
yield return new TestCase("DB506.D216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.Single});
yield return new TestCase("DB506.DINT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Type = DbType.DInt});
yield return new TestCase("DB506.INT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int});
yield return new TestCase("DB506.DBW216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Type = DbType.Int});
yield return new TestCase("DB506.DUL216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt});
yield return new TestCase("DB506.DULINT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt});
yield return new TestCase("DB506.DULONG216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Type = DbType.ULInt});
}
public record TestCase(string Input, S7VariableAddress Expected)
{
public override string ToString() => Input;
}
}

View File

@@ -1,7 +1,7 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.S7ValueConverterTests;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class ConvertBothWays : ConverterTestBase

View File

@@ -1,15 +1,15 @@
using System.Reflection;
using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx.Tests.S7ValueConverterTests;
namespace Sharp7.Rx.Tests.ValueConverterTests;
internal abstract class ConverterTestBase
{
protected static readonly IS7VariableNameParser Parser = new S7VariableNameParser();
protected static readonly IVariableNameParser Parser = new VariableNameParser();
public static MethodInfo CreateReadMethod(ConverterTestCase tc)
{
var convertMi = typeof(S7ValueConverter).GetMethod(nameof(S7ValueConverter.ReadFromBuffer));
var convertMi = typeof(ValueConverter).GetMethod(nameof(ValueConverter.ReadFromBuffer));
var convert = convertMi!.MakeGenericMethod(tc.Value.GetType());
return convert;
}
@@ -69,14 +69,14 @@ internal abstract class ConverterTestBase
/// This helper method exists, since I could not manage to invoke a generic method
/// accepring a Span&lt;T&gt; as parameter.
/// </summary>
public static void WriteToBuffer<TValue>(byte[] buffer, TValue value, S7VariableAddress address)
public static void WriteToBuffer<TValue>(byte[] buffer, TValue value, VariableAddress address)
{
S7ValueConverter.WriteToBuffer(buffer, value, address);
ValueConverter.WriteToBuffer(buffer, value, address);
}
public record ConverterTestCase(object Value, string Address, byte[] Data)
{
public S7VariableAddress VariableAddress => Parser.Parse(Address);
public VariableAddress VariableAddress => Parser.Parse(Address);
public override string ToString() => $"{Value.GetType().Name}, {Address}: {Value}";
}

View File

@@ -1,7 +1,7 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.S7ValueConverterTests;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class ReadFromBuffer : ConverterTestBase
@@ -34,7 +34,7 @@ internal class ReadFromBuffer : ConverterTestBase
var variableAddress = Parser.Parse(address);
//Act
Should.Throw<UnsupportedS7TypeException>(() => S7ValueConverter.ReadFromBuffer<T>(data, variableAddress));
Should.Throw<UnsupportedS7TypeException>(() => ValueConverter.ReadFromBuffer<T>(data, variableAddress));
}
[TestCase(123, "DB12.DINT3", new byte[] {0x01, 0x02, 0x03})]
@@ -46,6 +46,6 @@ internal class ReadFromBuffer : ConverterTestBase
var variableAddress = Parser.Parse(address);
//Act
Should.Throw<ArgumentException>(() => S7ValueConverter.ReadFromBuffer<T>(data, variableAddress));
Should.Throw<ArgumentException>(() => ValueConverter.ReadFromBuffer<T>(data, variableAddress));
}
}

View File

@@ -1,7 +1,7 @@
using NUnit.Framework;
using Shouldly;
namespace Sharp7.Rx.Tests.S7ValueConverterTests;
namespace Sharp7.Rx.Tests.ValueConverterTests;
[TestFixture]
internal class WriteToBuffer : ConverterTestBase
@@ -37,7 +37,7 @@ internal class WriteToBuffer : ConverterTestBase
var buffer = new byte[bufferSize];
//Act
Should.Throw<ArgumentException>(() => S7ValueConverter.WriteToBuffer(buffer, input, variableAddress));
Should.Throw<ArgumentException>(() => ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
[TestCase((char) 18, "DB0.DBB0")]
@@ -48,6 +48,6 @@ internal class WriteToBuffer : ConverterTestBase
var buffer = new byte[variableAddress.BufferLength];
//Act
Should.Throw<UnsupportedS7TypeException>(() => S7ValueConverter.WriteToBuffer(buffer, input, variableAddress));
Should.Throw<UnsupportedS7TypeException>(() => ValueConverter.WriteToBuffer(buffer, input, variableAddress));
}
}

View File

@@ -1,15 +1,15 @@
using NUnit.Framework;
using Sharp7.Rx.Extensions;
using Sharp7.Rx.Interfaces;
using Sharp7.Rx.Tests.S7ValueConverterTests;
using Sharp7.Rx.Tests.ValueConverterTests;
using Shouldly;
namespace Sharp7.Rx.Tests.S7VariableAddressTests;
namespace Sharp7.Rx.Tests.VariableAddressTests;
[TestFixture]
public class MatchesType
{
static readonly IS7VariableNameParser parser = new S7VariableNameParser();
static readonly IVariableNameParser parser = new VariableNameParser();
private static readonly IReadOnlyList<Type> typeList = new[]
{

View File

@@ -0,0 +1,91 @@
using DeepEqual.Syntax;
using NUnit.Framework;
using Sharp7.Rx.Enums;
using Shouldly;
namespace Sharp7.Rx.Tests;
[TestFixture]
internal class VariableNameParserTests
{
[TestCaseSource(nameof(ValidTestCases))]
public void Run(TestCase tc)
{
var parser = new VariableNameParser();
var resp = parser.Parse(tc.Input);
resp.ShouldDeepEqual(tc.Expected);
}
[TestCase("DB506.Bit216", TestName = "Bit without Bit")]
[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")]
[TestCase("DB506.UDInt216.1", TestName = "UDInt with Length")]
[TestCase("DB506.LInt216.1", TestName = "LInt with Length")]
[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")]
[TestCase("x506.Int216", TestName = "Wrong type")]
[TestCase("506.Int216", TestName = "No type")]
[TestCase("", TestName = "empty")]
[TestCase(" ", TestName = "space")]
[TestCase(" DB506.Int216", TestName = "leading space")]
[TestCase("DB506.Int216 ", TestName = "trailing space")]
[TestCase("DB.Int216 ", TestName = "No db")]
[TestCase("DB5061234.Int216.1", TestName = "DB too large")]
public void Invalid(string? input)
{
var parser = new VariableNameParser();
Should.Throw<InvalidS7AddressException>(() => parser.Parse(input));
}
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.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.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.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});
// 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});
}
public record TestCase(string Input, VariableAddress Expected)
{
public override string ToString() => Input;
}
}

View File

@@ -3,16 +3,16 @@ using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx;
internal class CacheVariableNameParser : IS7VariableNameParser
internal class CacheVariableNameParser : IVariableNameParser
{
private static readonly ConcurrentDictionary<string, S7VariableAddress> addressCache = new ConcurrentDictionary<string, S7VariableAddress>(StringComparer.OrdinalIgnoreCase);
private static readonly ConcurrentDictionary<string, VariableAddress> addressCache = new ConcurrentDictionary<string, VariableAddress>(StringComparer.OrdinalIgnoreCase);
private readonly IS7VariableNameParser inner;
private readonly IVariableNameParser inner;
public CacheVariableNameParser(IS7VariableNameParser inner)
public CacheVariableNameParser(IVariableNameParser inner)
{
this.inner = inner;
}
public S7VariableAddress Parse(string input) => addressCache.GetOrAdd(input, inner.Parse);
public VariableAddress Parse(string input) => addressCache.GetOrAdd(input, inner.Parse);
}

View File

@@ -31,13 +31,13 @@ public class S7CommunicationException : S7Exception
public class DataTypeMissmatchException : S7Exception
{
internal DataTypeMissmatchException(string message, Type type, S7VariableAddress address) : base(message)
internal DataTypeMissmatchException(string message, Type type, VariableAddress address) : base(message)
{
Type = type;
Address = address.ToString();
}
internal DataTypeMissmatchException(string message, Exception innerException, Type type, S7VariableAddress address) : base(message, innerException)
internal DataTypeMissmatchException(string message, Exception innerException, Type type, VariableAddress address) : base(message, innerException)
{
Type = type;
Address = address.ToString();
@@ -50,13 +50,13 @@ public class DataTypeMissmatchException : S7Exception
public class UnsupportedS7TypeException : S7Exception
{
internal UnsupportedS7TypeException(string message, Type type, S7VariableAddress address) : base(message)
internal UnsupportedS7TypeException(string message, Type type, VariableAddress address) : base(message)
{
Type = type;
Address = address.ToString();
}
internal UnsupportedS7TypeException(string message, Exception innerException, Type type, S7VariableAddress address) : base(message, innerException)
internal UnsupportedS7TypeException(string message, Exception innerException, Type type, VariableAddress address) : base(message, innerException)
{
Type = type;
Address = address.ToString();

View File

@@ -2,9 +2,9 @@
namespace Sharp7.Rx.Extensions;
internal static class S7VariableAddressExtensions
internal static class VariableAddressExtensions
{
private static readonly Dictionary<Type, Func<S7VariableAddress, bool>> supportedTypeMap = new()
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 },
@@ -20,6 +20,6 @@ internal static class S7VariableAddressExtensions
{typeof(byte[]), a => a.Type==DbType.Byte},
};
public static bool MatchesType(this S7VariableAddress address, Type type) =>
public static bool MatchesType(this VariableAddress address, Type type) =>
supportedTypeMap.TryGetValue(type, out var map) && map(address);
}

View File

@@ -1,7 +0,0 @@
#nullable enable
namespace Sharp7.Rx.Interfaces;
internal interface IS7VariableNameParser
{
S7VariableAddress Parse(string input);
}

View File

@@ -0,0 +1,7 @@
#nullable enable
namespace Sharp7.Rx.Interfaces;
internal interface IVariableNameParser
{
VariableAddress Parse(string input);
}

View File

@@ -32,13 +32,13 @@ internal class Sharp7Connector : IS7Connector
private readonly int port;
private readonly int rackNr;
private readonly LimitedConcurrencyLevelTaskScheduler scheduler = new(maxDegreeOfParallelism: 1);
private readonly IS7VariableNameParser variableNameParser;
private readonly IVariableNameParser variableNameParser;
private bool disposed;
private S7Client sharp7;
public Sharp7Connector(PlcConnectionSettings settings, IS7VariableNameParser variableNameParser)
public Sharp7Connector(PlcConnectionSettings settings, IVariableNameParser variableNameParser)
{
this.variableNameParser = variableNameParser;
ipAddress = settings.IpAddress;

View File

@@ -17,7 +17,7 @@ public class Sharp7Plc : IPlc
private readonly ConcurrentSubjectDictionary<string, byte[]> multiVariableSubscriptions = new(StringComparer.InvariantCultureIgnoreCase);
private readonly List<long> performanceCoutner = new(1000);
private readonly PlcConnectionSettings plcConnectionSettings;
private readonly IS7VariableNameParser varaibleNameParser = new CacheVariableNameParser(new S7VariableNameParser());
private readonly IVariableNameParser varaibleNameParser = new CacheVariableNameParser(new VariableNameParser());
private bool disposed;
private Sharp7Connector s7Connector;
@@ -89,7 +89,7 @@ public class Sharp7Plc : IPlc
Observable.FromAsync(() => GetValue<TValue>(variableName))
.Concat(
disposeableContainer.Observable
.Select(bytes => S7ValueConverter.ReadFromBuffer<TValue>(bytes, address))
.Select(bytes => ValueConverter.ReadFromBuffer<TValue>(bytes, address))
);
if (transmissionMode == TransmissionMode.OnChange)
@@ -119,7 +119,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);
return S7ValueConverter.ReadFromBuffer<TValue>(data, address);
return ValueConverter.ReadFromBuffer<TValue>(data, address);
}
public async Task<bool> InitializeAsync()
@@ -161,7 +161,7 @@ public class Sharp7Plc : IPlc
{
// TODO: Use ArrayPool.Rent() once we drop Framwework support
var bytes = new byte[address.BufferLength];
S7ValueConverter.WriteToBuffer(bytes, value, address);
ValueConverter.WriteToBuffer(bytes, value, address);
await s7Connector.WriteBytes(address.Operand, address.Start, bytes, address.DbNr, token);
}
@@ -213,7 +213,7 @@ public class Sharp7Plc : IPlc
return Unit.Default;
}
private S7VariableAddress ParseAndVerify(string variableName, Type type)
private VariableAddress ParseAndVerify(string variableName, Type type)
{
var address = varaibleNameParser.Parse(variableName);
if (!address.MatchesType(type))

View File

@@ -5,7 +5,7 @@ using Sharp7.Rx.Enums;
namespace Sharp7.Rx;
internal static class S7ValueConverter
internal static class ValueConverter
{
private static readonly Dictionary<Type, WriteFunc> writeFunctions = new()
{
@@ -175,7 +175,7 @@ internal static class S7ValueConverter
},
};
public static TValue ReadFromBuffer<TValue>(byte[] buffer, S7VariableAddress address)
public static TValue ReadFromBuffer<TValue>(byte[] buffer, VariableAddress address)
{
// Todo: Change to Span<byte> when switched to newer .net
@@ -191,7 +191,7 @@ internal static class S7ValueConverter
return (TValue) result;
}
public static void WriteToBuffer<TValue>(Span<byte> buffer, TValue value, S7VariableAddress address)
public static void WriteToBuffer<TValue>(Span<byte> buffer, TValue value, VariableAddress address)
{
if (buffer.Length < address.BufferLength)
throw new ArgumentException($"Buffer must be at least {address.BufferLength} bytes long for {address}", nameof(buffer));
@@ -204,7 +204,7 @@ internal static class S7ValueConverter
writeFunc(buffer, address, value);
}
delegate object ReadFunc(byte[] data, S7VariableAddress address);
delegate object ReadFunc(byte[] data, VariableAddress address);
[StructLayout(LayoutKind.Explicit)]
private struct UInt32SingleMap
@@ -220,5 +220,5 @@ internal static class S7ValueConverter
[FieldOffset(0)] public double Double;
}
delegate void WriteFunc(Span<byte> data, S7VariableAddress address, object value);
delegate void WriteFunc(Span<byte> data, VariableAddress address, object value);
}

View File

@@ -4,7 +4,7 @@ using Sharp7.Rx.Enums;
namespace Sharp7.Rx;
[NoReorder]
internal class S7VariableAddress
internal class VariableAddress
{
public Operand Operand { get; set; }
public ushort DbNr { get; set; }

View File

@@ -6,7 +6,7 @@ using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx;
internal class S7VariableNameParser : IS7VariableNameParser
internal class VariableNameParser : IVariableNameParser
{
private static readonly Regex regex = new(@"^(?<operand>db)(?<dbNo>\d+)\.?(?<type>[a-z]+)(?<start>\d+)(\.(?<bitOrLength>\d+))?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
@@ -46,7 +46,7 @@ internal class S7VariableNameParser : IS7VariableNameParser
{"x", DbType.Bit},
};
public S7VariableAddress Parse(string input)
public VariableAddress Parse(string input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
@@ -111,7 +111,7 @@ internal class S7VariableNameParser : IS7VariableNameParser
byte? bit = type == DbType.Bit ? GetBit() : null;
var s7VariableAddress = new S7VariableAddress
var s7VariableAddress = new VariableAddress
{
Operand = operand,
DbNr = dbNr,