我正在写一个程序来控制一个与卫星通信的Iridium调制解调器。在发送每个AT命令后,调制解调器将发送回复(取决于命令),以指示命令成功。
现在我已经实现了它,以便程序在每个命令传输到调制解调器之间只等待10秒,但这有点风险,因为它不允许在命令未成功解释的情况下进行错误处理。我知道如何读取串行输入的唯一方法是使用while(fgets( , ,)),所以我想知道如何让程序通过串行端口等待调制解调器的应答,并在发送下一个命令之前检查它是什么,而不是具有统一的延迟。
我使用的是linux操作系统。
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
usleep(10000000);
}发布于 2011-07-12 05:14:10
在与串行端口对应的文件描述符上使用select或poll,这将在描述符准备好读取时返回。
如下所示:
int fd = fileno(stdin); /* If the serial port is on stdin */
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
struct timeval timeout = { 10, 0 }; /* 10 seconds */
int ret = select(fd+1, &fds, NULL, NULL, &timeout);
/* ret == 0 means timeout, ret == 1 means descriptor is ready for reading,
ret == -1 means error (check errno) */发布于 2011-07-12 05:15:17
为了尽可能接近你的模型,你可以做几件事:
1)使用纯文件句柄(通过open() )。然后,您将使用read()和write()与串行端口进行通信。
2)使用上面的1可以让您使用select来查看是否有准备好写入或读取的内容。
这也允许你将与调制解调器的通信转移到另一个线程,如果你的程序有其他事情要做的话……
我最近做了一些非常类似的事情,只用了一个短波无线电调制解调器,并且使用了Boost ASIO库来进行串口通信。
如果这是一种选择,这可能会对你有所帮助。
https://stackoverflow.com/questions/6656579
复制相似问题