Esempio n. 1
0
        private void ReceivedS7PlcBlockInfoAckDatagram(Memory <byte> buffer)
        {
            var data = S7PlcBlockInfoAckDatagram.TranslateFromMemory(buffer);

            if (_blockInfoHandler.TryGetValue(data.UserData.Header.ProtocolDataUnitReference, out var cbh))
            {
                if (data.UserData.Parameter.ParamErrorCode != (int)ErrorParameter.AtLeatOneOfTheGivenBlocksNotFound)
                {
                    if (data.UserData.Parameter.ParamErrorCode != 0)
                    {
                        _logger?.LogError("Error while reading blockdata for reference {0}. ParamErrorCode: {1}", data.UserData.Header.ProtocolDataUnitReference, data.UserData.Parameter.ParamErrorCode);
                        cbh.Exception = new Dacs7ParameterException(data.UserData.Parameter.ParamErrorCode);
                        cbh.Event.Set(null);
                    }
                    if (data.UserData.Data == null)
                    {
                        _logger?.LogWarning("No data from blockinfo ack received for reference {0}", data.UserData.Header.ProtocolDataUnitReference);
                    }
                }
                cbh.Event.Set(data);
            }
            else
            {
                _logger?.LogWarning("No block info handler found for received read ack reference {0}", data.UserData.Header.ProtocolDataUnitReference);
            }
        }
Esempio n. 2
0
        public async Task <S7PlcBlockInfoAckDatagram> ReadBlockInfoAsync(PlcBlockType type, int blocknumber)
        {
            if (ConnectionState != ConnectionState.Opened)
            {
                ThrowHelper.ThrowNotConnectedException();
            }

            var id = GetNextReferenceId();

            using (var dg = S7UserDataDatagram.TranslateToMemory(S7UserDataDatagram.BuildBlockInfoRequest(_s7Context, id, type, blocknumber), out var memoryLength))
            {
                using (var sendData = _transport.Build(dg.Memory.Slice(0, memoryLength), out var sendLength))
                {
                    try
                    {
                        CallbackHandler <S7PlcBlockInfoAckDatagram> cbh;
                        S7PlcBlockInfoAckDatagram blockinfoResult = null;
                        using (await SemaphoreGuard.Async(_concurrentJobs).ConfigureAwait(false))
                        {
                            cbh = new CallbackHandler <S7PlcBlockInfoAckDatagram>(id);
                            _blockInfoHandler.TryAdd(cbh.Id, cbh);
                            try
                            {
                                if (await _transport.Client.SendAsync(sendData.Memory.Slice(0, sendLength)).ConfigureAwait(false) != SocketError.Success)
                                {
                                    return(null);
                                }
                                blockinfoResult = await cbh.Event.WaitAsync(_s7Context.Timeout).ConfigureAwait(false);
                            }
                            finally
                            {
                                _blockInfoHandler.TryRemove(cbh.Id, out _);
                            }
                        }

                        HandlerErrorResult(id, cbh, blockinfoResult);

                        return(blockinfoResult);
                    }
                    catch (TaskCanceledException)
                    {
                        ThrowHelper.ThrowTimeoutException();
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
 private void HandlerErrorResult(ushort id, CallbackHandler <S7PlcBlockInfoAckDatagram> cbh, S7PlcBlockInfoAckDatagram blockinfoResult)
 {
     if (blockinfoResult == null)
     {
         if (_closeCalled)
         {
             ThrowHelper.ThrowNotConnectedException();
         }
         else
         {
             if (cbh.Exception != null)
             {
                 ThrowHelper.ThrowException(cbh.Exception);
             }
             ThrowHelper.ThrowWriteTimeoutException(id);
         }
     }
 }
Esempio n. 4
0
        public async Task <S7PlcBlockInfoAckDatagram> ReadBlockInfoAsync(PlcBlockType type, int blocknumber)
        {
            if (_closeCalled || ConnectionState != ConnectionState.Opened)
            {
                ThrowHelper.ThrowNotConnectedException();
            }

            var id = GetNextReferenceId();

            using (var dg = S7UserDataDatagram.TranslateToMemory(S7UserDataDatagram.BuildBlockInfoRequest(_s7Context, id, type, blocknumber), out var memoryLength))
            {
                using (var sendData = _transport.Build(dg.Memory.Slice(0, memoryLength), out var sendLength))
                {
                    try
                    {
                        CallbackHandler <S7PlcBlockInfoAckDatagram> cbh = null;
                        S7PlcBlockInfoAckDatagram blockinfoResult       = null;
                        try
                        {
                            if (_concurrentJobs == null)
                            {
                                return(null);
                            }

                            using (await SemaphoreGuard.Async(_concurrentJobs).ConfigureAwait(false))
                            {
                                cbh = new CallbackHandler <S7PlcBlockInfoAckDatagram>(id);
                                if (_blockInfoHandler.TryAdd(id, cbh))
                                {
                                    _logger?.LogTrace("Metadata read handler with id {id} was added.", id);

                                    try
                                    {
                                        if (await _transport.Connection.SendAsync(sendData.Memory.Slice(0, sendLength)).ConfigureAwait(false) != SocketError.Success)
                                        {
                                            // we return false, because if one send faild we expect also all other ones failed.
                                            _logger?.LogWarning("Could not send metadata read package with reference <{id}>.", id);
                                            return(null);
                                        }
                                        blockinfoResult = await cbh.Event.WaitAsync(_s7Context.Timeout).ConfigureAwait(false);
                                    }
                                    finally
                                    {
                                        _blockInfoHandler.TryRemove(id, out _);
                                        _logger?.LogTrace("Metadata read handler with id {id} was removed.", id);
                                    }
                                }
                                else
                                {
                                    _logger?.LogWarning("Could not add metadata read handler with reference <{id}>.", id);
                                }
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            if (cbh == null)
                            {
                                return(null);
                            }
                        }

                        HandlerErrorResult(id, cbh, blockinfoResult);

                        return(blockinfoResult);
                    }
                    catch (TaskCanceledException)
                    {
                        ThrowHelper.ThrowTimeoutException();
                    }
                }
            }

            return(null);
        }