【问题标题】:Debian: managing ttyUSB* buffersDebian:管理 ttyUSB* 缓冲区
【发布时间】:2013-11-15 16:59:29
【问题描述】:

我正在 Debian 6(ARM) 上使用(看似)挑剔的 USB 网络设备 (zigbee)。通信都是通过“AT”命令集进行的。我遇到了一些与 IO 缓冲区有关的问题,希望有人能阐明如何最好地管理这个问题。

问题:要协商 USB 设备和我正在使用的传感器之间的连接,我必须遵循一系列命令。通常有 3-5 个:有些我只需要知道成功/失败,而另一些则需要我得到一个返回值并在以后使用它。这一切都很好,除了设备的输出通常是相同消息的重复序列页面(通常是旧命令的返回值)。有时是前面的命令,有时是字母“A”的序列(真的)。

这就是我认为我对 IO 流管理不善的原因。

我是如何尝试使用它的:

(打开端口 - 通常是 /dev/ttyUSB1 )

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";

}

最初我希望使用“从缓冲区读取直到它为空然后写入”策略,但缓冲区永远不会为空。或者至少它在读取时总是返回字符。

【问题讨论】:

    标签: c++ serial-port debian


    【解决方案1】:

    串行驱动程序可能正在对命令进行一些处理 - 尝试使用 cfmakeraw 来设置一些 termios 设置以及您所拥有的设置。另外,请确保options.c_cc[VMIN] = 1,这将确保您在字节到达端口后立即获得。

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 2022-01-03
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2012-05-12
      相关资源
      最近更新 更多