Beispiel #1
0
        private AdsErrorCode TryWriteSymbolic(ISymbol address, int offset, int byteSize, byte[] value)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (this.Connection == null)
            {
                throw new AdsException("Connection not established!");
            }
            int handle = 0;

            using (new AdsConnectionRestore(this))
            {
                AdsErrorCode code = this.Connection.RawInterface.TryCreateVariableHandle(address.InstancePath, false, out handle);
                if (code == AdsErrorCode.NoError)
                {
                    try
                    {
                        return(this.Connection.RawInterface.Write(handle, offset, byteSize, value, false));
                    }
                    finally
                    {
                        code = this.Connection.RawInterface.TryDeleteVariableHandle(handle, false);
                    }
                }
                return(AdsErrorCode.DeviceSymbolNotFound);
            }
        }
Beispiel #2
0
 private void CheckResult(bool throwAdsException, AdsErrorCode result)
 {
     if (throwAdsException && (result != AdsErrorCode.NoError))
     {
         ThrowAdsException(result);
     }
 }
Beispiel #3
0
        public AdsErrorCode TryDeleteVariableHandle(int variableHandle)
        {
            AdsErrorCode noError = AdsErrorCode.NoError;
            Dictionary <int, SymbolEntry> dictionary = this._symbolTable;

            lock (dictionary)
            {
                if (!this._symbolTable.ContainsKey(variableHandle))
                {
                    noError = AdsErrorCode.DeviceSymbolNotFound;
                }
                else
                {
                    SymbolEntry entry = this._symbolTable[variableHandle];
                    int         num   = entry.referenceCount - 1;
                    entry.referenceCount = num;
                    if (num != 0)
                    {
                        noError = AdsErrorCode.NoError;
                    }
                    else
                    {
                        noError = this._syncPort.Write(0xf006, 0, entry.serverHandle, false);
                        bool flag2 = this._symbolTable.Remove(variableHandle);
                        bool flag3 = this._symbolPathTable.Remove(entry.symbolPath);
                    }
                }
            }
            return(noError);
        }
Beispiel #4
0
        private AdsErrorCode TryReadSymbolic(ISymbol address, int numBytes, byte[] value, out int read)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (this.Connection == null)
            {
                throw new AdsException("Connection not established!");
            }
            AdsErrorCode noError = AdsErrorCode.NoError;

            using (new AdsConnectionRestore(this))
            {
                int handle = 0;
                noError = this.Connection.RawInterface.TryCreateVariableHandle(address.InstancePath, false, out handle);
                read    = 0;
                if (noError == AdsErrorCode.NoError)
                {
                    try
                    {
                        return(this.Connection.RawInterface.Read(handle, 0, numBytes, value, false, out read));
                    }
                    finally
                    {
                        AdsErrorCode code2 = this.Connection.RawInterface.TryDeleteVariableHandle(handle, false);
                        if (noError == AdsErrorCode.NoError)
                        {
                            noError = code2;
                        }
                    }
                }
                return(AdsErrorCode.DeviceSymbolNotFound);
            }
        }
Beispiel #5
0
        public AdsErrorCode Disconnect(Func <AdsErrorCode> action)
        {
            AdsErrorCode result = action();

            this.AfterDisconnect(result);
            return(result);
        }
Beispiel #6
0
        public AdsErrorCode TryRead(out object[] values, out AdsErrorCode[] returnCodes)
        {
            IList <SumDataEntity> sumEntities = this.CreateSumEntityInfos();

            base.innerCommand = new SumRead(base.connection, sumEntities, TwinCAT.Ads.SumCommand.SumCommand.SumAccessMode.IndexGroupIndexOffset);
            values            = null;
            IList <byte[]> readData = null;
            AdsErrorCode   code     = base.innerCommand.TryReadRaw(out readData, out returnCodes);

            if (code == AdsErrorCode.NoError)
            {
                values = new object[sumEntities.Count];
                IList <Symbol> unwrappedSymbols = base.UnwrappedSymbols;
                ValueAccessor  valueAccessor    = base.ValueAccessor;
                for (int i = 0; i < base.symbols.Count; i++)
                {
                    ISymbol symbol = base.symbols[i];
                    if (returnCodes[i] == AdsErrorCode.NoError)
                    {
                        values[i] = valueAccessor.ValueFactory.CreateValue(symbol, readData[i], 0, DateTime.UtcNow);
                    }
                }
            }
            return(code);
        }
Beispiel #7
0
 protected override void Dispose(bool disposing)
 {
     if (!base._disposed)
     {
         Module.Trace.TraceVerbose($"TcAdsSyncPort.Dispose({this._id:d}); disposing={disposing.ToString()}");
         if (disposing)
         {
             if (this._notificationMngt != null)
             {
                 this._notificationMngt.Dispose();
                 this._notificationMngt = null;
             }
             if (this._routerSyncWindow != null)
             {
                 this._routerSyncWindow.Dispose();
                 this._routerSyncWindow = null;
             }
             if (this._symbolTable != null)
             {
                 this._symbolTable.Dispose();
                 this._symbolTable = null;
             }
         }
         AdsErrorCode code = this.OnClosePort();
         base.Dispose(disposing);
     }
 }
 protected override void OnAfterWriteState(StateInfo adsState, AdsErrorCode result)
 {
     foreach (ICommunicationInterceptHandler handler in this._list)
     {
         handler.AfterWriteState(adsState, result);
     }
 }
 protected override void OnAfterDisconnect(AdsErrorCode errorCode)
 {
     foreach (ICommunicationInterceptHandler handler in this._list)
     {
         handler.AfterDisconnect(errorCode);
     }
 }
Beispiel #10
0
        public AdsErrorCode TryCreateHandles(out ISumHandleCollection handles)
        {
            IList <SumDataEntity> sumEntities = this.CreateSumEntityInfos();

            handles = null;
            AdsErrorCode[] returnCodes = null;
            List <byte[]>  writeData   = new List <byte[]>();
            IList <byte[]> readData    = null;

            foreach (InstancePathSumEntity entity in sumEntities)
            {
                writeData.Add(entity.GetWriteBytes());
            }
            base.innerCommand = new SumReadWrite(this._connection, sumEntities, TwinCAT.Ads.SumCommand.SumCommand.SumAccessMode.AquireHandleByName);
            AdsErrorCode code = base.innerCommand.TryReadWriteRaw(writeData, out readData, out returnCodes);

            if (code == AdsErrorCode.NoError)
            {
                handles = new SumHandleList();
                for (int i = 0; i < sumEntities.Count; i++)
                {
                    uint handle = 0;
                    if (returnCodes[i] == AdsErrorCode.NoError)
                    {
                        handle = BitConverter.ToUInt32(readData[i], 0);
                    }
                    handles.Add(new SumHandleInstancePathEntry(this._instancePaths[i], handle, returnCodes[i]));
                }
            }
            return(code);
        }
        public AdsErrorCode TryCreateHandles(out ISumHandleCollection handles)
        {
            IList <SumDataEntity> list = this.CreateSumEntityInfos();

            handles = null;
            AdsErrorCode[] returnCodes = null;
            IList <byte[]> readData    = null;

            int[]        readDataSizes = null;
            AdsErrorCode code          = base.Execute(null, out readData, out readDataSizes, out returnCodes);

            if (code == AdsErrorCode.NoError)
            {
                handles = new SumHandleList();
                for (int i = 0; i < list.Count; i++)
                {
                    uint notificationHandle = 0;
                    if (returnCodes[i] == AdsErrorCode.NoError)
                    {
                        notificationHandle = BitConverter.ToUInt32(readData[i], 0);
                    }
                    handles.Add(new SumNotificationHandleEntry(this._variableHandles[i], notificationHandle, returnCodes[i]));
                }
            }
            return(code);
        }
Beispiel #12
0
 private TcLocalSystem(TransportProtocol protocol)
 {
     object[] args = new object[] { this.GetHashCode() };
     using (new MethodTrace("ID: {0:d})", args))
     {
         if (protocol == TransportProtocol.All)
         {
             this._port = this.AdsPortOpen();
         }
         else
         {
             if (protocol != TransportProtocol.Router)
             {
                 throw new NotSupportedException("Enforced Tcp/IP Transport not available yet!");
             }
             this._port = this.AdsPortOpenEx();
         }
         this._routerPort = this._port >= 0x8000;
         if (this._port == 0)
         {
             ThrowAdsException(AdsErrorCode.ClientPortNotOpen);
         }
         this._refCount = 0;
         this._amsRouterNotificationDelegate = new TcAdsDllWrapper.AmsRouterNotificationDelegate(this.OnRouterNotification);
         base.address = new AmsAddress(base.GetLocalNetId(), AmsPort.SystemService);
         this.UpdateRouterState();
         AdsErrorCode code = this.AmsRegisterRouterNotification(this._amsRouterNotificationDelegate, false);
         this._routerNotificationsRegistered = code == AdsErrorCode.NoError;
     }
 }
 public DisposableNotificationExHandleBag(IAdsConnection client, IDictionary <string, AnyTypeSpecifier> dict, NotificationSettings settings, object userData) : base(client)
 {
     if (dict == null)
     {
         throw new ArgumentNullException("dict");
     }
     if (dict.Count == 0)
     {
         throw new ArgumentOutOfRangeException("dict");
     }
     base.handleDict      = new SumHandleList();
     base.validHandleDict = new Dictionary <string, uint>();
     foreach (KeyValuePair <string, AnyTypeSpecifier> pair in dict)
     {
         uint             handle    = 0;
         AnyTypeSpecifier specifier = pair.Value;
         Type             tp        = null;
         int[]            args      = null;
         specifier.GetAnyTypeArgs(out tp, out args);
         AdsErrorCode errorCode = client.TryAddDeviceNotificationEx(pair.Key, settings, userData, tp, args, out handle);
         base.handleDict.Add(new SumHandleInstancePathEntry(pair.Key, handle, errorCode));
         if (errorCode == AdsErrorCode.NoError)
         {
             base.validHandleDict.Add(pair.Key, handle);
         }
     }
 }
Beispiel #14
0
        public AdsErrorCode BeforeDisconnect(Func <AdsErrorCode> action)
        {
            AdsErrorCode code = action();

            ((ICommunicationInterceptHandler)this).BeforeDisconnect();
            return(code);
        }
Beispiel #15
0
        private void UpdateRouterState()
        {
            bool enabled = false;

            if (!base._disposed)
            {
                try
                {
                    if (this.AmsPortEnabled(false, out enabled) == AdsErrorCode.NoError)
                    {
                        if (enabled)
                        {
                            this._routerState = AmsRouterState.Start;
                        }
                        else
                        {
                            StateInfo    info;
                            AdsErrorCode code = this.ReadState(false, out info);
                            this._routerState = AmsRouterState.Stop;
                            if (code == AdsErrorCode.PortNotConnected)
                            {
                                this._routerState = AmsRouterState.Removed;
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    Module.Trace.TraceError(exception);
                }
            }
        }
Beispiel #16
0
        public virtual NotificationEntry DeleteNotification(int notificationHandle, out AdsErrorCode result)
        {
            result = AdsErrorCode.NoError;
            NotificationEntry entry = null;
            Dictionary <int, NotificationEntry> dictionary = this._notificationTable;

            lock (dictionary)
            {
                if (this._notificationTable.ContainsKey(notificationHandle))
                {
                    entry = this._notificationTable[notificationHandle];
                }
            }
            if (entry == null)
            {
                result = AdsErrorCode.ClientRemoveHash;
                return(null);
            }
            Dictionary <int, NotificationEntry> dictionary2 = this._notificationTable;

            lock (dictionary2)
            {
                this._notificationTable.Remove(notificationHandle);
            }
            return(entry);
        }
 protected override void OnAfterWriteState(TwinCAT.Ads.StateInfo adsState, AdsErrorCode result)
 {
     if (result == AdsErrorCode.NoError)
     {
         this.setAdsState(adsState);
     }
 }
Beispiel #18
0
 public LostCommunicationState(TimeSpan timeout, AdsErrorCode causingError) : base(timeout)
 {
     this._lostTime = DateTime.UtcNow;
     this._error    = causingError;
     object[] args = new object[] { causingError };
     Module.TraceSession.TraceInformation("FailFastHandlerInterceptor --> Lost (Caused by '{0}')", args);
 }
        public DisposableNotificationHandleBag(IAdsConnection client, IEnumerable <ISymbol> symbols, NotificationSettings settings, object userData) : base(client)
        {
            if (symbols == null)
            {
                throw new ArgumentNullException("dict");
            }
            int length = Enumerable.Sum <ISymbol>(symbols, s => s.ByteSize);

            this._stream           = new AdsStream(length);
            base.handleDict        = new SumHandleList();
            this._handleSymbolDict = new Dictionary <uint, ISymbol>();
            base.validHandleDict   = new Dictionary <string, uint>();
            int offset = 0;

            foreach (ISymbol symbol in symbols)
            {
                uint         handle    = 0;
                int          byteSize  = symbol.ByteSize;
                AdsErrorCode errorCode = client.TryAddDeviceNotification(symbol.InstancePath, this._stream, offset, byteSize, settings, userData, out handle);
                base.handleDict.Add(new SumHandleInstancePathEntry(symbol.InstancePath, handle, errorCode));
                if (errorCode == AdsErrorCode.NoError)
                {
                    base.validHandleDict.Add(symbol.InstancePath, handle);
                    this._handleSymbolDict.Add(handle, symbol);
                }
                offset += byteSize;
            }
        }
Beispiel #20
0
 public AdsErrorCode InjectError(AdsErrorCode error, bool throwAdsException)
 {
     if (throwAdsException)
     {
         ThrowAdsException(error);
     }
     return(error);
 }
Beispiel #21
0
 private static AdsErrorException CreateException(string message, AdsErrorCode adsErrorCode)
 {
     if (adsErrorCode == AdsErrorCode.NoError)
     {
         throw new ArgumentException("No error indicated!", "adsErrorCode");
     }
     return(new AdsErrorException(ResMan.GetString(message, adsErrorCode), adsErrorCode));
 }
Beispiel #22
0
        public AdsErrorCode AdsWriteRes(AmsAddress rAddr, uint invokeId, AdsErrorCode result)
        {
            TcAdsWriteResHeader adsHeader = new TcAdsWriteResHeader {
                _result = result
            };

            return(this.AdsResponse(rAddr, invokeId, 3, 0, adsHeader, null));
        }
Beispiel #23
0
        public AdsErrorCode InjectError(AdsErrorCode error, bool throwAdsException)
        {
            AdsErrorCode        noError = AdsErrorCode.NoError;
            Func <AdsErrorCode> del     = () => ((IAdsErrorInjector)this._inner).InjectError(error, false);

            noError = this.Invoke(del);
            this.CheckResult(throwAdsException, noError);
            return(noError);
        }
Beispiel #24
0
 public override void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     if (info == null)
     {
         throw new ArgumentNullException("info");
     }
     this._errorCode = (AdsErrorCode)info.GetInt32("ErrorCode");
     this.GetObjectData(info, context);
 }
Beispiel #25
0
        public void Connect()
        {
            AdsErrorCode adsErrorCode = this.OnOpenPort();

            if (adsErrorCode != AdsErrorCode.NoError)
            {
                ThrowAdsException(adsErrorCode);
            }
        }
Beispiel #26
0
        public void Disconnect()
        {
            AdsErrorCode adsErrorCode = this.OnClosePort();

            if (adsErrorCode != AdsErrorCode.NoError)
            {
                ThrowAdsException(adsErrorCode);
            }
        }
Beispiel #27
0
        public override int TryWriteArrayElementValue(ISymbol arraySymbol, int[] indices, byte[] value, int valOffset, out DateTime writeTime)
        {
            if (this.Connection == null)
            {
                throw new AdsException("Connection not established!");
            }
            if (arraySymbol == null)
            {
                throw new ArgumentNullException("arraySymbol");
            }
            AdsErrorCode    noError   = AdsErrorCode.NoError;
            IArrayType      arrayType = null;
            IResolvableType dataType  = arraySymbol.DataType as IResolvableType;

            arrayType = (dataType == null) ? (arraySymbol.DataType as IArrayType) : (dataType.ResolveType(DataTypeResolveStrategy.AliasReference) as IArrayType);
            if (arrayType == null)
            {
                throw new ArgumentException("Is not an array type", "arraySymbol");
            }
            ValueAccessMode mode = this.calcAccessMethod(arraySymbol);

            writeTime = DateTime.MinValue;
            if ((indices == null) || (indices.Length == 0))
            {
                int        byteSize = arraySymbol.ByteSize;
                IAdsSymbol symbol2  = (IAdsSymbol)Symbol.Unwrap(arraySymbol);
                if (value.Length != byteSize)
                {
                    throw new ArgumentException("Value array size mismatch!", "value");
                }
                if (mode == ValueAccessMode.IndexGroupOffset)
                {
                    noError = this.Connection.RawInterface.Write(symbol2.IndexGroup, symbol2.IndexOffset, valOffset, byteSize, value, false);
                }
            }
            else
            {
                ArrayType.CheckIndices(indices, arrayType, false);
                int elementOffset = ArrayType.GetElementOffset(indices, arrayType);
                int byteSize      = arrayType.ElementType.ByteSize;
                if (mode != ValueAccessMode.IndexGroupOffset)
                {
                    noError = this.TryWriteSymbolic(arraySymbol, valOffset, byteSize, value);
                }
                else
                {
                    IAdsSymbol symbol = (IAdsSymbol)Symbol.Unwrap(arraySymbol);
                    noError = this.Connection.RawInterface.Write(symbol.IndexGroup, symbol.IndexOffset + ((uint)elementOffset), valOffset, byteSize, value, false);
                }
            }
            if (noError != AdsErrorCode.NoError)
            {
                writeTime = DateTime.UtcNow;
            }
            return((int)noError);
        }
        protected override unsafe AdsErrorCode GetLocalAddress(byte *data, bool throwAdsException)
        {
            AdsErrorCode adsErrorCode = TcAdsDllWrapper.UnsafeNativeMethods.AdsGetLocalAddressEx(base._port, (void *)data);

            if ((adsErrorCode != AdsErrorCode.NoError) & throwAdsException)
            {
                ThrowAdsException(adsErrorCode);
            }
            return(adsErrorCode);
        }
        protected override AdsErrorCode SetTimeout(int timeout, bool throwAdsException)
        {
            AdsErrorCode adsErrorCode = TcAdsDllWrapper.UnsafeNativeMethods.AdsSyncSetTimeoutEx(base._port, timeout);

            if ((adsErrorCode != AdsErrorCode.NoError) & throwAdsException)
            {
                ThrowAdsException(adsErrorCode);
            }
            return(adsErrorCode);
        }
Beispiel #30
0
        public void WriteRaw(IList <byte[]> writeData)
        {
            AdsErrorCode[] returnCodes = null;
            AdsErrorCode   code        = this.TryWriteRaw(writeData, out returnCodes);

            if (base.Failed)
            {
                throw new AdsSumCommandException("SumWriteCommand failed!", this);
            }
        }