From aaab24a4c2a2f4405649b64fe5e922fb6f1105bc Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Fri, 25 May 2018 22:40:56 +0200 Subject: [PATCH] Add and move serialization helpers Added (Get|Set)WordAt, SetAddressAt and moved GetPackage method. --- S7.Net/PLCHelpers.cs | 36 ------------------ S7.Net/PlcAsynchronous.cs | 2 +- S7.Net/PlcSynchronous.cs | 2 +- S7.Net/Protocol/Serialization.cs | 65 ++++++++++++++++++++++++++++++++ S7.Net/S7.Net.csproj | 1 + 5 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 S7.Net/Protocol/Serialization.cs diff --git a/S7.Net/PLCHelpers.cs b/S7.Net/PLCHelpers.cs index ab59d2c..cb004e2 100644 --- a/S7.Net/PLCHelpers.cs +++ b/S7.Net/PLCHelpers.cs @@ -316,42 +316,6 @@ namespace S7.Net } } - public byte[] GetPackage(object value) - { - switch (value.GetType().Name) - { - case "Byte": - return Types.Byte.ToByteArray((byte)value); - case "Int16": - return Types.Int.ToByteArray((Int16)value); - case "UInt16": - return Types.Word.ToByteArray((UInt16)value); - case "Int32": - return Types.DInt.ToByteArray((Int32)value); - case "UInt32": - return Types.DWord.ToByteArray((UInt32)value); - case "Double": - return Types.Double.ToByteArray((double)value); - case "Byte[]": - return (byte[])value; - case "Int16[]": - return Types.Int.ToByteArray((Int16[])value); - case "UInt16[]": - return Types.Word.ToByteArray((UInt16[])value); - case "Int32[]": - return Types.DInt.ToByteArray((Int32[])value); - case "UInt32[]": - return Types.DWord.ToByteArray((UInt32[])value); - case "Double[]": - return Types.Double.ToByteArray((double[])value); - case "String": - return Types.String.ToByteArray(value as string); - default: - throw new InvalidVariableTypeException(); - } - } - - /// /// Sets the to and to . /// diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs index ec5d18a..a09f79f 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -344,7 +344,7 @@ namespace S7.Net } throw new ArgumentException("Value must be a bool or an int to write a bit", nameof(value)); } - return await WriteBytesAsync(dataType, db, startByteAdr, GetPackage(value)); + return await WriteBytesAsync(dataType, db, startByteAdr, Serialization.SerializeValue(value)); } /// diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index 892d58b..c24a20d 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -324,7 +324,7 @@ namespace S7.Net } throw new ArgumentException("Value must be a bool or an int to write a bit", nameof(value)); } - return WriteBytes(dataType, db, startByteAdr, GetPackage(value)); + return WriteBytes(dataType, db, startByteAdr, Serialization.SerializeValue(value)); } /// diff --git a/S7.Net/Protocol/Serialization.cs b/S7.Net/Protocol/Serialization.cs new file mode 100644 index 0000000..d5bfe91 --- /dev/null +++ b/S7.Net/Protocol/Serialization.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using S7.Net.Types; + +namespace S7.Net.Protocol +{ + internal static class Serialization + { + public static ushort GetWordAt(IList buf, int index) + { + return (ushort) ((buf[index] << 8) + buf[index]); + } + + public static byte[] SerializeValue(object value) + { + switch (value.GetType().Name) + { + case "Byte": + return Types.Byte.ToByteArray((byte) value); + case "Int16": + return Types.Int.ToByteArray((Int16) value); + case "UInt16": + return Types.Word.ToByteArray((UInt16) value); + case "Int32": + return Types.DInt.ToByteArray((Int32) value); + case "UInt32": + return Types.DWord.ToByteArray((UInt32) value); + case "Double": + return Types.Double.ToByteArray((double) value); + case "Byte[]": + return (byte[]) value; + case "Int16[]": + return Types.Int.ToByteArray((Int16[]) value); + case "UInt16[]": + return Types.Word.ToByteArray((UInt16[]) value); + case "Int32[]": + return Types.DInt.ToByteArray((Int32[]) value); + case "UInt32[]": + return Types.DWord.ToByteArray((UInt32[]) value); + case "Double[]": + return Types.Double.ToByteArray((double[]) value); + case "String": + return Types.String.ToByteArray(value as string); + default: + throw new InvalidVariableTypeException(); + } + } + + public static void SetAddressAt(ByteArray buffer, int index, int startByte, byte bitNumber) + { + var start = startByte * 8 + bitNumber; + buffer[index + 2] = (byte) start; + start = start >> 8; + buffer[index + 1] = (byte) start; + start = start >> 8; + buffer[index] = (byte) start; + } + + public static void SetWordAt(ByteArray buffer, int index, ushort value) + { + buffer[index] = (byte) (value >> 8); + buffer[index + 1] = (byte) value; + } + } +} diff --git a/S7.Net/S7.Net.csproj b/S7.Net/S7.Net.csproj index 7b9f625..786e755 100644 --- a/S7.Net/S7.Net.csproj +++ b/S7.Net/S7.Net.csproj @@ -85,6 +85,7 @@ +