gpt4 book ai didi

c++ - 阻塞串口读取

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:38 27 4
gpt4 key购买 nike

我必须阅读我项目中的许多 (8) 个串行设备。它们是 Pantilt、Camera、GPS、Compass 等。它们都是 RS232 设备,但它们具有不同的命令结构和行为。例如 GPS 开始发送数据我一打开端口。 PanTilt 和 Camera 只有在我向它们发送特定命令时才会响应。

我使用以下环境

  • 操作系统:Ubuntu 11.10
  • 语言:C++
  • 框架:Qt 4.7

对于 PanTilt 和 Camera 我想开发这样的功能。

int SendCommand(string& command, string& response)
{
port.write(command, strlen(command));
while(1)
{
if(response contains '\n') break;
port.read(response) // Blocking Read
}
return num_of_bytes_read;
}

我想以这种方式实现,因为这个函数将用作更复杂算法的构建 block 像这样……

SendCoammd("GET PAN ANGLE", &angle);
if(angle > 60)
SendCommand("STOP PAN", &stopped?);
if(stopped? == true)
SendCommand("REVERSE PAN DIRECTION", &revesed?);
if(reversed? == true)
SendCommand("START PAN", &ok);

要执行这样的操作,我需要严格的同步行为。有人知道如何处理这个问题吗?

最佳答案

我发现本教程非常有趣且很有帮助。 http://www.webalice.it/fede.tft/serial_port/serial_port.html

它展示了如何使用 boost::asio 来执行同步和异步读/写。

谢谢你的帮助!

关于c++ - 阻塞串口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com