private IEnumerable <Task> OnReadResponseAsync(IModbusClientRequest request) { var bytesToRead = ProtocolConstants.MBAP_HEADER_LENGTH; var mbapHeader = new byte[bytesToRead]; var numBytesRead = 0; while (numBytesRead != ProtocolConstants.MBAP_HEADER_LENGTH) { var readTask = this._transport.ReadAsync(mbapHeader, numBytesRead, bytesToRead - numBytesRead); yield return(readTask); numBytesRead = readTask.Result; if (numBytesRead == 0) { this._logger.Log(ResourceConstants.TCP_MODBUS_READ_RESULT_WITH_ZERO_BYTES_MESSAGE, Category.ERROR, Priority.HIGH); Guard.Throw <ClientModbusResponseException>( ResourceConstants.TCP_MODBUS_READ_RESULT_WITH_ZERO_BYTES_MESSAGE); } } bytesToRead = (ushort)(mbapHeader[5] | (mbapHeader[4] << 8)); var frame = new byte[bytesToRead]; numBytesRead = 0; while (numBytesRead != bytesToRead) { var readTask = this._transport.ReadAsync(frame, numBytesRead, bytesToRead - numBytesRead); yield return(readTask); numBytesRead = readTask.Result; if (numBytesRead == 0) { this._logger.Log(ResourceConstants.TCP_MODBUS_READ_RESULT_WITH_ZERO_BYTES_MESSAGE, Category.ERROR, Priority.HIGH); Guard.Throw <ClientModbusResponseException>( ResourceConstants.TCP_MODBUS_READ_RESULT_WITH_ZERO_BYTES_MESSAGE); } } var response = request.CreateResponseFromFrame(mbapHeader.CombineArray(frame)); yield return(Task.Factory.FromResult(response)); }
/// <summary> /// Reads a modbus response from a transport. /// </summary> /// <param name="request"></param> /// <returns>An instance of a modbus response that has recently been read.</returns> protected override IModbusResponse ReadResponse(IModbusClientRequest request) { var requestResponse = this.ReadResponseInternal(); return(request.CreateResponseFromFrame(requestResponse)); }