简体   繁体   中英

Executing a command shell from popen and set other command shell

I'm working in a project with a quadrotor and mavlink. I have successfully installed mavproxy in my Ubuntu PC and and ran it without problems from terminal. 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.

When you execute mavproxy.py the program in the terminal begin to send and receive several parameters. You can write in the terminal some parameter to access for any configuration. For example, the command help in the terminal:

$ 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++

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.

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.

I have been reading about the Popen function, but I haven't found the correct form to do this.

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 .

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.

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:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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