私のOS:Ubuntu 14.04
IDE: 日食火星
ライブラリ: 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()
正しく読み取り、バッファーをいっぱいにしますが、その戻り値は、読み取った値の数ほど期待されていません。常に「6394433」を返します。
ここで何がうまくいかないのですか?