简体   繁体   English

从popen执行命令shell并设置其他命令shell

[英]Executing a command shell from popen and set other command shell

I'm working in a project with a quadrotor and mavlink. 我正在使用四旋翼和mavlink进行项目。 I have successfully installed mavproxy in my Ubuntu PC and and ran it without problems from terminal. 我已经在我的Ubuntu PC上成功安装了mavproxy,并且可以在终端上正常运行它。 When I run mavproxy.py from the terminal and connected a quadrotor with support for mavlink (APM autopilot), mavproxy detects the quadrotor and everything is ok. 当我从终端运行mavproxy.py并连接支持mavlink(APM自动驾驶仪)的四旋翼飞行器时,mavproxy会检测到四旋翼飞行器,一切正常。

When you execute mavproxy.py the program in the terminal begin to send and receive several parameters. 当您执行mavproxy.py时,终端中的程序将开始发送和接收多个参数。 You can write in the terminal some parameter to access for any configuration. 您可以在终端中编写一些参数以访问任何配置。 For example, the command help in the terminal: 例如,终端中的命令help

$ mavlink.py
.
.data beging
.
STABILIZE>  "when the program finish the configuration, allowed to you for doing an input any parameter, for example help"

STABILIZE>help
show all helps.

I have a code to execute mavlink.py from C++ 我有一个代码可以从C ++执行mavlink.py

include <iostream>
include <stdio.h>

using namespace std;

int main() {
FILE *in;
char buff[512];

if(!(in = popen("mavlink.py", "r"))){
    return 1;
}

while(fgets(buff, sizeof(buff), in)!=NULL){
    cout << buff;
}
pclose(in);

return 0;
}

When I run this C++ program the terminal shows the same things that would appear if I were running mavproxy.py from the terminal, but I don´t know how can I send a command such as help in the C++ code. 当我运行此C ++程序时,终端显示的内容与从终端运行mavproxy.py显示的内容相同,但是我不知道如何在C ++代码中发送诸如help的命令。

If you read the program, the while statement allows me to capture the parameters generated from the program mavproxy.py and cout in the terminal, but mavlink.py never ends until you write something in the terminal exit or press CTRL + C so the while loop never ends. 如果您阅读该程序,则while语句使我可以捕获在终端中从程序mavproxy.pycout生成的参数,但是mavlink.py永远不会结束,直到您在终端出口中写一些东西或按CTRL + C while循环永无止境。

I have been reading about the Popen function, but I haven't found the correct form to do this. 我一直在阅读有关Popen函数的信息,但是我没有找到正确的格式来执行此操作。

I know that I can use the mavlink.h library in my program and send parameters to the quadrotor, but don't want do this with mavlink.h . 我知道我可以在程序中使用mavlink.h库并将参数发送到Quadrotor,但是不想使用mavlink.h进行此mavlink.h

I am not sure I understand your question, but I think you want to send commands to mavlink.py as well as read its output. 我不确定我是否理解您的问题,但我认为您想将命令发送到mavlink.py并读取其输出。

If that is the case, you must change the open mode of popen() from " r " to " w " so you can write, then you can send commands to it like this: 如果是这种情况,则必须将popen()的打开模式从“ r ”更改为“ w ”,以便可以编写,然后可以向其发送命令,如下所示:

FILE *fp;
char *command="HELP";

if(!(fp = popen("mavlink.py", "w"))){
    return 1;

fwrite(command, sizeof(char), strlen(command), fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM