title: Extra Characters Reading from Serial Port Using libserial on Linux
body: <p>I have a very basic device with which I am trying to interact via a serial connection on Linux. I am on the steep part of the learning curve here, still, so please be gentle!</p>
  
  <p>Anyhow, I am able to control the device via Hyperterminal in Windows, or via cu, screen, or minicom in Linux. I am connected via a built-in serial port on a PC at 19200, 8N1. The interface for the device is very simple:</p>
  
  <ul>
  <li>Type "H", and the device echoes back "H". </li>
  <li>Type "V", and the device echoes back a string containing its software version, "ADD111C".</li>
  <li>Type "S", and the device returns "0", "1", or "?", depending on its printer status.</li>
  <li>Type "Q", and it returns a five-line response with details on the last transaction processed.</li>
  <li>Each response is followed by a new-line, perhaps a CR, too, I am not certain.</li>
  </ul>
  
  <p>There's more, but that's a good start. When I connect to the device using screen, it works fine:</p>
  
  <pre><code>root@dc5000:~# screen /dev/ttyS0 19200,cs8,-ixon,-ixoff
  
  V
  ADD111C
  </code></pre>
  
  <p>Once I had that working manually, I tried to write a simple program using C++ and libserial to interact with the device. It looks like this:</p>
  
  <pre><code>#include &lt;SerialStream.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  using namespace LibSerial;
  
  int main(){
  
      char next_char[100];
      int i;
  
      SerialStream my_serial_stream;
      my_serial_stream.Open("/dev/ttyS0") ;
      my_serial_stream.SetBaudRate( SerialStreamBuf::BAUD_19200 ) ;
      my_serial_stream.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
      my_serial_stream.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
      my_serial_stream.SetParity( SerialStreamBuf::PARITY_NONE ) ;
      my_serial_stream.SetNumOfStopBits(1) ;
      my_serial_stream.SetVTime(1);
      my_serial_stream.SetVMin(100);
  
      cout&lt;&lt;"Sending Command:\n";
      my_serial_stream &lt;&lt; "V";
  
      my_serial_stream.read(next_char,100);
      cout&lt;&lt;"Result: "&lt;&lt;next_char&lt;&lt;"\n";
  
      my_serial_stream.Close();
  
      return 0;
  }
  </code></pre>
  
  <p>This is successfully able to send the "V" to the serial port, but when I read it back, I get a number of non-printing characters back after the valid data:</p>
  
  <pre><code>root@dc5000:~# g++ -o serialtest serialtest.cpp -lserial
  root@dc5000:~# ./serialtest 
  Sending Command:
  Result: 
  V
  ADD111C
  ��se�Xw��AN��ƿ,�
  root@dc5000:~# 
  </code></pre>
  
  <p>What am I missing to only grab the response to my query? I'm guessing that I need to flush a buffer on the port or something, but I have reached the end of my limited knowledge here.</p>
  
  <p>Ideally I would like to just get the "ADD111C", but I don't want to paint myself into a corner by grabbing a specific length of data (in case it changes in the future), and the garbage at the end of the read does not always seem to be the same length, or the same data.</p>
  
  <p>Many thanks for taking a look,</p>
  
  <p>Tom</p>