0

我正在开发一个项目,带有 FTDI D2XX 的树莓派 4,我已按照安装 Linux D2xx 驱动程序的说明进行操作。我用C++编写代码,成功初始化驱动,得到如下信息:

设备数量为 1 Dev N 0: Flags=0x3 Type=0x8 ID=0x4036014 LocId=0x118
SerialNumber=FT6MGS9D Description=UM232H_FIFO_SYNC
ftHandle=0x1cef410

我正在尝试在 FTDI 上写入 0X85,命令 FT_write 返回 FT_Ok。当我尝试使用 FT_GetStatus 获取 numBytesToRead 时,它不会返回 FT_Ok,但 FT_Read 在没有数据读取的情况下返回 FT_Ok,所以我得到:

FT_Read 正常,dataBuffer = 0
FT_Read 正常,numBytesToRead= 0 FT_Read 正常,BytesReceived = 0

这是我的代码,我将不胜感激任何想法和帮助:

#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();
}
4

0 回答 0