小编Wan*_*ist的帖子

如何在程序中间等待来自串行端口的输入

我正在编写一个程序来控制与卫星通信的铱星调制解调器。发送每个AT命令后,调制解调器会发回一个回复(取决于命令),以指示命令成功。

现在,我已经实现了它,以便程序在将每个命令传输到调制解调器之间仅等待10秒钟,但这有点冒险,因为在未成功解释命令的情况下,它不允许进行错误处理。我知道如何读取串行输入的唯一方法是使用while(fgets( , ,)),所以我想知道如何让程序等待调制解调器通过串行端口的答复,并在发送下一个命令而不是发送命令之前检查其内容。具有统一的延迟。

我正在使用Linux OS。

FILE *out = fopen(portName.c_str(), "w");//sets the serial port

for(int i =0; i<(sizeof(messageArray)/sizeof(messageArray[0])); i++)
{
  //creates a string with the AT command that writes to the module
  std::string line1("AT+SBDWT=");
  line1+=convertInt( messageArray[i].numChar);
  line1+=" ";
  line1+=convertInt(messageArray[i].packetNumber);
  line1+=" ";
  line1+=messageArray[i].data;
  line1+=std::string("\r\n");

  //creates a string with the AT command that initiates the SBD session
  std::string line2("AT+SBDI");
  line2+=std::string("\r\n");

  fputs(line1.c_str(), out); //sends to serial port
  usleep(10000000);     //Pauses  between the addition of each packet.

  fputs(line2.c_str(), out); //sends to serial port …
Run Code Online (Sandbox Code Playgroud)

c++ linux serial-port

4
推荐指数
1
解决办法
3125
查看次数

标签 统计

c++ ×1

linux ×1

serial-port ×1