首页 > 解决方案 > Most efficient way to transmit 32768 int16_t values over USB?

问题描述

I've got a project that produces 32768 int16_t values every 1.125 seconds. What's the most efficient way to "package" these for transmission over USB? Currently I'm pumping them out with commas between every value (and semicolons every 4096 values, with an "X" as the final character). It occurs to me that the host won't know that a given value is one or two bytes, so pre-allocating a buffer and waiting for it to fill won't work.

The USB device is programmed in C++ and the host is programmed in C#, if that helps. On the host end, I've got this function from a UWP sample:

private async Task ReadAsync(CancellationToken cancellationToken)
    {

        Task<UInt32> loadAsyncTask;

        uint ReadBufferLength = 98305;

        // Don't start any IO if we canceled the task
        lock (ReadCancelLock)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Cancellation Token will be used so we can stop the task operation explicitly
            // The completion function should still be called so that we can properly handle a canceled task
            DataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
            loadAsyncTask = DataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
        }

        UInt32 bytesRead = await loadAsyncTask;
        if (bytesRead > 0)
        {
            ReadBytesTextBlock.Text += DataReaderObject.ReadString(bytesRead);
            ReadBytesCounter += bytesRead;
            UpdateReadBytesCounterView();

        }
        rootPage.NotifyUser("Read completed - " + bytesRead.ToString() + " bytes were read", NotifyType.StatusMessage);
    }

Is there a method that allows me to read until a certain character is detected, like "X"? Ultimately these values need to be parsed into int16_t arrays so I can do some FFTs.

I admit that right now I'm really hacking this together and hoping it works, but I would love a reliable and robust way to do this properly.

标签: c#c++uwpusb

解决方案


推荐阅读