我的操作系统:Ubuntu 14.04
IDE:Ecplipse Mars
Libs:GCC,libserial
我使用libserial通过串行总线进行通信。
#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string.h>
/*****************************************************************************/
int main(int argc, char** argv)
{
using namespace LibSerial ;
SerialStream serial_port ;
serial_port.Open("/dev/ttyACM0") ;
if ( ! serial_port.good() )
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
<< "Error: Could not open serial port."
<< std::endl ;
exit(1) ;
}
serial_port.SetBaudRate( SerialStreamBuf::BAUD_115200 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the baud rate." << std::endl ;
exit(1) ;
}
serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the character size." << std::endl ;
exit(1) ;
}
serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not disable the parity." << std::endl ;
exit(1) ;
}
serial_port.SetNumOfStopBits( 1 ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not set the number of stop bits."
<< std::endl ;
exit(1) ;
}
serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
if ( ! serial_port.good() )
{
std::cerr << "Error: Could not use hardware flow control."
<< std::endl ;
exit(1) ;
}
while(run)
{
int inCount = serial_port.rdbuf()->in_avail();
if (inCount > 0)
{
char buffer[1024];
// here is the problem
int nbytes = serial_port.readsome(buffer, inCount);
if (!serial_port.good())
{
std::cerr << "Error: readsome";
}
serial_port.write(buffer, strlen(buffer));
std::cerr << "read: " << nbytes << "available: " << inCount << "\n";
}
}
serial_port.Close();
std::cerr << std::endl ;
return EXIT_SUCCESS ;
}
要点
readsome()
正确读取并填充缓冲区,但其返回值不是读取值的数量。它总是返回&#34; 6394433&#34;。
知道这里出了什么问题吗?