1

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

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

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

案例#2:进入微控制器循环以逐字节传输,While循环退出,RxMessage数组保存'A',0xFF,'B',0xFF,'C',0xFF,'D',0xFF ,'E',0xFF。而 fsuccess 返回为 FT_OK 和 TotalRxBytes=10

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

注意:在上述情况 1 和 2 中,微控制器循环需要 5 次迭代才能让Windows 应用程序循环退出,就像“?”一样 和 0xFF 在每个微控制器循环迭代中传输。而在案例 #3 中,微控制器循环需要 10 次迭代才能退出Windows 应用程序 For-loop,就好像 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);
    }
4

1 回答 1

1

仔细检查 FT_SetDataCharacteristics(*ppfthandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE) 是否正确。两个设备之间的任何不匹配都会导致问题。

于 2013-09-02T12:17:19.050 回答