private void CopyToLast(NurApi.CustomExchangeParams xch)
        {
            mLastExchange.appendHandle = xch.appendHandle;
            /* May require write like response wait. */
            mLastExchange.asWrite = xch.asWrite;
            /* Copy built bytes. */
            mLastExchange.bitBuffer = xch.bitBuffer;            // = new byte[NurApi.MAX_BITSTR_BITS / 8];
            //System.Array.Copy(bb.bytes, xch.bitBuffer, byteLength);

            /* We want the RX CRC. */
            mLastExchange.noRxCRC = xch.noRxCRC;
            /* By default all the commands are appended with CRC. */
            mLastExchange.noTxCRC = xch.noTxCRC;
            /* Number of bits expected in the response including the 16-bit handle. */
            mLastExchange.rxLen = xch.rxLen;
            /* If length is set to 0 it means that the response length is unknown. */
            mLastExchange.rxLenUnknown = xch.rxLenUnknown;
            /* The application may want to see the handle. */
            mLastExchange.rxStripHandle = xch.rxStripHandle;
            /* Use the set timeout. */
            mLastExchange.rxTimeout = xch.rxTimeout;
            /* TX shall use normal CRC. */
            mLastExchange.txCRC5 = xch.txCRC5;
            /* Copy number of bits to transmit. */
            mLastExchange.txLen = xch.txLen;
            /* We want a response. */
            mLastExchange.txOnly = xch.txOnly;
            /* There is no cover coding required. */
            mLastExchange.xorRN16 = xch.xorRN16;

            mLastIsValid = true;
        }
        /// <summary>
        /// Build default custom excange parameters.
        /// </summary>
        /// <param name="rxLen">Reception length in bits.</param>
        /// <param name="asWrite">If true then the reader's AFE shall behave like the reponse is to a write (longer timeout etc.).</param>
        /// <param name="stripHandle">Whether to strip the handle from the response  or not.</param>
        /// <returns>Pre-built custom exchange parameters, <see cref="NurApi.CustomExchangeParams"/>.</returns>
        public static NurApi.CustomExchangeParams BuildDefault(ushort rxLen, bool asWrite, bool stripHandle)
        {
            NurApi.CustomExchangeParams xch = new NurApi.CustomExchangeParams();

            // Each command has an appended handle.
            xch.appendHandle = BoolToUint(true);
            // Controls the behavior of the reader's AFE when operation is write or write-like.
            xch.asWrite = BoolToUint(asWrite);

            // Maximum number of bits -> bytes.
            xch.bitBuffer = new byte[NurApi.MAX_BITSTR_BITS / 8];
            // Responses expect CRC-16.
            xch.noRxCRC = BoolToUint(false);
            // All commands use trasnmission's CRC-16.
            xch.noTxCRC = BoolToUint(false);

            // Careful.
            if (rxLen == 0)
            {
                xch.rxLen        = 0;
                xch.rxLenUnknown = BoolToUint(true);
            }
            else
            {
                xch.rxLen        = rxLen;
                xch.rxLenUnknown = BoolToUint(false);
            }

            // Usually the handle is removed from the responses.
            // If a command contains handle only then this may be in order to set to 0.
            xch.rxStripHandle = BoolToUint(stripHandle);

            // Transmission does not use CRC-5.
            xch.txCRC5 = BoolToUint(false);

            // Updated when the transmitted bitstream is built.
            xch.txLen = 0;

            // No. We are expecting a response from the tag.
            xch.txOnly = BoolToUint(false);

            // No. There is no 'write-like' 16-bit data to be transmitted.
            xch.xorRN16 = BoolToUint(false);

            return(xch);
        }
        /// <summary>
        /// Build default exchange.
        /// </summary>
        /// <param name="bb">Bit buffer to copy the bit data from.</param>
        /// <param name="expRespLength">Expected response length in bits. If zero, no specific RX length is expected.</param>
        /// <param name="stripHandle">Whether to strip the handle from the reponse or not (last two bytes).</param>
        /// <param name="isWrite">If true then the exchange will act like it is a write (air parameter and response timeout related behavior).</param>
        /// <param name="timeout">Timeout in milliseconds to wait the response.</param>
        /// <returns>The built custom exchange parameters.</returns>
        public NurApi.CustomExchangeParams BuildDefault(BitBuffer bb, ushort expRespLength, bool stripHandle, bool isWrite, uint timeout)
        {
            NurApi.CustomExchangeParams xch = new NurApi.CustomExchangeParams();
            int byteLength;

            byteLength = bb.actualLength / 8;

            if ((bb.actualLength % 8) != 0)
            {
                byteLength++;
            }

            /* Byte default all commands require handle. */
            xch.appendHandle = BoolToUint(true);
            /* May require write like response wait. */
            xch.asWrite = BoolToUint(isWrite);
            /* Copy built bytes. */
            xch.bitBuffer = new byte[NurApi.MAX_BITSTR_BITS / 8];
            System.Array.Copy(bb.bytes, xch.bitBuffer, byteLength);

            /* We want the RX CRC. */
            xch.noRxCRC = BoolToUint(false);
            /* By default all the commands are appended with CRC. */
            xch.noTxCRC = BoolToUint(false);
            /* Number of bits expected in the response including the 16-bit handle. */
            xch.rxLen = expRespLength;
            /* If length is set to 0 it means that the response length is unknown. */
            xch.rxLenUnknown = (uint)((expRespLength == 0) ? 1 : 0);
            /* The application may want to see the handle. */
            xch.rxStripHandle = BoolToUint(stripHandle);
            /* Timeout in milliseconds. */
            xch.rxTimeout = timeout;
            /* TX shall use normal CRC. */
            xch.txCRC5 = BoolToUint(false);
            /* Copy number of bits to transmit. */
            xch.txLen = (ushort)bb.actualLength;
            /* We want a response. */
            xch.txOnly = BoolToUint(false);
            /* There is no cover coding required. */
            xch.xorRN16 = BoolToUint(false);

            /* Copy laast for possible debugging. */
            CopyToLast(xch);

            return(xch);
        }
        /// <summary>
        /// Do simple exchange with the built command using password.
        /// </summary>
        /// <param name="password">Password for accessing the module.</param>
        /// <param name="secured"></param>
        /// <param name="epc"></param>
        /// <param name="xch"></param>
        /// <returns></returns>
        public NurApi.CustomExchangeResponse ExchangeCommand(uint password, bool secured, byte[] epc, NurApi.CustomExchangeParams xch)
        {
            NurApi.CustomExchangeResponse resp;

            resp = hApi.CustomExchangeByEPC(password, secured, epc, xch);

            return(resp);
        }
 /// <summary>
 /// Do simple exchange with the built command using no password.
 /// </summary>
 /// <param name="epc">Tag's EPC.</param>
 /// <param name="xch">Exchange parameters.</param>
 /// <returns>Return the custom exchange result if successful.</returns>
 /// <exception cref="NurApiException">Exception is thrown with communication error.</exception>
 public NurApi.CustomExchangeResponse ExchangeCommand(byte [] epc, NurApi.CustomExchangeParams xch)
 {
     return(ExchangeCommand(0, false, epc, xch));
 }