示例#1
0
        private static void Main()
        {
            bool success;
            UsbK usb;
            WINUSB_PIPE_INFORMATION  pipeInfoRead;
            USB_INTERFACE_DESCRIPTOR interfaceDescriptorRead;

            WINUSB_PIPE_INFORMATION  pipeInfoWrite;
            USB_INTERFACE_DESCRIPTOR interfaceDescriptorWrite;

            // Find the IN endpoint and configure the device.
            Test.PipeId = Test.PipeId | AllKConstants.USB_ENDPOINT_DIRECTION_MASK;
            if (!Test.ConfigureDevice(out pipeInfoRead, out usb, out interfaceDescriptorRead))
            {
                return;
            }

            // Find the OUT endpoint.
            Test.PipeId = Test.PipeId & ~AllKConstants.USB_ENDPOINT_DIRECTION_MASK;
            if (!Test.FindPipeAndInterface(usb, out interfaceDescriptorWrite, out pipeInfoWrite))
            {
                return;
            }

            Debug.Assert(
                interfaceDescriptorRead.bInterfaceNumber == interfaceDescriptorWrite.bInterfaceNumber,
                "This example requires the IN and OUT endpoints have the same bInterfaceNumber.");

            Debug.Assert(
                pipeInfoRead.MaximumPacketSize > 0 &&
                pipeInfoRead.MaximumPacketSize == pipeInfoWrite.MaximumPacketSize,
                "This example requires the IN and OUT endpoints have the same MaximumPacketSize.");

            // We will keep the buffer >= 1024.
            // To satisfy StmK it must also be an interval of MaximumPacketSize
            if (Test.TransferBufferSize == -1)
            {
                Test.TransferBufferSize = pipeInfoWrite.MaximumPacketSize * ((1024 + pipeInfoWrite.MaximumPacketSize - 1) / pipeInfoWrite.MaximumPacketSize);
            }

#if BMFW
            // TODO FOR USER: Remove this block if not using benchmark firmware.
            // This configures devices running benchmark firmware for streaming DeviceToHost transfers.
            Console.WriteLine("Configuring for benchmark device..");
            BM_TEST_TYPE testType = BM_TEST_TYPE.LOOP;
            success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptorRead.bInterfaceNumber, ref testType);
            if (!success)
            {
                Console.WriteLine("Bench_Configure failed.");
            }
#endif
            if (!Test.ShowTestReady())
            {
                goto Done;
            }

            UsbStream stmRead = new UsbStream(usb,
                                              pipeInfoRead.PipeId,
                                              Test.TransferBufferSize,
                                              Test.MaxPendingTransfers,
                                              Test.MaxPendingIO,
                                              true,
                                              1);
            UsbStream stmWrite = new UsbStream(usb,
                                               pipeInfoWrite.PipeId,
                                               Test.TransferBufferSize,
                                               Test.MaxPendingTransfers,
                                               Test.MaxPendingIO,
                                               true,
                                               3000);

            StreamReader stmReader = new StreamReader(stmRead);
            StreamWriter stmWriter = new StreamWriter(stmWrite);

            stmRead.Start();
            stmWrite.Start();

            Char[] chTemp = new char[Test.TransferBufferSize];
            while (stmReader.Read(chTemp, 0, chTemp.Length) > 0)
            {
                Console.WriteLine("Flushing packets..");
            }
            string       sWrite       = String.Empty;
            string       sPartialRead = String.Empty;
            string       sRead;
            const string lineFormat = "Tx#{0:000000} This string is being looped through endpoints {1:X2}h and {2:X2}h";
            int          lineLength = String.Format(lineFormat, 0, 0, 0).Length;

            // This is a counter/timer used only for statistics gathering.
            Thread.Sleep(0);
            Test.Dcs.Start();

            for (int iTransfers = 0; iTransfers < Test.MaxTransfersTotal; iTransfers++)
            {
                sWrite = String.Format(lineFormat, iTransfers, pipeInfoWrite.PipeId, pipeInfoRead.PipeId);
                stmWriter.WriteLine(sWrite);
                Console.WriteLine(sWrite);

                if ((iTransfers & 8) > 0)
                {
                    stmWriter.Flush();
                }
                if (iTransfers <= 16 || (String.IsNullOrEmpty((sRead = stmReader.ReadLine()))))
                {
                    continue;
                }

                if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead))
                {
                    continue;
                }
                Console.WriteLine(sRead);
            }

            stmWriter.Flush();
            while (!(String.IsNullOrEmpty((sRead = stmReader.ReadLine()))))
            {
                if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead))
                {
                    continue;
                }
                Console.WriteLine(sRead);
            }

            Test.Dcs.Stop();
            TimeSpan ts = new TimeSpan(Test.Dcs.Ticks);
            Console.WriteLine("Elapsed Time:\n\t{0} mins\n\t{1} secs\n\t{2} msecs", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds);

            stmWriter.Dispose();
            stmReader.Dispose();

Done:
            usb.Free();
        }
示例#2
0
        private static void Main()
        {
            bool success;
            UsbK usb;
            WINUSB_PIPE_INFORMATION pipeInfoRead;
            USB_INTERFACE_DESCRIPTOR interfaceDescriptorRead;

            WINUSB_PIPE_INFORMATION pipeInfoWrite;
            USB_INTERFACE_DESCRIPTOR interfaceDescriptorWrite;

            // Find the IN endpoint and configure the device.
            Test.PipeId = Test.PipeId | AllKConstants.USB_ENDPOINT_DIRECTION_MASK;
            if (!Test.ConfigureDevice(out pipeInfoRead, out usb, out interfaceDescriptorRead)) return;

            // Find the OUT endpoint.
            Test.PipeId = Test.PipeId & ~AllKConstants.USB_ENDPOINT_DIRECTION_MASK;
            if (!Test.FindPipeAndInterface(usb, out interfaceDescriptorWrite, out pipeInfoWrite)) return;

            Debug.Assert(
                         interfaceDescriptorRead.bInterfaceNumber == interfaceDescriptorWrite.bInterfaceNumber,
                         "This example requires the IN and OUT endpoints have the same bInterfaceNumber.");

            Debug.Assert(
                         pipeInfoRead.MaximumPacketSize > 0 && 
                         pipeInfoRead.MaximumPacketSize == pipeInfoWrite.MaximumPacketSize,
                         "This example requires the IN and OUT endpoints have the same MaximumPacketSize.");

            // We will keep the buffer >= 1024.
            // To satisfy StmK it must also be an interval of MaximumPacketSize
            if (Test.TransferBufferSize == -1)
                Test.TransferBufferSize = pipeInfoWrite.MaximumPacketSize * ((1024 + pipeInfoWrite.MaximumPacketSize-1) / pipeInfoWrite.MaximumPacketSize);

#if BMFW
            // TODO FOR USER: Remove this block if not using benchmark firmware.
            // This configures devices running benchmark firmware for streaming DeviceToHost transfers.
            Console.WriteLine("Configuring for benchmark device..");
            BM_TEST_TYPE testType = BM_TEST_TYPE.LOOP;
            success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptorRead.bInterfaceNumber, ref testType);
            if (!success)
            {
                Console.WriteLine("Bench_Configure failed.");
            }
#endif
            if (!Test.ShowTestReady()) goto Done;

            UsbStream stmRead = new UsbStream(usb,
                                              pipeInfoRead.PipeId,
                                              Test.TransferBufferSize,
                                              Test.MaxPendingTransfers,
                                              Test.MaxPendingIO,
                                              true,
                                              1);
            UsbStream stmWrite = new UsbStream(usb,
                                               pipeInfoWrite.PipeId,
                                               Test.TransferBufferSize,
                                               Test.MaxPendingTransfers,
                                               Test.MaxPendingIO,
                                               true,
                                               3000);

            StreamReader stmReader = new StreamReader(stmRead);
            StreamWriter stmWriter = new StreamWriter(stmWrite);

            stmRead.Start();
            stmWrite.Start();

            Char[] chTemp = new char[Test.TransferBufferSize];
            while (stmReader.Read(chTemp, 0, chTemp.Length) > 0)
            {
                Console.WriteLine("Flushing packets..");
            }
            string sWrite = String.Empty;
            string sPartialRead = String.Empty;
            string sRead;
            const string lineFormat = "Tx#{0:000000} This string is being looped through endpoints {1:X2}h and {2:X2}h";
            int lineLength = String.Format(lineFormat, 0, 0, 0).Length;

            // This is a counter/timer used only for statistics gathering.
            Thread.Sleep(0);
            Test.Dcs.Start();

            for (int iTransfers = 0; iTransfers < Test.MaxTransfersTotal; iTransfers++)
            {
                sWrite = String.Format(lineFormat, iTransfers, pipeInfoWrite.PipeId, pipeInfoRead.PipeId);
                stmWriter.WriteLine(sWrite);
                Console.WriteLine(sWrite);

                if ((iTransfers & 8) > 0) stmWriter.Flush();
                if (iTransfers <= 16 || (String.IsNullOrEmpty((sRead = stmReader.ReadLine()))))
                    continue;

                if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead)) continue;
                Console.WriteLine(sRead);
            }

            stmWriter.Flush();
            while (!(String.IsNullOrEmpty((sRead = stmReader.ReadLine()))))
            {
                if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead)) continue;
                Console.WriteLine(sRead);
            }

            Test.Dcs.Stop();
            TimeSpan ts = new TimeSpan(Test.Dcs.Ticks);
            Console.WriteLine("Elapsed Time:\n\t{0} mins\n\t{1} secs\n\t{2} msecs", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds);

            stmWriter.Dispose();
            stmReader.Dispose();

            Done:
            usb.Free();
        }