Esempio n. 1
0
        internal int Unmarshal(IAttributedInstance symbol, Type targetType, byte[] data, int offset, out object value)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            int byteSize = 0;

            if (targetType.IsValueType)
            {
                object obj2;
                byteSize = this._internalConverter.Unmarshal(symbol, data, offset, out obj2);
                value    = PrimitiveTypeConverter.Convert(obj2, targetType);
            }
            else
            {
                value = this.createValue(symbol.DataType, targetType);
                this.InitializeInstanceValue(symbol, ref value, data, offset);
                byteSize = symbol.ByteSize;
            }
            return(byteSize);
        }
Esempio n. 2
0
        internal int InitializePointerType(Type managedType, AdsDatatypeId dataType, int byteLength, AdsBinaryReader reader, int readerOffset, out object value)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if ((byteLength != 4) && (byteLength != 8))
            {
                throw new ArgumentOutOfRangeException("byteLength");
            }
            reader.BaseStream.Position = readerOffset;
            byte[] buffer      = reader.ReadBytes(byteLength);
            object sourceValue = null;

            if (byteLength == 4)
            {
                sourceValue = BitConverter.ToUInt32(buffer, 0);
            }
            else if (byteLength == 8)
            {
                sourceValue = BitConverter.ToUInt64(buffer, 0);
            }
            if ((managedType == null) || (managedType == sourceValue.GetType()))
            {
                value = sourceValue;
            }
            else
            {
                value = PrimitiveTypeConverter.Convert(sourceValue, managedType);
            }
            return(0);
        }
Esempio n. 3
0
        internal void WritePointerValue(string symbolPath, object value, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if ((byteSize < 0) || ((byteSize != 4) && (byteSize != 8)))
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type targetType = null;

            if (byteSize == 4)
            {
                targetType = typeof(uint);
            }
            else if (byteSize == 8)
            {
                targetType = typeof(ulong);
            }
            if (targetType != value.GetType())
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, targetType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{targetType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            if (byteSize == 4)
            {
                writer.Write((uint)value);
            }
            else if (byteSize == 8)
            {
                writer.Write((ulong)value);
            }
        }
Esempio n. 4
0
        private int readArraySize()
        {
            int          num             = 0;
            IValueSymbol arraySizeSymbol = (IValueSymbol)this.ArraySizeSymbol;

            if (arraySizeSymbol != null)
            {
                try
                {
                    num = PrimitiveTypeConverter.Convert <int>(arraySizeSymbol.ReadValue());
                }
                catch (Exception)
                {
                }
            }
            return(num);
        }
Esempio n. 5
0
        internal void WritePrimitiveValue(string symbolPath, object value, Type managedType, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (byteSize < 0)
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type type  = value.GetType();
            bool flag2 = PrimitiveTypeConverter.IsPlcOpenType(value.GetType());

            if (PrimitiveTypeConverter.IsPlcOpenType(managedType))
            {
                Type tp = null;
                if (PrimitiveTypeConverter.TryGetManagedType(dataType, out tp))
                {
                    managedType = tp;
                }
                else if (managedType == typeof(LTIME))
                {
                    managedType = typeof(ulong);
                    dataType    = AdsDatatypeId.ADST_UINT64;
                }
            }
            if (managedType != type)
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, managedType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{managedType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                writer.Write((short)value);
                return;

            case AdsDatatypeId.ADST_INT32:
                writer.Write((int)value);
                return;

            case AdsDatatypeId.ADST_REAL32:
                writer.Write((float)value);
                return;

            case AdsDatatypeId.ADST_REAL64:
                writer.Write((double)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT8:
                writer.Write((sbyte)value);
                return;

            case AdsDatatypeId.ADST_UINT8:
                writer.Write((byte)value);
                return;

            case AdsDatatypeId.ADST_UINT16:
                writer.Write((ushort)value);
                return;

            case AdsDatatypeId.ADST_UINT32:
                writer.Write((uint)value);
                return;

            case AdsDatatypeId.ADST_INT64:
                writer.Write((long)value);
                return;

            case AdsDatatypeId.ADST_UINT64:
                writer.Write((ulong)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_STRING:
            {
                int  byteCount = Encoding.Default.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcAnsiString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_WSTRING:
            {
                int  byteCount = Encoding.Unicode.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcUnicodeString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_BIT:
                if ((bool)value)
                {
                    writer.Write((byte)1);
                    return;
                }
                writer.Write((byte)0);
                return;
            }
            throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
        }
Esempio n. 6
0
        internal int InitializePrimitiveType(string typeName, Type managedType, AdsDatatypeId dataType, int byteLength, AdsBinaryReader reader, int readerOffset, out object value)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            int num = 0;

            reader.BaseStream.Position = readerOffset;
            object sourceValue = null;

            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                sourceValue = reader.ReadInt16();
                num         = 2;
                break;

            case AdsDatatypeId.ADST_INT32:
                sourceValue = reader.ReadInt32();
                num         = 4;
                break;

            case AdsDatatypeId.ADST_REAL32:
                sourceValue = reader.ReadSingle();
                num         = 4;
                break;

            case AdsDatatypeId.ADST_REAL64:
                sourceValue = reader.ReadDouble();
                num         = 8;
                break;

            default:
                switch (dataType)
                {
                case AdsDatatypeId.ADST_INT8:
                    sourceValue = reader.ReadSByte();
                    num         = 1;
                    break;

                case AdsDatatypeId.ADST_UINT8:
                    sourceValue = reader.ReadByte();
                    num         = 1;
                    break;

                case AdsDatatypeId.ADST_UINT16:
                    sourceValue = reader.ReadUInt16();
                    num         = 2;
                    break;

                case AdsDatatypeId.ADST_UINT32:
                    sourceValue = reader.ReadUInt32();
                    num         = 4;
                    break;

                case AdsDatatypeId.ADST_INT64:
                    sourceValue = reader.ReadInt64();
                    num         = 8;
                    break;

                case AdsDatatypeId.ADST_UINT64:
                    sourceValue = reader.ReadUInt64();
                    num         = 8;
                    break;

                default:
                    switch (dataType)
                    {
                    case AdsDatatypeId.ADST_STRING:
                        sourceValue = reader.ReadPlcAnsiString(byteLength);
                        num         = byteLength;
                        break;

                    case AdsDatatypeId.ADST_WSTRING:
                        sourceValue = reader.ReadPlcUnicodeString(byteLength);
                        num         = byteLength;
                        break;

                    case AdsDatatypeId.ADST_BIT:
                        sourceValue = reader.ReadByte() > 0;
                        num         = 1;
                        break;

                    default:
                        if ((typeName == "TOD") || (typeName == "TIME"))
                        {
                            sourceValue = reader.ReadPlcTIME();
                            num         = 4;
                        }
                        else
                        {
                            if ((typeName != "DT") && (typeName != "DATE"))
                            {
                                throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
                            }
                            sourceValue = reader.ReadPlcDATE();
                            num         = 4;
                        }
                        break;
                    }
                    break;
                }
                break;
            }
            if ((managedType == null) || (managedType == sourceValue.GetType()))
            {
                value = sourceValue;
            }
            else
            {
                value = PrimitiveTypeConverter.Convert(sourceValue, managedType);
            }
            return(num);
        }