C++ Debian:管理ttyUSB*缓冲区
我正在Debian6(ARM)上使用一个(看起来)挑剔的USB网络设备(zigbee)。所有通信均通过“AT”命令集进行。我遇到了一些关于IO缓冲区的问题,希望有人能对如何最好地管理它有所帮助 问题:要协商USB设备和我正在使用的传感器之间的连接,我必须遵循一系列命令。通常有3-5种:一些我只需要知道成功/失败,而另一些我需要得到一个返回值并在以后使用它。这一切都很好,只是设备的输出通常是相同消息的重复序列的页面(通常是旧命令的返回值)。有时是以前的命令,有时是字母“A”(真的)的序列 这就是我认为IO流管理不当的原因 我如何尝试使用它: (打开端口-通常为/dev/ttyUSB1) (写入缓冲区)C++ Debian:管理ttyUSB*缓冲区,c++,serial-port,debian,C++,Serial Port,Debian,我正在Debian6(ARM)上使用一个(看起来)挑剔的USB网络设备(zigbee)。所有通信均通过“AT”命令集进行。我遇到了一些关于IO缓冲区的问题,希望有人能对如何最好地管理它有所帮助 问题:要协商USB设备和我正在使用的传感器之间的连接,我必须遵循一系列命令。通常有3-5种:一些我只需要知道成功/失败,而另一些我需要得到一个返回值并在以后使用它。这一切都很好,只是设备的输出通常是相同消息的重复序列的页面(通常是旧命令的返回值)。有时是以前的命令,有时是字母“A”(真的)的序列 这就是我
//清除所有未完成的IO
睡眠(2);
tcflush(fd,TCIOFLUSH);
int fw=写入(fd,cmd,strlen(cmd));
如果(fw==0)
{
perror(“fwrite err:”);
出口(0);
}
睡眠(2);
而(1)
{
n=读取(fd和in,1);
instr+=in;
std::size\u t found=仪器查找(“正常\n”);
如果(找到!=std::string::npos)
打破
静态常量boost::regex regx(“错误:/d{2}”);
boost::cmatch匹配;
if(boost::regex_search(instr.c_str(),match,regx))
std::cout串行驱动程序可能正在对命令进行一些处理-尝试使用cfmakeraw
来设置一些termios设置,除了您已有的设置之外。另外,确保options.c_cc[VMIN]=1
,这将确保一个字节一到达端口就可以得到它
fd = open( pPort, O_RDWR | O_APPEND );
if ( fd == -1 )
{
fprintf( stderr, "Unable to connect to port: %s", pPort );
perror( "err: " );
exit( 0 );
}
else fprintf( stderr, "Connected to %s\n", pPort );
// setup the buffering
struct termios options;
tcgetattr( fd, &options );
cfsetispeed( &options, B19200 );
cfsetospeed( &options, B19200 );
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~( IXON | IXOFF | IXANY );
tcsetattr( fd, TCSANOW, &options );
// clear out any outstanding IO
sleep( 2 );
tcflush( fd, TCIOFLUSH );
int fw = write( fd, cmd, strlen( cmd ) );
if( fw == 0 )
{
perror( "fwrite err: ");
exit( 0 );
}
sleep( 2 );
while( 1 )
{
n = read( fd, &in, 1 );
instr += in;
std::size_t found = instr.find( "OK\n" );
if( found != std::string::npos )
break;
static const boost::regex regx( "ERROR: /d{2}" );
boost::cmatch match;
if( boost::regex_search( instr.c_str(), match, regx ))
std::cout << "(5)" << instr << "\n";
}