mirror of
https://github.com/evopro-ag/Sharp7Reactive.git
synced 2025-12-17 04:02:52 +00:00
Add WriteToBuffer and unify supported types
This commit is contained in:
@@ -7,10 +7,6 @@ namespace Sharp7.Rx;
|
||||
|
||||
internal static class S7ValueConverter
|
||||
{
|
||||
public static void WriteToBuffer<TValue>(Span<byte> buffer, TValue value, S7VariableAddress address)
|
||||
{
|
||||
}
|
||||
|
||||
public static TValue ConvertToType<TValue>(byte[] buffer, S7VariableAddress address)
|
||||
{
|
||||
if (typeof(TValue) == typeof(bool))
|
||||
@@ -37,21 +33,10 @@ internal static class S7ValueConverter
|
||||
|
||||
if (typeof(TValue) == typeof(byte))
|
||||
return (TValue) (object) buffer[0];
|
||||
if (typeof(TValue) == typeof(char))
|
||||
return (TValue) (object) (char) buffer[0];
|
||||
|
||||
if (typeof(TValue) == typeof(byte[]))
|
||||
return (TValue) (object) buffer;
|
||||
|
||||
if (typeof(TValue) == typeof(double))
|
||||
{
|
||||
var d = new UInt32SingleMap
|
||||
{
|
||||
UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer)
|
||||
};
|
||||
return (TValue) (object) (double) d.Single;
|
||||
}
|
||||
|
||||
|
||||
if (typeof(TValue) == typeof(float))
|
||||
{
|
||||
var d = new UInt32SingleMap
|
||||
@@ -75,7 +60,83 @@ internal static class S7ValueConverter
|
||||
else
|
||||
return (TValue) (object) Encoding.ASCII.GetString(buffer).Trim();
|
||||
|
||||
throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue)));
|
||||
throw new InvalidOperationException($"type '{typeof(TValue)}' not supported.");
|
||||
}
|
||||
|
||||
public static void WriteToBuffer<TValue>(Span<byte> buffer, TValue value, S7VariableAddress address)
|
||||
{
|
||||
if (buffer.Length < address.BufferLength)
|
||||
throw new ArgumentException($"buffer must be at least {address.BufferLength} bytes long for {address}", nameof(buffer));
|
||||
|
||||
if (typeof(TValue) == typeof(bool))
|
||||
{
|
||||
var byteValue = (bool) (object) value ? (byte) 1 : (byte) 0;
|
||||
var shifted = (byte) (byteValue << address.Bit);
|
||||
buffer[0] = shifted;
|
||||
}
|
||||
|
||||
else if (typeof(TValue) == typeof(int))
|
||||
{
|
||||
if (address.Length == 2)
|
||||
BinaryPrimitives.WriteInt16BigEndian(buffer, (short) (int) (object) value);
|
||||
else
|
||||
BinaryPrimitives.WriteInt32BigEndian(buffer, (int) (object) value);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(short))
|
||||
{
|
||||
if (address.Length == 2)
|
||||
BinaryPrimitives.WriteInt16BigEndian(buffer, (short) (object) value);
|
||||
else
|
||||
BinaryPrimitives.WriteInt32BigEndian(buffer, (short) (object) value);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(long))
|
||||
BinaryPrimitives.WriteInt64BigEndian(buffer, (long) (object) value);
|
||||
else if (typeof(TValue) == typeof(ulong))
|
||||
BinaryPrimitives.WriteUInt64BigEndian(buffer, (ulong) (object) value);
|
||||
else if (typeof(TValue) == typeof(byte))
|
||||
buffer[0] = (byte) (object) value;
|
||||
else if (typeof(TValue) == typeof(byte[]))
|
||||
{
|
||||
var source = (byte[]) (object) value;
|
||||
|
||||
var length = Math.Min(Math.Min(source.Length, buffer.Length), address.Length);
|
||||
|
||||
source.AsSpan(0, length).CopyTo(buffer);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(float))
|
||||
{
|
||||
var map = new UInt32SingleMap
|
||||
{
|
||||
Single = (float) (object) value
|
||||
};
|
||||
|
||||
BinaryPrimitives.WriteUInt32BigEndian(buffer, map.UInt32);
|
||||
}
|
||||
else if (typeof(TValue) == typeof(string))
|
||||
{
|
||||
if (value is not string stringValue) throw new ArgumentException("Value must be of type string", nameof(value));
|
||||
|
||||
// Todo: Serialize directly to Span, when upgrading to .net
|
||||
var stringBytes = Encoding.ASCII.GetBytes(stringValue);
|
||||
|
||||
var length = Math.Min(address.Length, stringValue.Length);
|
||||
|
||||
int stringOffset;
|
||||
if (address.Type == DbType.String)
|
||||
{
|
||||
stringOffset = 2;
|
||||
buffer[0] = (byte) address.Length;
|
||||
buffer[1] = (byte) length;
|
||||
}
|
||||
else
|
||||
stringOffset = 0;
|
||||
|
||||
stringBytes.AsSpan(0, length).CopyTo(buffer.Slice(stringOffset));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"type '{typeof(TValue)}' not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
|
||||
Reference in New Issue
Block a user