Exemplo n.º 1
0
        private void AsyncCompletionRoutine(QuickUsb.BulkStream bulkStream)
        {
            // Retrieve our user data
            GCHandle           hTag = GCHandle.FromIntPtr(bulkStream.Tag);
            AsyncBulkStreamTag tag  = (AsyncBulkStreamTag)hTag.Target;

            // Check for errors
            if (bulkStream.Error != 0)
            {
                //Console.WriteLine("--> CR: ERROR! Request #{0} failed with error {1} ({2} of {3} bytes)", tag.Id, BulkStream.Error, BulkStream.BytesTransferred, BulkStream.BytesRequested);
            }
            else
            {
                lock (testThread)
                {
                    streamBytes += (int)bulkStream.BytesTransferred;
                    totalBytes  += (int)bulkStream.BytesTransferred;
                }

                if (tag.shutdown == 0)
                {
                    if (!qusb.ReadBulkDataAsync(bulkStream.Buffer, bulkStream.BytesRequested, bulkStream, AsyncCompletionRoutine, bulkStream.Tag))
                    {
                    }
                }
                //Console.WriteLine("--> CR: Completed Request #{0} for {1} bytes", tag.Id, BulkStream.BytesTransferred);
            }
        }
Exemplo n.º 2
0
        ///////////////////////////////////////
        // The BulkStream completion routine //
        ///////////////////////////////////////
        static void CompletionRoutine(QuickUsb.BulkStream BulkStream)
        {
            // Check for general errors
            if (BulkStream == null)
            {
                Console.WriteLine("Bulkstream error: {0}", QuickUsb.GetLastError());
                return;
            }

            // Note: If we needed to reteive a reference to the managed QuickUsb object
            // associated with this BulkStream object because, for example, the 'qusb'
            // variable was inaccessible to this function, we would do the following:
            //QuickUsb qusb = QuickUsb.RetreiveManagedQuickUsbObject(BulkStream.Handle);

            // Check for errors
            if (BulkStream.Error != 0)
            {
                Console.WriteLine("--> ERROR! (" + DateTime.Now.ToString() + ") Request failed with error {0} ({1} of {2} bytes)", BulkStream.Error, BulkStream.BytesTransferred, BulkStream.BytesRequested);
                ++ErrorCount;
            }
            else
            {
                //Console.WriteLine("--> CR: Completed Request for {0} bytes", BulkStream.BytesTransferred);
            }

            // Keep a running count of bytes transferred
            TotalBytes += (int)BulkStream.BytesTransferred;
            ++TransCount;

            //
            // Now that this request has completed we may process the data
            //

            // Retreive a reference to the managed data buffer from the BulkStream.Buffer
            //byte[] buffer = QuickUsb.RetrieveManagedByteDataBuffer(BulkStream.Buffer);
            ushort[] buffer = QuickUsb.RetrieveManagedUShortDataBuffer(BulkStream.Buffer);
            if (buffer == null)
            {
                Console.WriteLine("ERROR!  Unable to retrieve managed data buffer");
            }

            //Thread.Sleep(50); // Simulate data processing (to force the use of more threads)

            // Keep track of the thread that is executing this completion routine
            int id = Thread.CurrentThread.ManagedThreadId;

            if (!ThreadIds.ContainsKey(id))
            {
                if (id == 0)
                {
                    return;
                }
                ThreadIds[id] = 1;
            }
            else
            {
                ++ThreadIds[id];
            }
        }
Exemplo n.º 3
0
 private void StreamCompletionRoutine(QuickUsb.BulkStream bulkStream)
 {
     lock (testThread)
     {
         streamBytes += (int)bulkStream.BytesTransferred;
         totalBytes  += (int)bulkStream.BytesTransferred;
     }
 }
Exemplo n.º 4
0
        ///////////////////////////////////////
        // The BulkStream completion routine //
        ///////////////////////////////////////
        void CompletionRoutine(QuickUsb.BulkStream BulkStream)
        {
            // Check for general errors
            if (BulkStream == null)
            {
                log.AppendLine(String.Format("{0}: ERROR! BulkStream error: {1}\n", DateTime.Now, QuickUsb.GetLastError()));
                return;
            }

            // Note: If we needed to reteive a reference to the managed QuickUsb object
            // associated with this BulkStream object because, for example, the 'qusb'
            // variable was inaccessible to this function, we would do the following:
            //QuickUsb qusb = QuickUsb.RetreiveManagedQuickUsbObject(BulkStream.Handle);

            // Check for errors
            if (BulkStream.Error != 0)
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: ERROR! Request failed with error {0} ({1} of {2} bytes)\n", DateTime.Now, BulkStream.BytesTransferred, BulkStream.BytesRequested));
                }
                ++ErrorCount;
            }
            else
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: Completed Request for {1} bytes", DateTime.Now, BulkStream.BytesTransferred));
                }
            }

            // Keep a running count of bytes transferred
            TotalKiloBytes += (double)(BulkStream.BytesTransferred) / 1024.0;
            InstKiloBytes  += (double)(BulkStream.BytesTransferred) / 1024.0;

            //
            // Now that this request has completed we may process the data
            //

            // Retreive a reference to the managed data buffer from the BulkStream.Buffer
            //byte[] buffer = QuickUsb.RetrieveManagedByteDataBuffer(BulkStream.Buffer);
            ushort[] buffer = QuickUsb.RetrieveManagedUShortDataBuffer(BulkStream.Buffer);
            if (buffer == null)
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: ERROR!  Unable to retrieve managed data buffer", DateTime.Now));
                }
            }

            // TODO: Process data
            //Thread.Sleep(50); // Simulate data processing (to force use of more threads)
        }
Exemplo n.º 5
0
        ///////////////////////////////////////
        // The BulkStream completion routine //
        ///////////////////////////////////////
        static void CompletionRoutine(QuickUsb.BulkStream BulkStream)
        {
            // Check for general errors
            if (BulkStream == null)
            {
                Console.WriteLine("BulkStream Error: {0}", QuickUsb.GetLastError());
                return;
            }

            // Note: If we needed to reteive a reference to the managed QuickUsb object
            // associated with this BulkStream object because, for example, the 'qusb'
            // variable was inaccessible to this function, we would do the following:
            //QuickUsb qusb = QuickUsb.RetreiveManagedQuickUsbObject(BulkStream.Handle);

            // Retrieve our user data (the 'tag' in the async function call)
            GCHandle      hTag = GCHandle.FromIntPtr(BulkStream.Tag);
            BulkStreamTag tag  = (BulkStreamTag)hTag.Target;

            // Check for errors
            if (BulkStream.Error != 0)
            {
                Console.WriteLine("--> CR: ERROR! Request #{0} failed with error {1} ({2} of {3} bytes)", tag.Id, BulkStream.Error, BulkStream.BytesTransferred, BulkStream.BytesRequested);
                ++ErrorCount;
            }
            else
            {
                //Console.WriteLine("--> CR: Completed Request #{0} for {1} bytes on thread {2}", tag.Id, BulkStream.BytesTransferred, Thread.CurrentThread.ManagedThreadId);

                //
                // Now that this request has completed we may process the data
                //

                // Retreive a reference to the managed data buffer from the BulkStream.Buffer
                //byte[] buffer = QuickUsb.RetrieveManagedByteDataBuffer(BulkStream.Buffer);
                ushort[] buffer = QuickUsb.RetrieveManagedUShortDataBuffer(BulkStream.Buffer);
                if (buffer == null)
                {
                    Console.WriteLine(String.Format("{0}: ERROR!  Unable to retrieve managed data buffer", DateTime.Now));
                }

                // TODO: Process data
                //Thread.Sleep(50); // Simulate data processing (to force use of more threads)
            }

            // Keep a running count of bytes transferred
            TotalBytes += (int)BulkStream.BytesTransferred;

            // Keep track of the number of oustanding requests
            --RefCount;

            hTag.Free();
        }
Exemplo n.º 6
0
        public mainForm()
        {
            InitializeComponent();

            // We must keep an active reference to the delegate so it is not garbage collected
            cbDelegate = new QuickUsb.BulkStreamCompletionRoutine(CompletionRoutine);

            for (int k = 0; k < NumRequests; ++k)
            {
                // Allocate the BulkStream objects
                bulkStream[k] = new QuickUsb.BulkStream();

                // Allocate the data objects
                BufferArray[k] = new ushort[BufferByteSize / 2];
            }
        }
Exemplo n.º 7
0
 void CompletionRoutine(QuickUsb.BulkStream bs)
 {
     // Check for errors
     if (bs.Error == 0)
     {
         /*// Calculate FPS
          * QueryPerformanceCounter(out tEnd);
          * tElapsed = (double)(tEnd - tStart) / (double)(freq);
          * tStart = tEnd;
          * toolStripStatusLabel1.Text = String.Format("FPS: {0:0.0}", 1.0 / tElapsed);
          *
          * // Blit data to screen
          * BitmapData bData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
          * MoveMemory(bData.Scan0, bs.Buffer, FrameByteSize);
          * image.UnlockBits(bData);
          * pbFrame.Invalidate();*/
     }
 }
Exemplo n.º 8
0
        ////////////////////////////
        // The completion routine //
        ////////////////////////////
        void CompletionRoutine(QuickUsb.BulkStream BulkStream)
        {
            // Check for general errors
            if (BulkStream == null)
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: ERROR!  BulkStream error: {1}\n", DateTime.Now, QuickUsb.GetLastError()));
                }
                return;
            }

            // Note: If we needed to reteive a reference to the managed QuickUsb object
            // associated with this BulkStream object because, for example, the 'qusb'
            // variable was inaccessible to this function, we would do the following:
            //QuickUsb qusb = QuickUsb.RetreiveManagedQuickUsbObject(BulkStream.Handle);

            // Check for errors
            if (BulkStream.Error != 0)
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: ERROR!  Request failed with error {1} ({2} of {3} bytes)\n", DateTime.Now, BulkStream.Error, BulkStream.BytesTransferred, BulkStream.BytesRequested));
                }
                ++ErrorCount;
            }
            else
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: Completed Request for {1} bytes", DateTime.Now, BulkStream.BytesTransferred));
                }
            }

            // Keep a running count of bytes transferred
            TotalKiloBytes += (double)(BulkStream.BytesTransferred) / 1024.0;
            InstKiloBytes  += (double)(BulkStream.BytesTransferred) / 1024.0;

            // Keep track of outstanding requests and the nuext request that will complete
            ++nextRequestToProcess;
            --numPendingRequests;

            // Time data rates
            QueryPerformanceCounter(out tEnd);
            double tElapsed     = (double)(tEnd - tStart) / (double)(freq);
            double tElapsedInst = (double)(tEnd - tLast) / (double)(freq);

            overallRate   = (TotalKiloBytes / 1024.0) / tElapsed;
            instRate      = (numPendingRequests == 0) ? 0 : ((InstKiloBytes / 1024.0) / tElapsedInst);
            InstKiloBytes = 0;
            tLast         = tEnd;

            //
            // Now that this request has completed we may process the data
            //

            // Retreive a reference to the managed data buffer from the BulkStream.Buffer
            //byte[] buffer = QuickUsb.RetrieveManagedByteDataBuffer(BulkStream.Buffer);
            ushort[] buffer = QuickUsb.RetrieveManagedUShortDataBuffer(BulkStream.Buffer);
            if (buffer == null)
            {
                lock (log)
                {
                    log.AppendLine(String.Format("{0}: ERROR!  Unable to retrieve managed data buffer", DateTime.Now));
                }
            }

            // TODO: Process data
            //Thread.Sleep(50); // Simulate data processing (to force use of more threads)
        }
Exemplo n.º 9
0
        private void performTest()
        {
            // Open the module
            string[] modules = QuickUsb.FindModules();
            if (modules.Length == 0)
            {
                return;
            }
            qusb = new QuickUsb(modules[0]);
            qusb.Open(modules[0]);

            int  k, transBytes = 0;
            bool transOk;

            // Async Variables
            QuickUsb.BulkStream[] pBulkStream = null;
            ushort[][]            pDataBuffer = null;
            AsyncBulkStreamTag[]  asyncTag    = null;
            GCHandle[]            hAsyncTag   = null;

            // Legacy Async Variables
            int issuedTrans = 0, nextToComplete = 0;

            IntPtr[] bufferArray = null;
            byte[]   transId     = null;

            // Streaming Variables
            bool streaming = false;

            int dataSize   = 0;
            int numBuffers = 0;

            byte[] data = null;
            uint   size, bytesTransferred;

            QuickUsb.Error qusbError;

            // Start timing
            QueryPerformanceCounter(out startTime);

            // Perform the test
            do
            {
                // Check for modification to configuration parameters
                if (dataSize != testPackSize || numBuffers != (int)nNumBuffers.Value)
                {
                    dataSize   = testPackSize;
                    numBuffers = (int)nNumBuffers.Value;

                    if (rbSync.Checked)
                    {
                        data = new byte[dataSize];
                    }

                    if (rbAsync.Checked)
                    {
                        // Wait for all issued transactions to complete
                        while (issuedTrans > 0)
                        {
                            if (!qusb.BulkWait(pBulkStream[nextToComplete], false))
                            {
                            }
                            else
                            {
                                transOk = true;
                                --issuedTrans;
                                if (++nextToComplete == pBulkStream.Length)
                                {
                                    nextToComplete = 0;
                                }
                            }
                        }

                        // Free memories
                        if (hAsyncTag != null)
                        {
                            for (k = 0; k < hAsyncTag.Length; ++k)
                            {
                                hAsyncTag[k].Free();
                            }
                        }

                        // Allocate memories
                        pDataBuffer = new ushort[numBuffers][];
                        pBulkStream = new QuickUsb.BulkStream[numBuffers];
                        asyncTag    = new AsyncBulkStreamTag[numBuffers];
                        hAsyncTag   = new GCHandle[numBuffers];
                        for (k = 0; k < numBuffers; ++k)
                        {
                            pBulkStream[k]       = new QuickUsb.BulkStream();
                            pDataBuffer[k]       = new ushort[dataSize / 2];
                            asyncTag[k]          = new AsyncBulkStreamTag();
                            hAsyncTag[k]         = GCHandle.Alloc(asyncTag[k], GCHandleType.Pinned);
                            asyncTag[k].shutdown = 0;
                        }

                        // Reset varaibles
                        issuedTrans    = 0;
                        nextToComplete = 0;

                        // Issue new transactions
                        while (issuedTrans != numBuffers)
                        {
                            if (!qusb.ReadBulkDataAsync(pDataBuffer[nextToComplete], (uint)dataSize, pBulkStream[nextToComplete], AsyncCompletionRoutineDelegate, GCHandle.ToIntPtr(hAsyncTag[nextToComplete])))
                            {
                            }
                            else
                            {
                                ++issuedTrans;
                                if (++nextToComplete == numBuffers)
                                {
                                    nextToComplete = 0;
                                }
                            }
                        }
                    }

                    if (rbAsyncLegacy.Checked)
                    {
                        // Wait for all issued transactions to complete
                        while (issuedTrans > 0)
                        {
                            if (!qusb.AsyncWait(out bytesTransferred, transId[nextToComplete], false))
                            {
                            }
                            else
                            {
                                transOk = true;
                                --issuedTrans;
                                if (++nextToComplete == transId.Length)
                                {
                                    nextToComplete = 0;
                                }
                            }
                        }

                        // Free old memories
                        if (bufferArray != null)
                        {
                            for (k = 0; k < bufferArray.Length; ++k)
                            {
                                Marshal.FreeHGlobal(bufferArray[k]);
                            }
                        }

                        // Allocate memories
                        transId     = new byte[numBuffers];
                        bufferArray = new IntPtr[numBuffers];
                        for (k = 0; k < bufferArray.Length; ++k)
                        {
                            bufferArray[k] = Marshal.AllocHGlobal(dataSize);
                        }

                        // Reset varaibles
                        issuedTrans    = 0;
                        nextToComplete = 0;
                    }

                    if (rbStreaming.Checked)
                    {
                        // Stop the stream if one is running
                        if (streaming)
                        {
                            if (!qusb.StopStream(streamID, false))
                            {
                            }
                        }

                        // Restart the stream with new parameters
                        if (!qusb.ReadBulkDataStartStream(IntPtr.Zero, (uint)numBuffers, (uint)dataSize, StreamCompletionRoutineDelegate, IntPtr.Zero, out streamID, 4, 2))
                        {
                        }
                    }
                }

                // Perform the data R/W
                size    = (uint)dataSize;
                transOk = false;
                if (testRead)
                {
                    // Synchronous data read
                    if (rbSync.Checked)
                    {
                        if (!qusb.ReadData(data, ref size))
                        {
                            qusbError = qusb.LastError();
                            // Error
                            MessageBox.Show(String.Format("QuickUsbReadData() failed! - {0}", qusbError.ToString()), "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            quit       = true;
                            transBytes = 0;
                            size       = 0;
                        }
                        else if ((int)size != dataSize)
                        {
                            // Error
                            MessageBox.Show(String.Format("QuickUsbReadData() did not read correct amount of data ({0} of {1} bytes)!", size, dataSize), "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            quit       = true;
                            transBytes = 0;
                            size       = 0;
                        }
                        else
                        {
                            transOk     = true;
                            totalBytes += size;
                        }
                    }

                    // Asynchronous data read
                    else if (rbAsync.Checked)
                    {
                        transOk = true;
                        lock (testThread)
                        {
                            size        = (uint)streamBytes;
                            streamBytes = 0;
                        }
                    }

                    // Legacy asynchronous data read
                    else if (rbAsyncLegacy.Checked)
                    {
                        // First wait on the next transaction to complete
                        bytesTransferred = 0;
                        if (issuedTrans > 0)
                        {
                            if (!qusb.AsyncWait(out bytesTransferred, transId[nextToComplete], false))
                            {
                                qusbError = qusb.LastError();
                                // Error
                                MessageBox.Show("Error", "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                quit       = true;
                                transBytes = 0;
                                size       = 0;
                            }
                            else
                            {
                                transOk = true;
                                --issuedTrans;
                            }
                        }

                        // Issue new transactions
                        while (issuedTrans != numBuffers && !quit)
                        {
                            if (!qusb.ReadDataAsync(bufferArray[nextToComplete], ref size, out transId[nextToComplete]))
                            {
                                qusbError = qusb.LastError();
                                // Error
                                MessageBox.Show("Error", "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                quit       = true;
                                transBytes = 0;
                                size       = 0;
                            }
                            else
                            {
                                ++issuedTrans;
                                if (++nextToComplete == numBuffers)
                                {
                                    nextToComplete = 0;
                                }
                            }
                        }

                        if (transOk)
                        {
                            size = bytesTransferred;
                        }
                    }

                    // Streaming data read
                    else if (rbStreaming.Checked)
                    {
                        transOk = true;
                        lock (testThread)
                        {
                            size        = (uint)streamBytes;
                            streamBytes = 0;
                        }

                        // Nothing to do here
                        Thread.Sleep(250);
                    }
                }
                else if (rbSync.Checked)
                {
                    // Sychronous data write
                    if (!qusb.WriteData(data, size))
                    {
                        qusbError = qusb.LastError();
                        // Error
                        MessageBox.Show("QuickUsbWriteData() failed!", "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        quit       = true;
                        transBytes = 0;
                        size       = 0;
                    }
                    else
                    {
                        transOk     = true;
                        totalBytes += size;
                    }
                }

                if (transOk)
                {
                    transBytes += (int)size;
                }
            } while (!quit);

            if (rbAsync.Checked)
            {
                // Wait for all issued transactions to complete
                for (k = 0; k < pBulkStream.Length; ++k)
                {
                    asyncTag[k].shutdown = 1;
                }

                // Wait for pending transactions to complete
                qusb.BulkWait(null, false);

                /*while (issuedTrans > 0)
                 * {
                 *  if (!qusb.BulkWait(pBulkStream[nextToComplete], false))
                 *  {
                 *  }
                 *  else
                 *  {
                 *      transOk = true;
                 *      --issuedTrans;
                 *      if (++nextToComplete == numBuffers)
                 *      {
                 *          nextToComplete = 0;
                 *      }
                 *  }
                 * }*/

                // Free memory
                for (k = 0; k < pBulkStream.Length; ++k)
                {
                    hAsyncTag[k].Free();
                }
            }

            if (rbAsyncLegacy.Checked)
            {
                // Wait for all issued transactions to complete

                /*while (issuedTrans > 0)
                 * {
                 *  qusb.AsyncWait(out size, transId[nextToComplete], false);
                 *
                 *  --issuedTrans;
                 *  if (++nextToComplete == numBuffers)
                 *  {
                 *      nextToComplete = 0;
                 *  }
                 * }*/
            }

            if (rbStreaming.Checked)
            {
                // Stop the stream if one is running
                if (streaming)
                {
                    if (!qusb.StopStream(streamID, false))
                    {
                        qusbError = qusb.LastError();
                        // Error
                        MessageBox.Show("Error", "QuickUSB Throughput Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            qusb.Close();

            if (rbAsyncLegacy.Checked)
            {
                // Free memory
                for (k = 0; k < bufferArray.Length; ++k)
                {
                    Marshal.FreeHGlobal(bufferArray[k]);
                }
            }
        }
Exemplo n.º 10
0
        ///////////////////////////////////
        // QuickUSB Asynchronous Example //
        ///////////////////////////////////
        static void Main(string[] args)
        {
            // The number of data buffers to use.  An asynchronous data request is issued
            // for each data buffer, then the main thread waits for each request to complete
            // then issues a new asynchrnonous request using the same data buffer as the
            // completed request.  To maximize throughout, the number of buffers should be
            // at least equal to the number of threads (see the SETTING_THREADS setting).
            const int NumBuffers = 8;

            // The byte size of the buffers.  To maximize performance the buffer size should
            // be as large as possible and a power of 2.
            const int BufferByteSize = 512 * 1024; // 512 KB

            // The number of times to issue a request for each buffer.
            const int LOOPS = 50;

            // Variables
            int k, j, Id = 0;

            string[] nameList;
            bool     qResult;

            QuickUsb.Error qusbError;

            // Variables to track time and performace
            long   tStart, tEnd, freq;
            double tElapsed;

            // The data buffers, BulkStream objects, and tag objects.  Because these objects
            // are passed to and used by unmanaged code in QuickUSB land, they must be handled
            // with care.  They cannot be relocated by the garbage collector and must either
            // be pinned or allocated in unmanaged memory.
            ushort[][]            BufferArray = new ushort[NumBuffers][];
            QuickUsb.BulkStream[] BulkStream  = new QuickUsb.BulkStream[NumBuffers];
            BulkStreamTag[]       tag         = new BulkStreamTag[NumBuffers];

            // We must keep an active reference to the delegate so it is not garbage collected.
            // This reference must minimally stay alive until all pending asynchronous requests
            // have completed.
            QuickUsb.BulkStreamCompletionRoutine cbDelegate = new QuickUsb.BulkStreamCompletionRoutine(CompletionRoutine);

            // Query connected modules
            nameList = QuickUsb.FindModules();
            if (nameList.Length == 0)
            {
                Console.WriteLine("No modules found.");
                return;
            }

            // Open the first module
            qusb = new QuickUsb(nameList[0]);
            qusb.Open(nameList[0]);

            // Allocate buffers
            for (k = 0; k < NumBuffers; ++k)
            {
                // Allocate the data buffers in unmanaged memory
                BufferArray[k] = new ushort[BufferByteSize / 2];

                // Allocate the BulkStream objects
                BulkStream[k] = new QuickUsb.BulkStream();

                // Create the tag object as normal.  We will later get a pinned reference to this
                // object to use in issuing the request, that we will later be able to use to
                // reconstruct a reference to the managed tag object in the completion routine.
                // We can do this because the tag data is never accessed or modified in unmanaged
                // land and only ever used in managed land.
                tag[k] = new BulkStreamTag();
            }

            // Start throughput timer
            QueryPerformanceFrequency(out freq);
            QueryPerformanceCounter(out tStart);

            Console.WriteLine("Acquiring data...please wait");

            // Aquire
            for (j = 0; j < (LOOPS + 1); ++j)
            {
                for (k = 0; k < NumBuffers; ++k)
                {
                    // If this is not the first loop, wait for the last transaction to complete
                    if (j != 0)
                    {
                        // Wait for the transaction to complete.  Once this function returns successfully
                        // the completion routine has already been executed and the transaction is
                        // entirely complete.
                        qResult = qusb.BulkWait(BulkStream[k], false);
                        if (!qResult)
                        {
                            qusbError = qusb.LastError();
                            Console.WriteLine("Request failed (QuickUSB Error: {0})", qusb.LastError());
                        }
                        else
                        {
                            // Now that this request has completed we may process the data here or in the
                            // completion routine, though it is better to perform all processing in the
                            // completion routine as they can be multi-threaded, allowing the main thread
                            // to simply issue and re-issue data requests.
                        }
                    }

                    // If this is not the last loop, issue a new transaction
                    if (j != LOOPS)
                    {
                        //Console.WriteLine("Issuing Request #{0}", (Id + 1));

                        // Issue a new transaction
                        tag[k].Id = ++Id;
                        qResult   = qusb.ReadBulkDataAsync(
                            BufferArray[k],
                            BufferByteSize,
                            BulkStream[k],
                            cbDelegate,
                            GCHandle.ToIntPtr(GCHandle.Alloc(tag[k])));

                        if (!qResult)
                        {
                            Console.WriteLine(String.Format("QuickUSB Error: {0}", qusb.LastError()));
                            qusb.Close();
                            return;
                        }
                        else
                        {
                            ++RefCount;
                            ++TransCount;
                        }
                    }
                }
            }

            // Stop the throughput timer
            QueryPerformanceCounter(out tEnd);
            tElapsed = (double)(tEnd - tStart) / (double)(freq);
            Console.WriteLine();

            Console.WriteLine("Time elapsed: {0:0.000} s", tElapsed);
            Console.WriteLine("Total bytes transferred: {0:0.000} MB", ((float)TotalBytes / (1024.0 * 1024.0)));
            Console.WriteLine("Data rate: {0:0.000} MS/s", ((TotalBytes / (1024.0 * 1024.0)) / tElapsed));
            Console.WriteLine();

            // Close the module
            qResult = qusb.Close();
            if (!qResult)
            {
                Console.WriteLine("QuickUSB Error: {0}", qusb.LastError());
                return;
            }

            // Report any leaks or errors
            Console.WriteLine("{0} transaction(s) issued in total", TransCount);
            Console.WriteLine("{0} transaction(s) failed", ErrorCount);
            Console.WriteLine("{0} transaction(s) are still outstanding", RefCount);

            return;
        }
Exemplo n.º 11
0
 void cRoutine(QuickUsb.BulkStream bs)
 {
     Assert.AreEqual(bs.Error, QuickUsb.Error.NoError);
     Assert.AreEqual(bs.BytesRequested, bs.BytesTransferred);
 }