0

I am attempting to collect data from a USB device using a DLL C# wrapper from FTDI (D3XX.NET). In particular I collect 65536 samples continuously. Each collection of 65536 samples uses the ReadPipe method and takes 8.5 ms. Once collected I immediately signal another thread using a WaitHandle (0.2uS) so it can process the data and then the collecting thread doCs the ReadPipe again. The thing is, the next ReadPipe must occur within 0.5ms otherwise data will be lost. This is because the device has an internal 4K buffer that takes 0.5ms to fill. Now what actually happens is that 2% of the time, garbage collection takes place, which takes on average 1.4ms. Therefore some data is lost. Here is the code:

        while (keepGoing)
        {
            ftStatus = d3xxDevice.ReadPipe(0x84, myBuffer, 65536, ref bytesTransferred); //

            _wait.Set();
        }       

And in the other thread, I have a _wait.WaitOne(). The code in this other thread will execute in less than 10 milliseconds so this is no issue. What I would like to do, and I have no idea how yet, is to have a C++ thread running in another DLL which can reference the ReadPipe from the FTDI DLL, and then when is finished reading the 65536 samples, do the _wait.Set() to allow the managed thread to process the results. This way all collection is via unmanaged code and will not be slowed by the garbage collection. So here is a summary:

1) Call a function in my own C++ DLL which will start a thread.

2) The C++ thread runs and does a ReadPipe of 65536 samples from the FTDI DLL.

3) When the sample collection is complete, this thread does the equivalent of a _wait.Set() to allow the processing thread to do its work.

4) This C++ thread immediately issues the ReadPipe again.

So can this be done? How would I call the FTDI ReadPipe method from my DLL? How would I do the _wait.Set()? Would I use a different mechanism to signal the C# thread to continue?

Thanks for any insight and help (I know no C++ yet). Tom

Tom
  • 527
  • 1
  • 8
  • 28
  • take a look at [this question](https://stackoverflow.com/questions/6005865/prevent-net-garbage-collection-for-short-period-of-time) – vasily.sib Oct 31 '18 at 03:16
  • I've already tried those recommendations . No help. It would have to be done in unmanaged code. – Tom Oct 31 '18 at 03:58
  • If you want interaction between your native code and the C# code, you can look into C++/CLI. Also, you don't have to share the mutex between the managed and native code. You can write a native method that wait on the (native) mutex, and call it from the managed code. – Kevin Gosse Oct 31 '18 at 08:05
  • Would this not be like polling though? – Tom Oct 31 '18 at 16:11
  • @Tom this is no different from your solution. Just that you would wait on a mutex in native code instead of doing so in managed code – Kevin Gosse Oct 31 '18 at 19:49
  • Actually it's the other way around. I would sit in the managed code to process the results. – Tom Nov 01 '18 at 20:17

0 Answers0