标签: libserial

使用C/C++和LibSerial在Ubuntu中对串口进行读写

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

目前,我能够通过串口写入和接收字符串,但我的代码不能很好地工作:特别是,我想控制读取功能,以便只有在有东西需要读取时才能读取并且当没有信息要读取时退出,以便发送另一个命令而不会使流程程序失败.

我想要做:

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

现在,我能够发送第一个命令并在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 …
Run Code Online (Sandbox Code Playgroud)

c++ linux ubuntu serial-port libserial

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×1

libserial ×1

linux ×1

serial-port ×1

ubuntu ×1