title: Sending ASCII Control Characters to Serial Device 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>One of the functions involves sending data to an attached printer. You send a command to the device, which then relays the data you input to the printer attached to the device. The command looks like this:</p>
  
  <ul>
  <li>Send "EXEX*". The device echoes back "EXEX" (the '*' is not echoed yet)</li>
  <li>Send a single byte indicating the length of the data you will send, including a LF at the end.</li>
  <li>Send the data (the device will now echo back the *).</li>
  <li>Send "#". The device will now be ready for another command.</li>
  </ul>
  
  <p>I have a small C++ program to communicate with the device, and I can successfully send single characters and such, but when I try to send this command, I do not get the expected results. </p>
  
  <p>Using Hyperterminal in Windows, it is particularly easy, using alt-key combinations to send ASCII control codes. Just connect and:</p>
  
  <ul>
  <li>Type "EXEX*"</li>
  <li>Type Alt+010 to send a LF character, indicating that you are sending 10 bytes to the printer (nine characters and a LF).</li>
  <li>Type the data you wish to send: "123456789" (nine bytes in length).</li>
  <li>Type Alt+010 again to send a final LF character to the printer.</li>
  <li>Type "#" to finish.</li>
  </ul>
  
  <p>Here is what I cobbled together to try in C++:</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 buffer [50];
  int n;
  
  n=sprintf (buffer, "EXEX*%c123456789%c#",10,10);
  printf("Variable buffer was set to a %d character string: %s\n",n,buffer);
  
  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; buffer;
  //my_serial_stream &lt;&lt; printf("%s",buffer);
  //my_serial_stream &lt;&lt; "EXEX*\n123456789\n#";
  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>I also tried both of the commented out lines, and neither worked. The device does not receive the proper characters on the other end.'</p>
  
  <p>I'm certain that this is pretty basic, perhaps something is grabbing the control characters in the middle? If anyone has any ideas on a better way to do this, I would really appreciate it. Specifically, I might need to send a byte with a value anywhere between 1 and 40, depending on the length of the data I wish to send to the printer.</p>
  
  <p>My apologies for being unclear, please let me know if I need to break this down farther.</p>
  
  <p>Many thanks,</p>
  
  <p>Tom</p>