首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用C/C++和LibSerial实现Ubuntu串口的读写

用C/C++和LibSerial实现Ubuntu串口的读写
EN

Stack Overflow用户
提问于 2012-01-28 16:03:49
回答 2查看 32K关注 0票数 6

我使用Ubuntu上的LibSerial在串口上读写数据。

目前,我能够通过串口编写和接收字符串,但我的代码不能很好地工作:特别是,--我希望控制读取函数,以便只在没有信息可读取的情况下读取和退出,以便发送另一个命令,而不破坏流程程序。

我想做:

  • 写命令
  • 等待答案
  • 然后编写另一个命令
  • 等待答案

现在,我能够发送第一个命令,并在while循环中使用read函数读取答案,但是我不能做其他任何事情。我无法发送第二个命令,因为while循环从未退出,所以程序继续读取。

你能帮帮我吗?

这是我使用的代码:(读和写函数在代码的末尾)

代码语言:javascript
运行
复制
#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string>

int
main( int    argc,
       char** argv  )
{
     //
     // Open the serial port.
     //
     using namespace std;
     using namespace LibSerial ;
     SerialStream serial_port ;
     char c;
     serial_port.Open( "/dev/ttyACM0" ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                   << "Error: Could not open serial port."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Set the baud rate of the serial port.
     //
     serial_port.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the baud rate." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of data bits.
     //
     serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the character size." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Disable parity.
     //
     serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not disable the parity." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of stop bits.
     //
     serial_port.SetNumOfStopBits( 1 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the number of stop bits."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Turn off hardware flow control.
     //
     serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not use hardware flow control."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Do not skip whitespace characters while reading from the
     // serial port.
     //
     // serial_port.unsetf( std::ios_base::skipws ) ;
     //
     // Wait for some data to be available at the serial port.
     //
     //
     // Keep reading data from serial port and print it to the screen.
     //
  // Wait for some data to be available at the serial port.
     //
     while( serial_port.rdbuf()->in_avail() == 0 )
     {
         usleep(100) ;
     }


     char out_buf[] = "check";
     serial_port.write(out_buf, 5);  <-- FIRST COMMAND
     while( 1  )
     {
         char next_byte;
         serial_port.get(next_byte);  HERE I RECEIVE THE FIRST ANSWER
         std::cerr << next_byte;

     }
     std::cerr << std::endl ;
     return EXIT_SUCCESS ;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-28 17:52:59

我认为您只需要使用while( serial_port.rdbuf()->in_avail() > 0 )作为上一个while循环的条件。然后,它将读取所有可用数据(“应答”),然后您可以发送第二个命令。

票数 4
EN

Stack Overflow用户

发布于 2012-06-23 08:29:03

使用尝试和捕捉将是有帮助的。:)

尝试如下:一个全局指针SerialPort *pu已经声明并打开了端口。

代码语言:javascript
运行
复制
int rxstring(char *cstr, unsigned int len, bool print_str)

{

char temp=0;
int i=0;
while(temp!='\n')
{
    try
    {
        temp=pu->ReadByte(100);
    }
    catch(SerialPort::ReadTimeout &e)
    {
        //cout<<"Read Timeout"<<endl;
        return 1;
    }
    if((temp!='\n')&&(temp!=0)&&(temp!=' '))
    {
        cstr[i]=temp;
        ++i;
        //cout<<i++<<temp<<'x'<<endl;
    }
}
cstr[i]='\0';
if(print_str)
    puts(cstr);
return 0;
}

参考资料:port.html#a15

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9046649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档