Home Forums TrueRNG Hardware random number generator Trouble with reading from c#

This topic contains 4 replies, has 2 voices, and was last updated by  Ubld.it Staff 9 years, 6 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #947

    VrIgHtEr
    Member

    I bought one of these TrueRNG devices and I am trying to get it to work well. I installed the driver using the supplied inf file on windows 8.1. Whenever I use the rngcapture tool that’s provided on your page it reads random data from the device correctly. However, when I try to access it from c# using a simple program such as the following, it doesn’t manage to read anything from the device. Sometimes it manages to read a couple of bytes before the program gets stuck again (because it stops receiving data). This is also happening to me in Java programs. Any idea what might be happening here?

    
    static void Main(string[] args)
            {
                using (var port = new SerialPort("COM5", 115200))
                {
                    port.Open();
                    while (true)
                    {
                        while (port.BytesToRead == 0) ;
                        Console.WriteLine(port.ReadByte());
                    }
                }
            }
    
    • This topic was modified 9 years, 7 months ago by  VrIgHtEr.
    #952

    Ubld.it Staff
    Moderator

    Since rngcapture is working fine, the only thing I can think of is DTR. The TrueRNG uses the DTR signal to control the flow of data. It’s always generating random numbers, but when DTR isn’t present it throws them away stopping the stream. In rngcapture we specifically control DTR. Just opening a serial port should behave the same way (without any manual control) but I’m at a loss why this program would stop receiving data but rngcapture works as expected.

    Maybe instead of going into a deadlock loop with “while (port.BytesToRead == 0); ” you need to take a more cpu yielding approach with a callback or something. Some quick googling shows you aren’t alone in this issue. See this post on stackoverflow. It looks like it just needs to be done differently. That method works fine for a simple c program (like rngcapture almost does essentially that).

    #953

    VrIgHtEr
    Member

    Thanks a lot for the reply. You hit the nail right on the head.

    It was the DTR that was the problem. In this case all I had to do was simply add the following line before opening the port:

    port.DtrEnable = true;

    That has solved my problem. Thank you

    #954

    VrIgHtEr
    Member

    I have also figured out why it used to receive some data and then stop. It seems that it’s the driver that takes care of the serial buffer, not the individual programs, so if DTR is enabled (for example by running rngcapture) and I open the serial port from c#, there is still some data in the buffer and the c# program receives that.

    I have confirmed this by first running my program with DTR on and then stopping it. Then if I run the program with DTR set to off, I receive some data and then the stream stops (data still in the serial buffer). If I run the program again after that with DTR still off, the program receives absolutely nothing (as the serial buffer had been emptied by the previous run of the program). I then turn DTR back on and I start getting data again.

    • This reply was modified 9 years, 7 months ago by  VrIgHtEr.
    #956

    Ubld.it Staff
    Moderator

    Great I’m glad it was just the DTR issue. And that makes sense for the residual, since the usb stack pulls the data from the TrueRNG the driver is just holding it until someone pulls it out. That’s just the design of USB.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.