public S7VariableAddress Parse(string input)
        {
            var match = regex.Match(input);

            if (match.Success)
            {
                var operand = (Operand)Enum.Parse(typeof(Operand), match.Groups["operand"].Value, true);
                var dbNr    = ushort.Parse(match.Groups["dbNr"].Value, NumberStyles.Integer);
                var start   = ushort.Parse(match.Groups["start"].Value, NumberStyles.Integer);
                var type    = ParseType(match.Groups["type"].Value);

                var s7VariableAddress = new S7VariableAddress
                {
                    Operand = operand,
                    DbNr    = dbNr,
                    Start   = start,
                    Type    = type,
                };

                if (type == DbType.Bit)
                {
                    s7VariableAddress.Length = 1;
                    s7VariableAddress.Bit    = byte.Parse(match.Groups["bitOrLength"].Value);
                }
                else if (type == DbType.Byte)
                {
                    s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)1;
                }
                else if (type == DbType.String)
                {
                    s7VariableAddress.Length = match.Groups["bitOrLength"].Success ? ushort.Parse(match.Groups["bitOrLength"].Value) : (ushort)0;
                }
                else if (type == DbType.Integer)
                {
                    s7VariableAddress.Length = 2;
                }
                else if (type == DbType.DInteger)
                {
                    s7VariableAddress.Length = 4;
                }
                else if (type == DbType.ULong)
                {
                    s7VariableAddress.Length = 8;
                }

                return(s7VariableAddress);
            }

            return(null);
        }
Beispiel #2
0
        private TValue ConvertToType <TValue>(byte[] buffer, S7VariableAddress address)
        {
            if (typeof(TValue) == typeof(bool))
            {
                return((TValue)(object)Convert.ToBoolean(buffer[0] & (1 << address.Bit)));
            }

            if (typeof(TValue) == typeof(int))
            {
                if (address.Length == 2)
                {
                    return((TValue)(object)((buffer[0] << 8) + buffer[1]));
                }
                if (address.Length == 4)
                {
                    Array.Reverse(buffer);
                    return((TValue)(object)BitConverter.ToInt32(buffer, 0));
                }

                throw new InvalidOperationException($"length must be 2 or 4 but is {address.Length}");
            }

            if (typeof(TValue) == typeof(long))
            {
                Array.Reverse(buffer);
                return((TValue)(object)BitConverter.ToInt64(buffer, 0));
            }

            if (typeof(TValue) == typeof(ulong))
            {
                Array.Reverse(buffer);
                return((TValue)(object)BitConverter.ToUInt64(buffer, 0));
            }

            if (typeof(TValue) == typeof(short))
            {
                return((TValue)(object)(short)((buffer[0] << 8) + buffer[1]));
            }

            if (typeof(TValue) == typeof(byte) || typeof(TValue) == typeof(char))
            {
                return((TValue)(object)buffer[0]);
            }

            if (typeof(TValue) == typeof(byte[]))
            {
                return((TValue)(object)buffer);
            }

            if (typeof(TValue) == typeof(double) || typeof(TValue) == typeof(float))
            {
                var d = BitConverter.ToSingle(buffer.Reverse().ToArray(), 0);
                return((TValue)(object)d);
            }

            if (typeof(TValue) == typeof(string))
            {
                if (address.Type == DbType.String)
                {
                    return((TValue)(object)Encoding.ASCII.GetString(buffer));
                }
                else
                {
                    return((TValue)(object)Encoding.ASCII.GetString(buffer).Trim());
                }
            }

            throw new InvalidOperationException(string.Format("type '{0}' not supported.", typeof(TValue)));
        }