Rich
Rich

Reputation: 43

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

PROBLEM: Micro-controller is transmitting 10 bytes(ASCII A,B,C,D,E,F,G,H,I,J) in for-loop under debugger control. Windows application (C++/CLI Code abstracted below) is supposed to receive these bytes.

Refer the two different FT_Read attempts, first in while-loop and second in for-loop

Case #1: Executing micro-controller-for-loop in one-go, the While loop exits with RxMessage array holding first byte correctly as 'A' and rest nine bytes as junk. where fsuccess returned as FT_OK and TotalRxBytes=10

Case #2: Stepping in micro-controller-for-loop to transmit byte by byte, the While loop exits with RxMessage array holding 'A',0xFF,'B',0xFF,'C',0xFF,'D',0xFF,'E',0xFF. while fsuccess returned as FT_OK and TotalRxBytes=10

Case #3: Stepping in micro-controller-for-loop to transmit byte by byte. Executing Windows-app For-loop in one go. The Windows-app For-loop exits with RxMessage holding all 10 bytes correctly as 'A','B','C','D','E','F','G','H','I','J'.

Note: In case 1 & 2 above, the micro-controller-for-loop takes 5 iterations for Windows-app For-loop to exit as if '?' and 0xFF transmitted in every micro-controller-for-loop iteration. Whereas in Case #3, micro-controller-for-loop takes exactly 10 iterations to Windows-app For-loop to exit, as if FT_Read + FT_Purge removes undesired 0xFF chunk

             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)

Code snippet from microcontroller below:

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

Upvotes: 1

Views: 1303

Answers (1)

John Sheridan
John Sheridan

Reputation: 446

Double check FT_SetDataCharacteristics(*ppfthandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE) is correct. Any mismatch between the two devices will result in problems.

Upvotes: 1

Related Questions