static void usbDump(int numPackets)
    {
        // Setup variables
        byte[] packet      = new byte[1036];
        byte[] packetKBits = new byte[130];

        int packetnum = 0;
        int tries     = 10;

        BeagleUsb5000CaptureStatus captureStatus =
            BeagleUsb5000CaptureStatus.BG_USB5000_CAPTURE_STATUS_INACTIVE;
        uint pretrigAmt   = 0;
        uint pretrigTotal = 0;
        uint capAmt       = 0;
        uint capTotal     = 0;

        // Disable VBUS to device
        BeagleApi.bg_usb5000_target_power(
            beagle, BeagleUsbTargetPower.BG_USB5000_TARGET_POWER_OFF);

        // Start the capture
        if (BeagleApi.bg_enable(beagle, BeagleProtocol.BG_PROTOCOL_USB) !=
            (int)BeagleStatus.BG_OK)
        {
            Console.Write("error: could not enable USB capture; exiting...\n");
            Environment.Exit(1);
        }

        // Disable VBUS to device
        BeagleApi.bg_usb5000_target_power(
            beagle, BeagleUsbTargetPower.BG_USB5000_TARGET_POWER_HOST_SUPPLIED);

        // Wait for the analyzer to trigger for up to 10 seconds...
        while (tries != 0)
        {
            if (BeagleApi.bg_usb5000_usb3_capture_status(
                    beagle, 1000, ref captureStatus,
                    ref pretrigAmt, ref pretrigTotal,
                    ref capAmt, ref capTotal) !=
                (int)BeagleStatus.BG_OK)
            {
                Console.Write(
                    "error: could not query capture status; exiting...\n");
                Environment.Exit(1);
            }

            if (captureStatus <= BeagleUsb5000CaptureStatus.
                BG_USB5000_CAPTURE_STATUS_PRE_TRIGGER)
            {
                Console.Write("waiting for trigger...\n");
            }
            else
            {
                break;
            }

            tries--;
        }

        if (tries == 0)
        {
            Console.Write("did not trigger, make sure a host and a device " +
                          "is connected to the analyzer.\n");
            Environment.Exit(1);
        }

        Console.Write(
            "index,time(ns),source,event,status,data0 ... dataN(*)\n");
        Console.Out.Flush();

        // ...then start decoding packets
        while (packetnum < numPackets || (numPackets == 0))
        {
            uint  status               = 0;
            uint  events               = 0;
            ulong timeSop              = 0;
            ulong timeDuration         = 0;
            uint  timeDataOffset       = 0;
            BeagleUsb5000Source source =
                BeagleUsb5000Source.BG_USB5000_SOURCE_ASYNC;

            int length = BeagleApi.bg_usb5000_read(
                beagle,
                ref status, ref events, ref timeSop, ref timeDuration,
                ref timeDataOffset, ref source,
                1036, packet, 130, packetKBits);

            // Make sure capture is triggered.
            if (length == (int)BeagleStatus.BG_CAPTURE_NOT_TRIGGERED)
            {
                continue;
            }

            // Check for invalid packet or Beagle error
            if (length < 0)
            {
                Console.Write("error={0:d}\n", length);
                break;
            }

            // Exit if observed end of capture
            if (status == BeagleApi.BG_READ_USB_END_OF_CAPTURE)
            {
                Console.Write("\n");
                Console.Write("End of capture\n");
                break;
            }

            // Grab the next packet on a timeout.
            if (length == 0 &&
                status == BeagleApi.BG_READ_TIMEOUT &&
                events == 0)
            {
                continue;
            }

            // Print the packet details
            Console.Write("{0},", packetnum);
            Console.Write("{0},", timestampToNS(timeSop, samplerateKHz));
            printSource(source);
            Console.Write(",");
            printEvents(source, events);
            Console.Write(",");
            printStatus(source, status);
            Console.Write(",");
            printPacket(source, packet, packetKBits, length);
            Console.Write("\n");

            packetnum++;
        }

        // Stop the capture
        BeagleApi.bg_disable(beagle);
    }