title: How do I write commands to a ttyACM0 device and obtain the resultant information data using C/C++
body: <p>I am currently stuck with a task of retrieving data from a decoder that monitors 6 joint positions of a robot arm. This device has a USB interface that runs as a serial CDC device. An ASCII command set allows a host to configure each axis for resolution and set access method. The current position of each axis is output in engineering units or raw binary (converted
  to ASCII) at a user defined rate. The command protocol is similar to the USB serial protocol (Here's a link to the Decoder's technical manual: <a href="http://www.soc-robotics.com/pdfs/Quad6X%20Technical%20Manual.pdf" rel="nofollow">http://www.soc-robotics.com/pdfs/Quad6X%20Technical%20Manual.pdf</a>)</p>
  
  <p>Using Ubuntu, this device comes up as a 'ttyACM0' device when I type "dmesg" into the terminal. The furthest I've gotten is using "screen /dev/ttyACM0" and this allows me to input these serial commands and see the output information through the terminal, however, this doesn't accomplish the task I'm trying to achieve.</p>
  
  <p>I'm trying to write some C/C++ code that would enable a program to write these serial commands to the ttyACM0 device to retrieve the information data the device outputs as a result. This is code will be eventually used in ROS to allow for position feedback control in each of the robot arm joints using the obtained data. </p>
  
  <p>This is honestly the best I could come up in a feeble attempt to write in one of the commands to the ttyACM0 file with the information I've gathered:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      FILE *fp;
      fp = fopen("/dev/ttyACM0", "w");
  
      if(fp == NULL)
      {
          perror("File failed to open");
          exit(1);
      }
  
      fprintf(fp, "i");
  
  
      fclose(fp);
  
      return 0;
  }
  </code></pre>
  
  <p>Anybody know how to solve this problem? Is there a way to take the code that lets me do what I've achieved in the terminal but to suit my needs? I've hit so many dead ends in forums that only tell you how to do this through the terminal or how to do it for an Arduino and its compiler. I am at my wits end trying to figure this out, help is greatly appreciated.</p>