0

I'm communicating my C# applications with dsPIC x16 microcontroller using UART. I want to send/receive fixed size frames and I tried to manage it in a following way:

if(readFrame)
    {   IEC0bits.U1RXIE=0; //turn off the U1RX interrupts
        readFrame = false;            
        while(indexer < 8 )
        {   
            while(!U1STAbits.URXDA);
                modbusBuffer[indexer]=U1RXREG;
                indexer++;
        }
        if(indexer == 8)
        {
            modbusRecvTask(modbusBuffer);
            indexer=0;
        }                      
        IEC0bits.U1RXIE=1;     //turn on U1RX interrupts       
    }

void _ISR_NAP _U1RXInterrupt()
{    
if(IFS0bits.U1RXIF)
{
    IFS0bits.U1RXIF = 0;    //set the interrupt flag to false
    if(U1STAbits.OERR==1)   //check overload error
    {
        U1STAbits.OERR=0;       //clear error flag
    }
    else
    {
        readFrame = true;        
    }
}    

}

The thing is that it works fine only for the first received frame. After that the program goes into the receiver interrupt again and sets the flag readFrame to true even though no bytes were send and is getting stuck in line:

while(!U1STAbits.URXDA);

I've read some advices to clear the read buffer of the UART in order to prevent the program to go into the ISR again but I couldn't find a way to do it.

Glamdring
  • 84
  • 1
  • 5
  • Read `U1RXREG` to clear the buffer. Do the read before the error check. The input queue may have more than one char per interrupt. Do all this `while(!U1STAbits.URXDA)` in the ISR and I'd move processing the message outside; – jolati Feb 06 '16 at 17:41
  • Thank you for your answer. Thanks to you I solved my problems. As you suggested I append bytes to the array in the ISR and analyze it in main() and everything works fine. :) No need to use while(!U1STAbits.URXDA). Cheers! – Glamdring Feb 07 '16 at 23:41

0 Answers0