Problem with read data from a FTDI device via an USB port
Unsolved
General and Desktop
-
wrote on 16 Oct 2020, 18:06 last edited by Ivan Perynets
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"); } } } } }
-
wrote on 24 May 2024, 09:24 last edited by
Did you find a solution?
-