title: Serial programming: measuring time between characters
body: <p>I am sending/receiving data over a serial line in Linux and I would like to find the delay between characters.</p>
  
  <p>Modbus uses a 3.5 character delay to detect message frame boundaries.  If there is more than a 1.5 character delay, the message frame is declared incomplete.</p>
  
  <p>I'm writing a quick program in C which is basically</p>
  
  <pre><code>fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
  // setup newtio
  ....
  tcsetattr(fd, TCSANOW, &amp;newtio);
  for(;;) {
      res = read(fs, buf, 1);
      if (res &gt; 0) {
          // store time in milliseconds?
          //do stuff
      }
  }
  </code></pre>
  
  <p>Is there some way of measuring the time here?  Or do I need to look at retrieving data from the serial line in a different way?</p>
  
  <p>I've also tried hooking into SIGIO to get a signal whenever there is data but I seem to get data 8 bytes at a time.</p>
  
  <p>(yes, I know there exist some modbus libraries but I want to use this in other applications)</p>