title: Linux usb serial line input may lose the trailing new line
body: <p>I am testing a serial communication protocol based on printable characters only.
  The setup has a pc connected to an arduino board by USB. The PC USB serial is operated in canonical mode, with no echo, no flow control, 9600 baud.
  Since a read timeout is requested, pselect is called before the serial read. The arduino board simply echoes back every received character without any processing. The PC OS is Linux Neon with kernel 5.13.0-40-generic.</p>
  <p><strong>When lines of a specific length are transmitted from the PC and echoed back by the arduino, they are received correctly except for the final new line that is missing.</strong>
  A further read, returns an empty line (the previously missing NL).
  Lines with different length are transmitted and received correctly, including the trailing NL.</p>
  <p>This behavior is fully repeatable and stable. The following code reproduce the problem for a line transmitted with a length of 65 characters (including NL) and received with a length of 64 (NL missing). Other line lengths work fine.</p>
  <p>Thanks for any hints.</p>
  <pre><code>/* remote serial loop test 20220626 */
  
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt; 
  #include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/select.h&gt;
  #include &lt;string.h&gt;
  #include &lt;termios.h&gt;
  #include &lt;time.h&gt;
  #include &lt;unistd.h&gt;
  
  
  #define TX_MINLEN 63
  #define TX_MAXLEN 66
  #define DATA_MAXLEN 128
  
  #define LINK_DEVICE &quot;/dev/ttyUSB0&quot;
  #define LINK_SPEED B9600
  #define RECEIVE_TIMEOUT 2000
  
  
  
  int main()
  {
      int wlen;
      int retval;
      int msglen;
  
      uint8_t tx_data[257] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
      for (int i=16; i &lt; 256; i++) tx_data[i] = tx_data[i &amp; 0xf];
      uint8_t rx_data[257];
  
    /* serial interface */
    char * device;
    int speed;
    int fd;
    fd_set fdset;
    struct timespec receive_timeout;
    struct timespec *p_receive_timeout = &amp;receive_timeout;
    struct termios tty;
  
    /* open serial device in blocking mode */
    fd = open(LINK_DEVICE, O_RDWR | O_NOCTTY);
    if (fd &lt; 0) {
      printf(&quot;Error opening %s: %s\n&quot;,LINK_DEVICE,strerror(errno));
      return -1;
    }
  
    /* prepare serial read by select to have read timeout */
    FD_ZERO(&amp;(fdset));
    FD_SET(fd,&amp;(fdset));
  
    if (RECEIVE_TIMEOUT &gt;= 0) {
      p_receive_timeout-&gt;tv_sec = RECEIVE_TIMEOUT / 1000;
      p_receive_timeout-&gt;tv_nsec = RECEIVE_TIMEOUT % 1000 * 1000000;
    }
    else
      p_receive_timeout = NULL;
  
    /* get termios structure */
    if (tcgetattr(fd, &amp;tty) &lt; 0) {
        printf(&quot;Error from tcgetattr: %s\n&quot;, strerror(errno));
        return -1;
    }
  
    /* set tx and rx baudrate */
    cfsetospeed(&amp;tty, (speed_t)LINK_SPEED);
    cfsetispeed(&amp;tty, (speed_t)LINK_SPEED);
  
    /* set no modem ctrl, 8 bit, no parity, 1 stop */
    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &amp;= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &amp;= ~PARENB;     /* no parity bit */
    tty.c_cflag &amp;= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &amp;= ~CRTSCTS;    /* no hardware flowcontrol */
  
    /* canonical mode: one line at a time (\n is line terminator) */
    tty.c_lflag |= ICANON | ISIG;
    tty.c_lflag &amp;= ~(ECHO | ECHOE | ECHONL | IEXTEN);
  
    /* input control */
    tty.c_iflag &amp;= ~IGNCR;  /* preserve carriage return */
    tty.c_iflag &amp;= ~INPCK;  /* no parity checking */
    tty.c_iflag &amp;= ~INLCR;  /* no NL to CR traslation */
    tty.c_iflag &amp;= ~ICRNL;  /* no CR to NL traslation */
    tty.c_iflag &amp;= ~IUCLC;  /* no upper to lower case mapping */
    tty.c_iflag &amp;= ~IMAXBEL;/* no ring bell at rx buffer full */
    tty.c_iflag &amp;= ~(IXON | IXOFF | IXANY);/* no SW flowcontrol */
  
    /* no output remapping, no char dependent delays */
    tty.c_oflag = 0;
  
    /* no additional EOL chars, confirm EOF to be 0x04 */
    tty.c_cc[VEOL] = 0x00;
    tty.c_cc[VEOL2] = 0x00;
    tty.c_cc[VEOF] = 0x04;
  
    /* set changed attributes really */
    if (tcsetattr(fd, TCSANOW, &amp;tty) != 0) {
        printf(&quot;Error from tcsetattr: %s\n&quot;, strerror(errno));
        return -1;
    }
  
    /* wait for serial link hardware to settle, required by arduino reset
     * triggered by serial control lines */
    sleep(2);
  
    /* empty serial buffers, both tx and rx */
    tcflush(fd,TCIOFLUSH);
  
  
    /* repeat transmit and receive, each time reducing data length by 1 char */
    for (int l=TX_MAXLEN; l &gt; TX_MINLEN - 1; l--) {
  
      /* prepare data: set EOL and null terminator for current length */
      tx_data[l] = '\n';
      tx_data[l+1] = 0;
  
      /* send data */
      int sent = write(fd,tx_data,l+1);
  
      /* receive data */
  
      /* wait for received data or for timeout */
      retval = pselect(fd+1,&amp;(fdset),NULL,NULL,p_receive_timeout,NULL);
  
      /* check for error or timeout */
      if (retval &lt; 0)
        printf(&quot;pselect error: %d - %s\n&quot;,retval,strerror(errno));
      else if (retval == 0)
        printf(&quot;serial read timeout\n&quot;);
  
      /* there is enough data for a non block read: do read */
      msglen = read(fd,&amp;rx_data,DATA_MAXLEN);
  
      /* check rx data length */  
      if (msglen != l+1)
        printf(&quot;******** RX ERROR: sent %d, received %d\n&quot;,l+1,msglen);
      else
        continue;
  
      /* check received data, including new line if present */
      for (int i=0; i &lt; msglen; i++) {
        if (tx_data[i] == rx_data[i])
          continue;
        else {
          printf(&quot;different rx data:|%s|\n&quot;,rx_data);
          break;
        }
      }
  
      /* clear RX buffer */
      for (int i=0; i &lt; msglen + 1; i++) rx_data[i] = 0;
  
    }
  }
  
  
  </code></pre>