Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with read data from a FTDI device via an USB port
Forum Update on Monday, May 27th 2025

Problem with read data from a FTDI device via an USB port

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 611 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ivan Perynets
    wrote on 16 Oct 2020, 18:06 last edited by Ivan Perynets
    #1

    Hi, I have a sensor connected to USB port and I try to read data from it directly using ftd2xx library.
    For read data I use QtConcurrent::run function to create another thread for reading data and Windows API functions CreateEvent() and WaitForSingleObject(handle, INFINITE) .
    Everything looks fine: the port is opened and connected, but I still getting nothing from it because after line WaitForSingleObject(handle, INFINITE) nothing happen. I missing something, obvious.
    Could anyone advice me? Thank you.
    Here is my code:

    void MainWindow::on_BtnConnect_clicked()
    {
        FT_STATUS ftStatus;
        QString tdescription = ui->PortNameBox->currentText();
        char description[32];
        sprintf(description,"%s",(const char *)tdescription.toStdString().c_str()  );
        ui->consol->textCursor().insertText(QString("SerialNumber is: %1\n").arg(description));
        ftStatus = FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, &ftHandle);
        if (ftStatus == FT_OK)
        {
           // device is opened
            ui->consol->textCursor().insertText(QString("Device with description %1 connected!\n").arg(description));
            on_timer_to_get_data = true;
            DWORD nBytesWritten;
    
                if(FT_Write(ftHandle, (LPVOID) 0x1, 1, &nBytesWritten) != FT_OK)
                {
                    Sleep(10);
                    ui->consol->textCursor().insertText("Write OK!");
                }
                else return;
    
                FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);
                // write access code to FTDI
                if(FT_Write(ftHandle, (LPVOID) 0x3, 1, &nBytesWritten) != FT_OK)
                    ui->consol->textCursor().insertText("Write OK!");
                else return;
            QFuture<void> ReadThread = QtConcurrent::run(this, &MainWindow::FTxPortReadThread);
        }
        else
        {
            //Can not got FTDI HANDLE
            on_timer_to_get_data = false;
            ui->consol->textCursor().insertText(QString("Can't open the device with description: %1\n Error # %2\n").arg(description).arg(FT_W32_GetLastError(ftHandle)));
            return;
        }
    }
    void MainWindow::FTxPortReadThread()
    {
        unsigned char InBuf[FTxPORTMAXREADBUF];
    
        HANDLE hEvent;
        DWORD EventMask;
    
        hEvent = CreateEvent( NULL,
                              FALSE, // auto-reset event
                              FALSE, // non-signalled state
                              NULL);
    
        FT_SetEventNotification(ftHandle, FT_EVENT_RXCHAR, hEvent);  
        mStop = false;
    
        for(;;)
        {
            // Waiting data or going to stop
            if (mStop) {
                FT_SetEventNotification(ftHandle, 0, hEvent);
                return;
            }
    
            WaitForSingleObject(hEvent, INFINITE);
    
            for(;;)
            {
                if (mStop) {
                    FT_SetEventNotification(ftHandle, 0, hEvent);
                    return;
                }
    
               DWORD nBytesToRead;		
                DWORD nBytesReaded;		
    
                    DWORD nBytesToWrite;	// Not using
                    DWORD nEventMask;		// Not using
                    FT_GetStatus(ftHandle, &nBytesToRead, &nBytesToWrite, &nEventMask);
    
                    if(nBytesToRead == 0) break;
    
                    if(nBytesToRead > FTxPORTMAXREADBUF)
                        nBytesToRead = FTxPORTMAXREADBUF;
    
    
                // Reading data
                if(FT_Read(ftHandle, InBuf, nBytesToRead, &nBytesReaded) == FT_OK)
                {
                    
                    if(nBytesReaded != 0)
                    {
                        // get line status
                        DWORD dwLineAndModemStatus;
                        if(FT_GetModemStatus(ftHandle, &dwLineAndModemStatus) != FT_OK)
                            dwLineAndModemStatus = 0;
                        DataToBuffer(nBytesReaded, InBuf);
                        ui->consol->textCursor().insertText("Reading data from thread\n");
                    }
                }
            }
        }
    }
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      Andrew23
      wrote on 24 May 2024, 09:24 last edited by
      #2

      Did you find a solution?

      1 Reply Last reply
      0
      • A Andrew23 referenced this topic on 24 May 2024, 09:30

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved