【发布时间】:2012-05-10 11:55:29
【问题描述】:
我必须在我的项目中读取许多 (8) 个串行设备。它们是 Pantilt、相机、GPS、指南针等。它们都是 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;
}
我想以这种方式实现,因为此函数将用作更复杂算法的构建块 像这样……
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);
要做这样的事情,我需要严格的同步行为。有人知道如何解决这个问题吗?
【问题讨论】:
-
(无关的挑剔注意:传递
string &不是一个好主意,可能你想要const string &或只是string;另外,string上的strlen没有意义,你应该使用它的size方法)
标签: c++ qt ubuntu serial-port