diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index 95b690c..76705fd 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -142,96 +142,8 @@ namespace Sharp7.Rx var address = varaibleNameParser.Parse(variableName); if (address == null) throw new ArgumentException("Input variable name is not valid", nameof(variableName)); - if (typeof(TValue) == typeof(bool)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - return ConvertToType(b, address); - } - - if (typeof(TValue) == typeof(int)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - token.ThrowIfCancellationRequested(); - if (address.Length == 2) - return (TValue)(object)((b[0] << 8) + b[1]); - if (address.Length == 4) - { - Array.Reverse(b); - return (TValue)(object)BitConverter.ToInt32(b,0); - } - - - throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}"); - } - - if (typeof(TValue) == typeof(long)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - token.ThrowIfCancellationRequested(); - Array.Reverse(b); - return (TValue)(object)BitConverter.ToInt64(b,0); - } - - if (typeof(TValue) == typeof(ulong)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - token.ThrowIfCancellationRequested(); - Array.Reverse(b); - return (TValue)(object)BitConverter.ToUInt64(b, 0); - } - - if (typeof(TValue) == typeof(short)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, 2, address.DbNr, token); - token.ThrowIfCancellationRequested(); - return (TValue)(object)(short)((b[0] << 8) + b[1]); - } - - if (typeof(TValue) == typeof(byte) || typeof(TValue) == typeof(char)) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, 1, address.DbNr, token); - token.ThrowIfCancellationRequested(); - - return (TValue)(object)b[0]; - } - - if (typeof(TValue) == typeof(byte[])) - { - var b = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - token.ThrowIfCancellationRequested(); - return (TValue)(object)b; - } - - if (typeof(TValue) == typeof(double) || typeof(TValue) == typeof(float)) - { - var bytes = await s7Connector.ReadBytes(address.Operand, address.Start, 4, address.DbNr, token); - token.ThrowIfCancellationRequested(); - var d = BitConverter.ToSingle(bytes.Reverse().ToArray(),0); - return (TValue)(object)d; - } - - if (typeof(TValue) == typeof(string)) - { - if (address.Type == DbType.String) - { - var bytes = await s7Connector.ReadBytes(address.Operand, address.Start, 2, address.DbNr, token); - token.ThrowIfCancellationRequested(); - var stringLength = bytes[1]; - - var stringStartAddress = (ushort)(address.Start + 2); - var stringInBytes = await s7Connector.ReadBytes(address.Operand, stringStartAddress, stringLength, address.DbNr, token); - token.ThrowIfCancellationRequested(); - return (TValue)(object)Encoding.ASCII.GetString(stringInBytes); - } - else - { - var stringInBytes = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); - token.ThrowIfCancellationRequested(); - return (TValue)(object)Encoding.ASCII.GetString(stringInBytes).Trim(); - } - } - - throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue))); + var data = await s7Connector.ReadBytes(address.Operand, address.Start, address.Length, address.DbNr, token); + return ConvertToType(data, address); }