Beispiel #1
0
        /// <summary>
        /// Represents asynchronous processing of a modbus request
        /// </summary>
        /// <param name="request">An instance of modbus request to process</param>
        /// <returns>An enumeration of asynchronous actions to process</returns>
        protected virtual IEnumerable <Task> OnProcessRequestAsync <T>(IModbusClientRequest request) where T : IModbusResponse
        {
            this.ThrowIfDisposed();
            yield return(this._semaphore.WaitAsync());

            Task <IModbusResponse> readResult;

            try
            {
                yield return(this._transport.OpenConnectionAsync());

                var writeResult = this.WriteRequestAsync(request);
                yield return(writeResult);

                readResult = this.ReadResponseAsync(request);
                yield return(readResult);

                yield return(this._transport.CloseConnectionAsync());
            }
            finally
            {
                this._semaphore.Release();
            }
            if (readResult.Result == null)
            {
                this._logger.Log(RESPONSE_ERROR, Category.ERROR, Priority.HIGH);
                Guard.Throw <ClientModbusResponseException>(RESPONSE_ERROR);
            }
            request.ValidateResponse(readResult.Result);
            yield return(Task.Factory.FromResult((T)readResult.Result));
        }
Beispiel #2
0
        /// <summary>
        /// Processes modbus request message.
        /// </summary>
        /// <typeparam name="T">The type of response</typeparam>
        /// <param name="request">The modbus request message.</param>
        /// <returns>The result of message processing.</returns>
        internal virtual T ProcessRequest <T>(IModbusClientRequest request) where T : class, IModbusResponse
        {
            this.ThrowIfDisposed();
            T response;

            lock (this._syncObj)
            {
                this._transport.OpenConnection();
                this.WriteRequest(request);
                response = (T)this.ReadResponse(request);
                this._transport.CloseConnection();
            }
            if (response == null)
            {
                this._logger.Log(RESPONSE_ERROR, Category.ERROR, Priority.HIGH);
                Guard.Throw <ClientModbusResponseException>(RESPONSE_ERROR);
            }
            request.ValidateResponse(response);
            return(response);
        }