我正在尝试使用libserial从串口读取。代码设置与传感器(arduino)的串行端口通信,然后要求用户输入以从串行端口读取数据固定次数。
代码编译并运行。但是它忽略了从std :: cin获取用户输入并保持运行完成的行。它不会暂停使用cin捕获数据。每次我跑,从cin读取的值被设置为一些垃圾值,如-344969024,-1750564672,139065363等。 代码和示例输出如下:
我在ubuntu 14.04,gcc-4.8.4。 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;
}
示例输出:
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