title: How to process data received from serial port?
body: <p>I'm using a <a href="http://www.ti.com/tool/ek-tm4c123gxl" rel="nofollow">Tiva C Series Launchpad (the new version of the stellaris launchpad)</a> to communicate with the flight simulator FlightGear running on my laptop. The idea is to output orientation data (pitch/roll angles and the like) from FlightGear to the Tiva Launchpad and process that data. I've managed to configure the two to talk to each other so I am able to receive the required data and print it to the console (I'm using code composer studio). The XML protocol file I wrote for FlightGear outputs the data in ASCII format every second. So for example, some sample data that I can print to console looks like the following:</p>
  
  <p>-5</p>
  
  <p>0</p>
  
  <p>That's one transmission. And it is exactly what I expected. I'm telling it to send the pitch angle (which is -5 in this case), then a newline (to separate it from the next variable), then the roll angle (which is 0 in this case) and then end one transmission by another newline. </p>
  
  <p>To print it to the console all I'm doing is the following</p>
  
  <pre><code>char data;
  
  while(UARTCharsAvail(UART0_BASE)) { //loop while there are chars
      data = UARTCharGetNonBlocking(UART0_BASE);
      fprintf(stderr, "%c", data);
  }
  </code></pre>
  
  <p>This prints perfectly fine. If I change %c to a %d I get 4553104810 (ASCII converted to decimal) which also makes sense.</p>
  
  <p>Now I want to able to process that data somehow. I want to be able to extract the pitch angle (-5 in this case) and the roll angle (0 in this case) as numbers, do a few simple calculations (multiply, add and the like) to generate the pulse widths for two servos and then feed those values to the servos. I was thinking of writing the incoming data to an array and then read the required parts of the array (if that makes sense) but I haven't got it working yet. </p>
  
  <p>Any help would be appreciated. </p>
  
  <p>Cheers</p>
  
  <p><strong><em>EDIT: (edited on 30/09/14)</em></strong></p>
  
  <p>Based on the feedback provided by Chris (many thanks again) I wrote the following code:</p>
  
  <pre><code>int main(void) {
  
  uint32_t load;
  char data[10];
  char buffer[10];
  int i;
  int pitchAng, rollAng;
  char *pitch;
  char *roll;
  
  fprintf(stderr, "Program Start\n");
  SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
  
  uart_setup();
  load = PWM_setup();
  uint32_t *point;
  point = &amp;load;
  
  while (1) {
  
      while (UARTCharsAvail(UART0_BASE)) { //loop while there are chars
  
          i = 0;
          while ((UARTCharGetNonBlocking(UART0_BASE)) != '\n') {
              while (i &lt; 8) {
                  data[i++] = UARTCharGetNonBlocking(UART0_BASE); // read data into an array
              }
          }
          data[i] = '\0'; // terminate array with null to make it into a string
  
          strcpy(buffer, data); // copy data into buffer to avoid corrupting original data
          fprintf(stderr, "data = %s\n", data);
  
          pitch = strtok(data, " "); // split data where there is a space, pitch angle is before space
          pitchAng = atoi(pitch); // convert pitch angle to a number
          fprintf(stderr, "P=%d\n", pitchAng);
  
          roll = strtok(NULL, "\n"); // split data where there's a new line to take out roll angle
          rollAng = atoi(roll); // convert roll angle to a number
  
          move_pitch_motor(pitchAng, point);
          move_roll_motor(rollAng, point);
      }
  }
  </code></pre>
  
  <p>The last two functions basically output PWM to the respective motors (based on the data received from flightgear).</p>
  
  <p>The above code outputs the following to the console:</p>
  
  <pre><code>data = -5 0
  ÿÿÿ
  P=-5
  </code></pre>
  
  <p>for one loop. This makes sense since I'm sending -5 0\n from FlightGear. And the P=-5 is also fine.</p>
  
  <p>But the strange thing is that if I delete the two fprintf statements (I have to delete them because they interfere with the timing of the servo pulses), every thing goes weird. I know it goes weird because I can see the pulses all out of sorts on the scope. Also, if I delete just the P=-5 fprintf statement, the output to the console is:</p>
  
  <pre><code>data = ÿÿÿÿÿÿÿÿ
  </code></pre>
  
  <p>which means that data is not being read correctly. </p>
  
  <p>Some help would be appreciated. </p>
  
  <p>Thank you</p>