FT_Read失败,除非逐字节读取,其中每个FT_Read后面跟着FT_Purge

FT_Read fails, UNLESS reading byte-by-byte where every FT_Read is followed by FT_Purge

本文关键字:FT Read Purge 读取 失败 字节      更新时间:2023-10-16

问题:微控制器在调试器控制下为循环传输10个字节(ASCII A、B、C、D、E、F、G、H、I、J)。Windows应用程序(下面抽象的C++/CLI代码)应该接收这些字节。

参考两次不同的FT_Read尝试,第一次在while循环中,第二次在循环中

案例#1:一次性执行循环的微控制器,While循环退出时,RxMessage数组将第一个字节正确保存为"A",其余九个字节为垃圾。其中fsuccess返回为FT_OK,TotalRxBytes=10

情况#2:进入微控制器,使循环逐字节传输,While循环退出,RxMessage数组保持"A"、"0xFF"、"B"、0xFF、"C"、"0"、"D"、"x"、"E"answers"0"。而fsuccess返回为FT_OK和TotalRxBytes=10

案例#3:进入微控制器,循环逐字节传输。一次性执行Windows应用程序For循环。Windows应用程序For循环退出时,RxMessage将所有10个字节正确保存为"A"、"B"、"C"、"D"、"E"、"F"、"G"、"H"、"I"、"J"。

注意:在情况1&2,循环的微控制器需要5次迭代才能使Windows应用程序循环退出,就像"?"在每个微控制器中发送0xFF用于循环迭代。而在案例#3中,循环的微控制器正好需要10次迭代才能退出Windows应用程序循环,就好像FT_Read+FT_Purge删除了不需要的0xFF块

             private:
    /// Required designer variable.
        PVOID fth;
        BOOL fSuccess, fthsuccess;
        array<wchar_t> ^ TxMessage;
        array<unsigned char> ^ RxMessage;

    Form1(void) //Constructor
    {
        fthsuccess = false;
        InitializeComponent();
        TxMessage = gcnew array<wchar_t> (12);
        RxMessage = gcnew array<unsigned char> (12);
    }

    /*PS. All the FT_xxxx calls below are tested for fsuccess before proceeding ahead */
    FT_Open(0, ppfthandle);
    FT_SetBaudRate(*ppfthandle, 9600);
    unsigned char LatencyTimer;
    FT_SetLatencyTimer(*ppfthandle, 2);
    FT_GetLatencyTimer(*ppfthandle, &LatencyTimer);
    FT_SetDataCharacteristics(*ppfthandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
    FT_SetTimeouts(*ppfthandle, 10000/*read*/, 1000/*write*/);

    if(fthsuccess == true)
    {
        pin_ptr<FT_HANDLE> ppfthandle = &fth;
        pin_ptr<wchar_t> ppTx = &TxMessage[0];
        fSuccess = FT_Write(*ppfthandle,&ppTx[0],4,&dwhandled);
        /*in absence of Purge below, Tx chars echo as part of Rx chars
        ultimately workaround would be needed to avoid Purging of 
        real time RxData at runtime
        */
        FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
        pin_ptr<unsigned char> ppRx = &RxMessage[0];
        DWORD RxBytes,TotalRxBytes;
        RxBytes=TotalRxBytes=0;
        while(TotalRxBytes<10){
        FT_GetQueueStatus(*ppfthandle,&RxBytes);
        fSuccess = FT_Read(*ppfthandle,ppRx,RxBytes,&dwhandled);//reading 10 bytes in one go
        if(fSuccess != FT_OK){
            break;
        }
        TotalRxBytes += dwhandled;
        ppRx = &RxMessage[TotalRxBytes];
        }
        fSuccess = FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
        ppRx = &RxMessage[0];//reset ppRx and attempt read in for-loop, the same bytes from micro-controller
        for(int i=0;i<10;i++)//reading byte-by-byte in debug steps
        {
            fSuccess = FT_Read(*ppfthandle,&ppRx[i],1,&dwhandled);
            /*in absence of Purge below, alternate characters are read as 0xFF
            ultimately workaround would be needed to avoid Purging of 
            real time RxData at runtime
            */
            FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
        }
    }// if (!fthsuccess)

下面的微控制器代码片段

    uint8_t Series[10]={'A','B','C','D','E','F','G','H','I','J'};
    for(loopcount=0;loopcount<10;loopcount++)
    {
        UART1Send(Series+loopcount,1);
    }

双击FT_SetDataCharacteristics(*ppfthandle、FT_BITS_8、FT_STOP_BITS_1、FT_PARITY_NONE)是否正确。这两个设备之间的任何不匹配都会导致问题。