title: how to read multiple lines from serial C, terminated by carriage return AND line feed?
body: <p>I am working on a project where I need to communicate from an AVR microcontroller to an rs232 device via UART and a TTL level converter. I am using a serial callback to get the characters from the serial port, and checking for a carriage return/new line, then also checking the most recent character in the buffer for carriage return/new line.  The data from the rs232 device is coming in the format of 26 lines seperated by carriage return AND line feed comprised of ASCII characters. The entire 26 lines is sent approximately every .7 seconds.  I am trying to fill a 2D array with the 26 lines.  However, I am now getting a what I should in the receive buffer.  Here is my code so far:</p>
  
  <pre><code>    volatile uint8_t rxrs_buffer[26][12] = {
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
          };
  
      static void serialrs_rx_cb(const struct usart_async_descriptor *const io_descriptor)
  
  {
  
      //counters
      uint8_t ch, count;
  
      //read a character
      count = io_read(&amp;SERIALRS232.io, &amp;ch, 1);
  
      //check if we are receiving
      if (serialrs_receiving == 0)
      {
          //check for new line or carriage return
          if (ch != 10 &amp;&amp; ch != 13)
          {
              if (rxrs_buffer[row-1][column-1] != 10 &amp;&amp; rxrs_buffer[row-1][column-1] != 13)
              {           //set receiving flag
              serialrs_receiving = 1;
  
              //reset byte counter
              serialrs_bytes_received_counter = 0;
  
              //start filling the rx buffer
              rxrs_buffer[row][column] = ch;
  
              //increment the byte counter and column
              serialrs_bytes_received_counter += count;
              column++;
              }
  
          }
      }
      else
      {
          //continue filling the buffer
          rxrs_buffer[row][column] = ch;
  
          //increment the byte counter and column
          serialrs_bytes_received_counter += count;
          column++;
  
          //check for new line or carriage return
          if (ch == 10 || ch == 13)
          {   
              if (rxrs_buffer[row-1][column-1] != 10 &amp;&amp; rxrs_buffer[row-1][column-1] != 13)
              {
              //continue filling the buffer
              rxrs_buffer[row][column] = ch;
  
              //increment the byte counter
              serialrs_bytes_received_counter += count;
  
              //increment the line counter and reset column
              row++;
              column = 0;
  
              if (row == 26)
                  {
                      //set the completion flag
                      serialrs_complete = 1;
  
                      //total bytes
                      totalrs_bytes = serialrs_bytes_received_counter - 2;
  
                      row = 0;
                  }   
              }
  
          }
  
          //check for buffer overflow
          if (serialrs_bytes_received_counter &gt;= SERIALRS_BUFFER_SIZE)
          {
              //reset buffer counter
              serialrs_bytes_received_counter = 0;
          }
      }
  }
  </code></pre>
  
  <p>The issue is my 2D buffer array is not filling up correctly.  I am getting rows that start with two line feeds, then some that work perfectly and end in a carriage return line feed, only to find the next row starts with two carriage returns or line feeds? Any suggestions at all would help </p>