Restructure and extens tests for converter

This commit is contained in:
Peter Butzhammer
2024-02-08 10:12:13 +01:00
parent 6492d039da
commit d1ec075aa7
8 changed files with 164 additions and 131 deletions

View File

@@ -7,7 +7,7 @@ namespace Sharp7.Rx;
internal static class S7ValueConverter
{
public static TValue ConvertToType<TValue>(byte[] buffer, S7VariableAddress address)
public static TValue ReadFromBuffer<TValue>(byte[] buffer, S7VariableAddress address)
{
if (typeof(TValue) == typeof(bool))
return (TValue) (object) (((buffer[0] >> address.Bit) & 1) > 0);

View File

@@ -28,12 +28,15 @@ internal class S7VariableNameParser : IS7VariableNameParser
{"real", DbType.Single},
{"lreal", DbType.Double},
// used for legacy compatability
{"b", DbType.Byte},
{"d", DbType.Single},
// S7 notation
{"dbb", DbType.Byte},
{"dbw", DbType.Int},
{"dbx", DbType.Bit},
{"dbd", DbType.DInt},
// used for legacy compatability
{"b", DbType.Byte},
{"d", DbType.Single},
{"dul", DbType.ULInt},
{"dulint", DbType.ULInt},
{"dulong", DbType.ULInt},

View File

@@ -90,7 +90,7 @@ public class Sharp7Plc : IPlc
Observable.FromAsync(() => GetValue<TValue>(variableName))
.Concat(
disposeableContainer.Observable
.Select(bytes => S7ValueConverter.ConvertToType<TValue>(bytes, address))
.Select(bytes => S7ValueConverter.ReadFromBuffer<TValue>(bytes, address))
);
if (transmissionMode == TransmissionMode.OnChange)
@@ -121,7 +121,7 @@ public class Sharp7Plc : IPlc
if (address == null) throw new ArgumentException("Input variable name is not valid", nameof(variableName));
var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token);
return S7ValueConverter.ConvertToType<TValue>(data, address);
return S7ValueConverter.ReadFromBuffer<TValue>(data, address);
}
public async Task<bool> InitializeAsync()