1
  • Microcontroller: dsPIC30F4013
  • Compiler: xc16

I'm trying to receive an array from UART, but I only get the first 5 bytes.

I know that the receiver buffer is 4 words deep, but I need to receive the other bytes too. Maybe using a circular buffer, but I don't know how to uses this.

Can anyone help me to get all the bytes in the array?

This is my current code:

void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) {
        IFS0bits.U1RXIF = 0; 

        int i = 0;
            while (U1STAbits.URXDA) {
                array[i] = U1RXREG;
                i++;
                if (i == 10) {
                    break;
                }
            }
    }

I'm sending each array position to a PC:

serial-monitor serial-monitor

Sorry for my bad English.

L_J
  • 2,351
  • 10
  • 23
  • 28

2 Answers2

0

After many attempts I had success.

I'm sharing the correct code for anyone who needs it.

int k;


void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) {
        IFS0bits.U1RXIF = 0;

        array[k++] = U1RXREG;


        if (k == 10) {
            k = 0;
        }

    }
  • You had to be sure that your first Byte of the received array is always in array[0]. Otherwise you lost sync if you lost one Byte. Better use a frame with a startbyte and a check and the end. – Mike Jul 09 '18 at 06:08
  • How can I do this? – BrunoPadilha Jul 09 '18 at 13:09
0

Example :

Byte 1: 'S' :Startbyte

Byte 2: arrayByte[0]

Byte 3: arrayByte[1]

.......

Byte 11: arrayByte[9]

Byte 12: checksumm (maybe the LSB of the sum of all send Bytes)

The receiver will only start the receiption if the received Byte is an 'S'.
After the complete receiption of all Bytes the receiver had to calculate the checksumm again to be sure there are no Bytes lost.

Mike
  • 4,041
  • 6
  • 20
  • 37