c++ - 使用 C/C++ 和 LibSerial 在 Ubuntu 中的串行端口上读取和写入

标签 c++ linux ubuntu serial-port libserial

我正在使用 LibSerial在 Ubuntu 上通过串口读写数据。

目前,我可以通过串行端口写入和接收字符串,但我的代码不能很好地工作:特别是,我想控制读取功能以便只读如果没有信息可读时有可读退出,以便发送另一个命令而不阻塞流程序。

我想做的事:

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

现在,我可以发送第一个命令并通过在 while 循环中使用 read 函数读取答案,但我无法执行任何其他操作。 我无法发送第二个命令,因为 while 循环永远不会退出,所以程序会继续读取。

你能帮帮我吗?

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

#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 ;
}

最佳答案

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

关于c++ - 使用 C/C++ 和 LibSerial 在 Ubuntu 中的串行端口上读取和写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9046649/

相关文章:

python - 使用 Boost.Python 将 numpy 数组传递给 C++

c++ - 启用 openmp 时出错 - "ld: library not found for -lgomp"和 Clang 错误

C++ vector 问题

Java - 分配的内存与高内存利用率(Elasticsearch)

c++ - AOSP : arm-linux-androideabi-gcc: command not found even though it is present in prebuilts folder

linux - 如何在 x86 汇编中编写我自己的 atoi 函数

python - 在 python 中在 Ubuntu localhost 上运行的调用进程

python - 反复安装 scrapy 和 lxml 失败

java - 在 Tomcat (JSP) 上为我的 webapp 使用一个类, "The import my_matrix cannot be resolved"

c++字符串,构建删除字符 'b'并将字符 'a'替换为两个 'd'的函数