0

我正在开发一个项目,带有 FTDI D2XX 的树莓派 4,我已按照安装 Linux D2xx 驱动程序的说明进行操作。我在 pi 上使用以下行命令在 C++ 中运行我的代码:

须藤 g++ -o FTDIEX FTDI_Mgt.cpp -L。-lftd2xx -Wl,-rpath,/usr/local/lib -pthread

须藤 ./FTDIEX

我按照建议运行了 sudo rmmod ftdi_sio 和 sudo rmmod usbserial 命令以卸载默认内核驱动程序。

但是,当尝试调用 FT_Open 失败并出现代码错误 3。

一些建议是创建一个 udev 规则以将正确的 ttyUSB 符号链接到一个众所周知的位置。我尝试了下面的行,但没有找到错误命令

驱动程序=="ftdi_sio", ATTRS{serial}=="FT6MGS9D", SYMLINK+="/dev/ttyUSB2"

-bash: ATTRS{serial}==FT6MGS9D,: 找不到命令

我使用我的代码成功获得了以下关于我的设备的信息:

开发 0:
标志=0x3 类型=0x8 ID=0x4036014 LocId=0x106 序列号=FT6MGS9D 描述=UM232H_FIFO_SYNC
ftHandle=0x4d7410

这是我的代码,当我使用 ftStatus = FT_CyclePort(ftHandle) 而不是 FT_Open 它返回 FT_Ok 但我无法读取任何内容 FT_Read 返回 data = 0 并且 FT_GetStatus 失败

 #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_Open(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