FTDI Chip-C# Programmierung - Wie zu Lesen-Puffer?

Derzeit arbeite ich an einem Projekt mit einem FTDI chip.

Ich bin Programmierung in C# und habe versucht eine der Beispiele (die 3. mit den Daten loopback) auf der FTDI-website.

Der code funktioniert, ich in der Lage bin, zu schreiben, "Hallo Welt" und Lesen Sie es zurück. In diesem Fall wissen wir, wie viel Daten, die wir erwarten, um wieder aus dem Puffer :

//Perform loop back - make sure loop back connector is fitted to the device
//Write string data to the device
   string dataToWrite = "Hello world!";
   UInt32 numBytesWritten = 0;

//Note that the Write method is overloaded, so can write string or byte array data
   ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);

   if (ftStatus != FTDI.FT_STATUS.FT_OK)
   {
       //Wait for a key press
       Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")");
       Console.ReadKey();
       return;
   }

   //Check the amount of data available to read
   //In this case we know how much data we are expecting, 
   //so wait until we have all of the bytes we have sent.
   UInt32 numBytesAvailable = 0;
   do
   {
       ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);

       if (ftStatus != FTDI.FT_STATUS.FT_OK)
       {
           //Wait for a key press
           Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
           Console.ReadKey();
           return;
       }
       Thread.Sleep(10);
    } while (numBytesAvailable < dataToWrite.Length);

    //Now that we have the amount of data we want available, read it
    string readData;
    UInt32 numBytesRead = 0;

    //Note that the Read method is overloaded, so can read string or byte array data
    ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);

    if (ftStatus != FTDI.FT_STATUS.FT_OK)
    {
        //Wait for a key press
        Console.WriteLine("Failed to read data (error " + ftStatus.ToString() + ")");
        Console.ReadKey();
        return;
    }
    Console.WriteLine(readData);

Aber was ist, wenn ich will, um alle Daten Lesen, und ich weiß nicht, wie viel Daten kann ich erwarten, um wieder aus dem Puffer ?

Dank

  • müssen Sie die Prüfung für die Daten der Zählung Teil in einen regelmäßigen timer-Schleife und ein Ereignis auslösen, wenn es etwas zu Lesen.
InformationsquelleAutor Whatever | 2013-06-26
Schreibe einen Kommentar