0

I am trying to read from the serial port using libserial. The code sets up the serial port communication with the sensor(arduino) and then asks for an input from the user for reading data from serial port for a fixed number of times.

The code compiles and runs. However it ignores the line to to take user input from std::cin and keeps running to completion. It doesn't pause to capture data using cin. Each time i ran, the value to be read from cin was set to some junk values like -344969024, -1750564672, 139065363 etc. The code and sample output is as below:

I am on ubuntu 14.04, gcc-4.8.4. This behavior has also been reported at Cannot read from stdin after initializing serial port

#include <SerialStream.h>
#include "SerialPort.h"
#include <iostream>
//#include <unistd.h>
//#include <cstdlib>

#include <fstream>
#include <string>

using namespace LibSerial;

int main(int argc,char * argv[])
{

    SerialPort serial_port ("/dev/ttyUSB0") ;
    serial_port.Open() ;
    serial_port.SetBaudRate( SerialPort::BAUD_115200 );
    serial_port.SetCharSize( SerialPort::CHAR_SIZE_8 );   

    std::ofstream out("output.txt");

    int i=0;

    for (i=0;i<4;i++)
    {
      std::cout << serial_port.ReadLine() ; // necessary to read headers from sensor connected to serial port
    }

    serial_port.WriteByte( 'S' ); // necessary to initiate the communication from sensor connected to serial port

    int num_readings;    
    std::cout <<"enter number of readings to take" << std::endl;
    std::cin>>num_readings;
    int j=0;
    while(j<num_readings)
    {
    std::cout << serial_port.ReadLine() ;
    j++;
    std::cout << "came inside loop"<< std::endl;
    }

     return 0;
}

Sample output:

came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
quat    -0.98   0.06    0.16    -0.10
came inside new loop
user27665
  • 673
  • 7
  • 27

0 Answers0