Search code examples
serial-portmicrocontrolleravratmelstudio

serial port resend recived data back to pc


I'm working with atxmega avr, I want to send a pattern of bytes to the MC and when it's complete and valid the MC Reply the pattern back to the PC, the pattern sent is an array of 8 bytes, I tried to find-out the problem but not succeeded :(, I don't know what's the problem with my code. knowing that serial port is working fine. my code is here:

    while (1)
{

     if (sampling_flag==1)
      {
          sampling_flag=0;
          tx_buf2[i] =  usart_getchar(&USARTF0);
          i++;

                 if (tx_buf2[0]== 0x01 && i==7)
                  {
                      for (int j = 0; j < 7; j++)
                      {
                      usart_putchar(&USARTF0, tx_buf2[i]);
                      }   
                  }
             if(i==7)
             i=0;
            /*  
        if (!fifo_is_empty(&rx_fifo))
          {

                  fifo_pull_uint8(&rx_fifo, &byteToSend);
                  usart_putchar(&USARTF0, received_byte);
          }
          */
      } 
}//end of while(1)

Solution

  • Probably you have to use "j" in usart_putchar like below

     for (int j = 0; j < 7; j++)
     {
        usart_putchar(&USARTF0, tx_buf2[j]);
     } 
    

    Your current code may reply only tx_buf2[7].