0

I am trying to get the RSSI value from a Option modem installed as ttyHS4 (control) and ttyHS5 (data) on a Linux board. Expected result shows up on console but the fgets just does not capture any console output data.

if ((f=popen("echo -e \"AT+CSQ\r\n\">dev\ttyHS4","r"))==NULL){  
perror("popen");  
exit(1);  
}  

while (fgets(buff,sizeof(buff),f){  
printf(":%s:\n",buff);  
}  

I tested with "echo \"TEST\"" in the popen command and code above was able to print out the ":TEST:" string. With the ttyHS4 output, I only can get a few output on the console but the fgets + fprintf does not get any data to work on.

Please advise where I could have faulted.

4

2 回答 2

1

您正在调用popen()创建一个文件描述符,该文件描述符将这个命令的标准输出引导到您的主程序:

echo -e ... >/dev/ttyHS4

确实意识到此命令不会向该文件描述符发送任何内容,对吗?popen()打开一个外壳 - 通常/bin/sh-然后执行该命令。由于重定向到/dev/ttyHS4,该echo命令不会将其标准输出发送到与其父 shell 相同的文件描述符,这会导致没有任何内容被写入f- 即使这不是问题,echo也永远不会读回调制解调器对您的响应..

但是你为什么还要使用popen()and echo,而不是直接打开/dev/ttyHS4读/写并在结果文件描述符上使用read()and呢?write()

于 2012-12-30T12:14:10.870 回答
0

路径中有制表符是非常不寻常的。您可能打算这样做:

popen("echo \"AT+CSQ\">/dev/ttyHS4","r")
于 2012-12-30T08:30:20.683 回答