Ejemplo n.º 1
0
        private string QueryHostname(AmsNetId amsNetId)
        {
            using (var client = new TcAdsClient(AdsClientSettings.Default))
            {
                client.Connect(amsNetId, AmsPort.SystemService);

                var stream = new AdsStream(256);
                var error  = client.TryRead(702 /*SYSTEMSERVICE_IPHOSTNAME*/, 0, stream, out int readBytes);
                if (error != AdsErrorCode.NoError)
                {
                    _logger.LogWarning("Error {error} query hostname for {amsNetId}.", error, amsNetId);
                    return(null);
                }

                var hostname = Encoding.GetEncoding(1252).GetString(stream.GetBuffer(), 0, readBytes - 1);
                return(hostname);
            }
        }
Ejemplo n.º 2
0
        private unsafe void loadSymbols(TimeSpan timeout)
        {
            switch (this._settings.SymbolsLoadMode)
            {
            case SymbolsLoadMode.Flat:
                this._symbols = new SymbolCollection(InstanceCollectionMode.Path);
                break;

            case SymbolsLoadMode.VirtualTree:
            case SymbolsLoadMode.DynamicTree:
                this._symbols = new SymbolCollection(InstanceCollectionMode.PathHierarchy);
                break;

            default:
                throw new NotSupportedException();
            }
            int num = this._connection.Timeout;

            if (this.UploadInfo.SymbolsBlockSize > 0)
            {
                AdsStream input = new AdsStream(this.UploadInfo.SymbolsBlockSize);
                using (BinaryReader reader = new BinaryReader(input))
                {
                    this._connection.Timeout = (int)timeout.TotalMilliseconds;
                    try
                    {
                        int num2 = 0x10000;
                        if (this.UploadInfo.SymbolsBlockSize >= num2)
                        {
                            uint num3   = 0;
                            int  num4   = 0;
                            int  num5   = 0;
                            int  num6   = 0;
                            int  length = 0;
                            while (true)
                            {
                                int dataRead = 0;
                                while (true)
                                {
                                    AdsErrorCode    code;
                                    ref byte pinned numRef;
                                    length = ((num4 + num2) <= input.Length) ? num2 : (((int)input.Length) - num4);
                                    try
                                    {
                                        byte[] buffer;
                                        if (((buffer = input.GetBuffer()) == null) || (buffer.Length == 0))
                                        {
                                            numRef = null;
                                        }
                                        else
                                        {
                                            numRef = buffer;
                                        }
                                        code = this._connection.RawInterface.Read(0xf00b, 0x80000000 | num3, length, (void *)(numRef + num4), false, out dataRead);
                                    }
                                    finally
                                    {
                                        numRef = null;
                                    }
                                    if (code != AdsErrorCode.NoError)
                                    {
                                        if (code == AdsErrorCode.DeviceInvalidSize)
                                        {
                                            num2 *= 4;
                                        }
                                        else
                                        {
                                            TcAdsDllWrapper.ThrowAdsException(code);
                                        }
                                    }
                                    if (code == AdsErrorCode.NoError)
                                    {
                                        num2           = 0x10000;
                                        num5           = num4;
                                        num6           = 0;
                                        input.Position = num4;
                                        num6           = reader.ReadInt32();
                                        while (true)
                                        {
                                            if ((num6 > 0) && (num3 < this.UploadInfo.SymbolCount))
                                            {
                                                num5          += num6;
                                                input.Position = num5;
                                                num3++;
                                                if ((num5 < (num4 + num2)) && (num5 < input.Length))
                                                {
                                                    num6 = reader.ReadInt32();
                                                    continue;
                                                }
                                            }
                                            num4 = num5;
                                            if ((num3 < this.UploadInfo.SymbolCount) && (num4 < input.Length))
                                            {
                                                break;
                                            }
                                            break;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            this._connection.Read(0xf00b, 0, input);
                        }
                        SymbolParser.ParseSymbols(input, this.UploadInfo.StringEncoding, this._symbolFactorServices);
                    }
Ejemplo n.º 3
0
        public WoopsaValue ReadAdsValue(IWoopsaProperty woopsaProperty)
        {
            WoopsaAdsProperty property = (WoopsaAdsProperty)woopsaProperty;
            AdsStream         stream   = new AdsStream(80); // for STRING(80)
            long data = 0;

            stream.Flush();
            try
            {
                _tcAds.Read(property.AdsInfo.IndexGroup, property.AdsInfo.IndexOffset, stream);
            }
            catch (Exception)
            {
                isAdsConnected = false;
                return(null);
            }
            switch (property.Type)
            {
            case WoopsaValueType.Integer:
                if (property.AdsInfo.Type == BeckhoffValueType.WORD ||
                    property.AdsInfo.Type == BeckhoffValueType.UINT)
                {
                    return(BitConverter.ToUInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DWORD ||
                         property.AdsInfo.Type == BeckhoffValueType.UDINT)
                {
                    return(BitConverter.ToUInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.SINT)
                {
                    return(Convert.ToSByte((sbyte)stream.GetBuffer()[0]));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.INT)
                {
                    return(BitConverter.ToInt16(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.DINT)
                {
                    return(BitConverter.ToInt32(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LINT || property.AdsInfo.Type == BeckhoffValueType.ULINT)
                {
                    return(BitConverter.ToInt64(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.USINT || property.AdsInfo.Type == BeckhoffValueType.BYTE)
                {
                    return((byte)stream.GetBuffer()[0]);
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Integer");
                }

            case WoopsaValueType.Logical:
                return(Convert.ToBoolean(stream.GetBuffer()[0]));

            case WoopsaValueType.Real:
                if (property.AdsInfo.Type == BeckhoffValueType.REAL)
                {
                    return(BitConverter.ToSingle(stream.GetBuffer(), 0));
                }
                else if (property.AdsInfo.Type == BeckhoffValueType.LREAL)
                {
                    return(BitConverter.ToDouble(stream.GetBuffer(), 0));
                }
                else
                {
                    throw new WoopsaException("Ads type not compatible with Woopsa Real");
                }

            case WoopsaValueType.Text:
                string s     = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());
                int    index = s.IndexOf('\0');
                return(s.Remove(index, s.Length - index));

            case WoopsaValueType.TimeSpan:
                data = bufferToLong(stream.GetBuffer(), 4);
                TimeSpan timeSpan = new TimeSpan(TimeSpan.TicksPerMillisecond * data);
                return(timeSpan);

            case WoopsaValueType.DateTime:
                TimeSpan timeSp;
                DateTime dateTime;
                data     = bufferToLong(stream.GetBuffer(), 4);
                dateTime = BeckhoffPlcReferenceDateTime;
                if (property.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY)
                {
                    timeSp = TimeSpan.FromMilliseconds(data);
                }
                else
                {
                    timeSp = TimeSpan.FromSeconds(data);
                }
                dateTime = dateTime + timeSp;
                WoopsaValue value = new WoopsaValue(dateTime);
                return(value);

            default:
                return(null);
            }
        }
Ejemplo n.º 4
0
        public WoopsaValue ReadAdsValue(IWoopsaProperty woopsaProperty)
        {
            WoopsaAdsProperty property = (WoopsaAdsProperty)woopsaProperty;
            AdsStream stream = new AdsStream(80); // for STRING(80)
            long data = 0;

            stream.Flush();
            try
            {
                _tcAds.Read(property.AdsInfo.IndexGroup, property.AdsInfo.IndexOffset, stream);
            }
            catch (Exception)
            {
                isAdsConnected = false;
                return null;
            }
            switch (property.Type)
            {
                case WoopsaValueType.Integer:
                    if (property.AdsInfo.Type == BeckhoffValueType.WORD ||
                        property.AdsInfo.Type == BeckhoffValueType.UINT)
                        return BitConverter.ToUInt16(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.DWORD ||
                            property.AdsInfo.Type == BeckhoffValueType.UDINT)
                        return BitConverter.ToUInt32(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.SINT)
                        return Convert.ToSByte((sbyte)stream.GetBuffer()[0]);
                    else if (property.AdsInfo.Type == BeckhoffValueType.INT)
                        return BitConverter.ToInt16(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.DINT)
                        return BitConverter.ToInt32(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.LINT || property.AdsInfo.Type == BeckhoffValueType.ULINT)
                        return BitConverter.ToInt64(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.USINT || property.AdsInfo.Type == BeckhoffValueType.BYTE)
                        return (byte)stream.GetBuffer()[0];
                    else
                        throw new WoopsaException("Ads type not compatible with Woopsa Integer");
                case WoopsaValueType.Logical:
                    return Convert.ToBoolean(stream.GetBuffer()[0]);
                case WoopsaValueType.Real:
                    if (property.AdsInfo.Type == BeckhoffValueType.REAL)
                        return BitConverter.ToSingle(stream.GetBuffer(), 0);
                    else if (property.AdsInfo.Type == BeckhoffValueType.LREAL)
                        return BitConverter.ToDouble(stream.GetBuffer(), 0);
                    else
                        throw new WoopsaException("Ads type not compatible with Woopsa Real");
                case WoopsaValueType.Text:
                    string s = System.Text.Encoding.ASCII.GetString(stream.GetBuffer());
                    int index = s.IndexOf('\0');
                    return s.Remove(index, s.Length - index);
                case WoopsaValueType.TimeSpan:
                    data = bufferToLong(stream.GetBuffer(), 4);
                    TimeSpan timeSpan = new TimeSpan(TimeSpan.TicksPerMillisecond * data);
                    return timeSpan;
                case WoopsaValueType.DateTime:
                    TimeSpan timeSp;
                    DateTime dateTime;
                    data = bufferToLong(stream.GetBuffer(), 4);
                    dateTime = BeckhoffPlcReferenceDateTime;
                    if (property.AdsInfo.Type == BeckhoffValueType.TIME_OF_DAY)
                        timeSp = TimeSpan.FromMilliseconds(data);
                    else
                        timeSp = TimeSpan.FromSeconds(data);
                    dateTime = dateTime + timeSp;
                    WoopsaValue value = new WoopsaValue(dateTime);
                    return value;
                default:
                    return null;
            }
        }
Ejemplo n.º 5
0
        public object ReadSymbol(string symbolPath, Type managedType, bool bReloadInfo)
        {
            AdsErrorCode code2;

            if (bReloadInfo)
            {
                this.Cleanup();
            }
            TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(symbolPath, true);

            if (adsSymbol == null)
            {
                TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound);
            }
            TcAdsDataType type = (TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName);

            if (type == null)
            {
                throw new NotSupportedException("Type of symbol not supported");
            }
            if (adsSymbol.IndexGroup != 0xf005L)
            {
                AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol);
                if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut)
                {
                    TcAdsDllWrapper.ThrowAdsException(adsErrorCode);
                }
            }
            AdsStream       stream = new AdsStream(adsSymbol.Size);
            bool            flag   = true;
            AdsBinaryReader reader = new AdsBinaryReader(stream);
            int             num2   = 0;

            goto TR_001C;
TR_000F:
            num2++;
TR_001C:
            while (true)
            {
                int num3;
                if (!((num2 < 2) & flag))
                {
                    object        obj2    = null;
                    int           num     = 0;
                    TcAdsDataType adsType = (TcAdsDataType)type.ResolveType(DataTypeResolveStrategy.AliasReference);
                    if (adsType.IsEnum)
                    {
                        num = this.InitializeEnum(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsArray)
                    {
                        num = this.InitializeArray(managedType, adsType, reader, 0, -1, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsStruct)
                    {
                        num = this.InitializeStruct(adsType.SubItems, managedType, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsPointer)
                    {
                        num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (adsType.IsPrimitive)
                    {
                        num = this.InitializePrimitiveType(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                        return(obj2);
                    }
                    if (!adsType.IsPointer)
                    {
                        throw new NotSupportedException("Type of symbol not supported");
                    }
                    num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2);
                    return(obj2);
                }
                flag  = false;
                code2 = this._adsClient.RawInterface.Read((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false, out num3);
                if (code2 > AdsErrorCode.DeviceInvalidOffset)
                {
                    if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid))
                    {
                        break;
                    }
                }
                else if (code2 == AdsErrorCode.NoError)
                {
                    goto TR_000F;
                }
                else if (code2 != AdsErrorCode.DeviceInvalidOffset)
                {
                    break;
                }
                if (adsSymbol.IndexGroup == 0xf005L)
                {
                    uint num4;
                    this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false);
                    if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, symbolPath, false, out num4) == AdsErrorCode.NoError)
                    {
                        adsSymbol.IndexGroup  = 0xf005L;
                        adsSymbol.IndexOffset = num4;
                        flag = true;
                    }
                }
                goto TR_000F;
            }
            TcAdsDllWrapper.ThrowAdsException(code2);
            goto TR_000F;
        }
Ejemplo n.º 6
0
        public object InvokeRpcMethod(ITcAdsSymbol symbol, IRpcMethod rpcMethod, object[] parameterValues)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }
            if (rpcMethod == null)
            {
                throw new ArgumentNullException("rpcMethod");
            }
            if (parameterValues == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (rpcMethod.Parameters.Count != parameterValues.Length)
            {
                throw new ArgumentOutOfRangeException("parameters", $"Parameter mismatch! The number of expected parameters is '{rpcMethod.Parameters.Count}");
            }
            object         returnValue  = null;
            string         variableName = $"{symbol.Name}#{rpcMethod.Name}";
            ITcAdsDataType type         = null;

            if (!rpcMethod.IsVoid)
            {
                type = (ITcAdsDataType)this._datatypeTable.ResolveDataType(rpcMethod.ReturnType);
            }
            List <IDataType> list = new List <IDataType>();

            foreach (RpcMethodParameter parameter in rpcMethod.Parameters)
            {
                ITcAdsDataType item = (ITcAdsDataType)this._datatypeTable.ResolveDataType(parameter.TypeName);
                if (item == null)
                {
                    throw new AdsDatatypeNotSupportedException();
                }
                list.Add(item);
            }
            int wrLength = 0;
            SymbolAdsMarshaller marshaller = new SymbolAdsMarshaller(this._datatypeTable);

            object[] objArray = new object[0];
            wrLength = marshaller.GetInMarshallingSize(rpcMethod, objArray);
            int outMarshallingSize = marshaller.GetOutMarshallingSize(rpcMethod, objArray);
            int variableHandle     = this._adsClient.CreateVariableHandle(variableName);

            try
            {
                byte[] buffer = new byte[wrLength];
                int    num4   = marshaller.MarshallParameters((RpcMethod)rpcMethod, parameterValues, buffer, 0);
                using (AdsStream stream = new AdsStream(buffer))
                {
                    using (AdsStream stream2 = new AdsStream(outMarshallingSize))
                    {
                        int num5 = 0;
                        num5 = this._adsClient.ReadWrite(variableHandle, stream2, 0, outMarshallingSize, stream, 0, wrLength);
                        if (outMarshallingSize > 0)
                        {
                            int num6 = marshaller.UnmarshalRpcMethod(rpcMethod, parameterValues, stream2.GetBuffer(), out returnValue);
                        }
                    }
                }
            }
            finally
            {
                this._adsClient.DeleteVariableHandle(variableHandle);
            }
            return(returnValue);
        }
Ejemplo n.º 7
0
        public void WriteSymbol(string name, object value, bool bReloadInfo)
        {
            AdsErrorCode code2;

            if (bReloadInfo)
            {
                this.Cleanup();
            }
            TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(name, true);

            if (adsSymbol == null)
            {
                TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound);
            }
            TcAdsDataType type = (TcAdsDataType)((TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName)).ResolveType(DataTypeResolveStrategy.AliasReference);

            if (type == null)
            {
                throw new NotSupportedException("Type of symbol not supported");
            }
            if (adsSymbol.IndexGroup != 0xf005L)
            {
                AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol);
                if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut)
                {
                    TcAdsDllWrapper.ThrowAdsException(adsErrorCode);
                }
            }
            AdsStream       stream = new AdsStream(adsSymbol.Size);
            AdsBinaryWriter writer = new AdsBinaryWriter(stream);

            if (type.IsEnum)
            {
                this.WriteEnumValue(adsSymbol.Name, value, type, writer, 0);
            }
            else if (type.IsArray)
            {
                this.WriteArray(value, type, writer, 0);
            }
            else if (type.IsStruct)
            {
                this.WriteStruct(value, type.SubItems, writer, 0);
            }
            else if (type.IsPointer)
            {
                this.WritePointerValue(adsSymbol.Name, value, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0);
            }
            else
            {
                if (!type.IsPrimitive)
                {
                    throw new NotSupportedException("Type of symbol not supported");
                }
                this.WritePrimitiveValue(adsSymbol.Name, value, type.ManagedType, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0);
            }
            bool flag = true;
            int  num  = 0;

            goto TR_000F;
TR_0002:
            num++;
TR_000F:
            while (true)
            {
                if (!((num < 2) & flag))
                {
                    return;
                }
                flag  = false;
                code2 = this._adsClient.RawInterface.Write((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false);
                if (code2 > AdsErrorCode.DeviceInvalidOffset)
                {
                    if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid))
                    {
                        break;
                    }
                }
                else if (code2 == AdsErrorCode.NoError)
                {
                    goto TR_0002;
                }
                else if (code2 != AdsErrorCode.DeviceInvalidOffset)
                {
                    break;
                }
                if (adsSymbol.IndexGroup == 0xf005L)
                {
                    uint num2;
                    this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false);
                    if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, name, false, out num2) == AdsErrorCode.NoError)
                    {
                        adsSymbol.IndexGroup  = 0xf005L;
                        adsSymbol.IndexOffset = num2;
                        flag = true;
                    }
                }
                goto TR_0002;
            }
            TcAdsDllWrapper.ThrowAdsException(code2);
            goto TR_0002;
        }