Error FT_GetStatus can't read and get the numBytesToRead and BytesReceived

For issues with communication ports, protocols, etc.
Post Reply
anaouss1
Posts: 1
Joined: Aug 10, 2021 20:39

Error FT_GetStatus can't read and get the numBytesToRead and BytesReceived

Post by anaouss1 »

Hi, I'm working on a project, raspberry pi 4 with FTDI D2XX, I have followed directions given on installing the Linux D2xx driver. I wrote the code in C++, I successfully initialized the driver and got the following information:
Number of devices is 1 Dev N 0: Flags=0x3 Type=0x8 ID=0x4036014 LocId=0x118
SerialNumber=FT6MGS9D Description=UM232H_FIFO_SYNC
ftHandle=0x1cef410
I'm trying to write 0X85 on the FTDI the command FT_write returns FT_Ok. When I try to get the numBytesToRead using FT_GetStatus it does not return FT_Ok, but FT_Read returns FT_Ok with no data read, so I am getting:
FT_Read is OK,dataBuffer = 0
FT_Read is OK,numBytesToRead= 0 FT_Read is OK,BytesReceived = 0
here is my code, I will appreciate any idea and help:

Code: Select all

#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <functional>
#include <string>
#include <atomic>
#include <fstream>
#include <cstdio>

using namespace std;
#include "ftd2xx.h"
#include "FTDI_Mgt.h"

    FT_HANDLE ftHandle;
    FT_STATUS ftStatus;
    DWORD devcount;   
    FT_DEVICE_LIST_INFO_NODE *devicelist;
    char CurrentBuffer ;

    bool FTDI_Mgt::FTDI_DeviceInitOK = false;

    
    FTDI_Mgt::FTDI_Mgt()
    {
    }

    bool FTDI_Mgt::InitDevice()
    {
        //Get FTDI Dev number
            printf("Getting the FTDI Dev number ...\n");
            ftStatus =  FT_CreateDeviceInfoList(&devcount);   
            if (ftStatus == FT_OK) 
            {
                printf("  Number of devices is %d\n",devcount);
            }

        if (devcount > 0)
        {
           // allocate storage for list based on numDevs
           devicelist = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*devcount);
           // get the device information list
           ftStatus = FT_GetDeviceInfoList(devicelist,&devcount);
            for ( int i = 0; i < devcount; i++)
            {
                    ftStatus = FT_CyclePort(ftHandle);
                    if (ftStatus == FT_OK)
                    {   
                       printf("FT opened successfully \n");
                       //FT_BIT_MODE_RESET = 0x00
                       ftStatus = FT_SetBitMode(ftHandle, 0xFF, 0x00);    //Enables different chip modes
                       //wait 10ms, wait N_Milliseconds(10);
                       std::this_thread::sleep_for(std::chrono::milliseconds(10));

                       //SET SYNC MODE Mode, FT_BIT_MODE_SYNC_FIFO = 0x40
                       ftStatus = FT_SetBitMode(ftHandle, 0xFF, 0x40);    
                       if (ftStatus == FT_OK)
                        {
                           ftStatus = FT_SetLatencyTimer(ftHandle, 2);  //Set the latency timer value
                           ftStatus = FT_SetUSBParameters(ftHandle, 0x10000, 0); //Set the USB request transfer size
                           ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0x0000, 0x0000);  //sets the flow control for the device
                           ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);  //purges receive and transmit buffers in the dev
                           FTDI_DeviceInitOK = true;
                        }
                    }
                    else
                    {
                        FTDI_DeviceInitOK = false;
                    }
                    break;
            }
        }

        return FTDI_DeviceInitOK;
    }

    void FTDI_Mgt::Start()
    {
        DWORD k = 0;
        ftdiCmd[0] = 0x85 ;   
        FT_Write(ftHandle, ftdiCmd, sizeof(ftdiCmd), &k);     
        for(int i=0; i<30; i++){
              FTDI_Mgt::ReadData_Tick();
              i++;
              std::this_thread::sleep_for(std::chrono::milliseconds(50));
        }
}

    void FTDI_Mgt::Stop()
    {
        DWORD k = 0;
        ftdiCmd[0] = 0x00 ;   
        // waitN_Milliseconds(50);
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        FT_Write(ftHandle, ftdiCmd, sizeof(ftdiCmd), &k);
        //Close FTDI
        FT_Close(ftHandle);                                                                   
    }

     void FTDI_Mgt::ReadData_Tick()   
    { 
        do
        {
            ReadCurrentBuffer_Data(CurrentBuffer);
        } while (sizeof(CurrentBuffer) > 10000);

    }
                             //             Read a specified number of bytes from the driver receive buffer             //

    bool FTDI_Mgt::ReadCurrentBuffer_Data(char& dataBuffer)    
    {
        DWORD numBytesToRead = 0;      
        DWORD numBytesRead = 0;
        DWORD EventDWord;
        DWORD TxBytes;
DWORD BytesReceived;

        ftStatus = FT_GetStatus(ftHandle,&numBytesToRead,&TxBytes,&EventDWord);
        ftStatus = FT_Read(ftHandle,&dataBuffer,numBytesToRead,&BytesReceived);    
    }

int main(){

    FTDI_Mgt a;
    a.InitDevice();
    a.Start();
    a.Stop();
}
Post Reply