Search code examples
python-3.xshellcommand-line-interfaceos.systemcubesat-protocol

How to execute a shell program taking inputs with python?


First of all, I'm using Ubuntu 20.04 and Python 3.8. I would like to run a program that takes command line inputs. I managed to start the program from python with the os.system() command, but after starting the program it is impossible to send the inputs. The program in question is a product interface application that uses the CubeSat Space Protocol (CSP) as a language. However, the inputs used are encoded in a .c file with their corresponding .h header.

In the shell, it looks like this: starting the program

In python, it looks like this:

import os
os.chdir('/home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1')
os.system('./waf')
os.system('./build/csp-client -k/dev/ttyUSB1')
os.system('cmp ident') #cmp ident is typically the kind of command that does not work on python

The output is the same as in the shell but without the "cmp ident output", that is to say it's impossible for me to use the csp-client#

As you can probably see, I'm a real beginner trying to be as clear and precise as possible. I can of course try to give more information if needed. Thanks for your help !


Solution

  • I'll try and give you some hints to get you started - though bear in mind I do not know any of your tools, i.e. waf or csp-client, but hopefully that will not matter.

    I'll number my points so you can refer to the steps easily.


    Point 1

    If waf is a build system, I wouldn't keep running that every time you want to run your csp-client. Just use waf to rebuild when you have changed your code - that should save time.


    Point 2

    When you change directory to /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1 and then run ./build/csp-client you are effectively running:

    /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client -k/dev/ttyUSB1
    

    But that is rather annoying, so I would make a symbolic link to that that from /usr/local/bin so that you can run it just with:

    csp-client -k/dev/ttyUSB1
    

    So, I would make that symlink with:

    ln -s /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client  /usr/local/bin/csp-client
    

    You MAY need to put sudo at the start of that command. Once you have that, you should be able to just run:

    csp-client -k/dev/ttyUSB1
    

    Point 3

    Your Python code doesn't work because every os.system() starts a completely new shell, unrelated to the previous line or shell. And the shell that it starts then exits before your next os.system() command.

    As a result, the cmp ident command never goes to the csp-client. You really need to send the cmp ident command on the stdin or "standard input" of csp-client. You can do that in Python, it is described here, but it's not all that easy for a beginner.

    Instead of that, if you just have aa few limited commands you need to send, such as "take a picture", I would make and test complete bash scripts in the Terminal, till I got them right and then just call those from Python. So, I would make a bash script in your HOME directory called, say csp-snap and put something like this in it:

    #/bin/bash
    
    # Extend PATH so we can find "/usr/local/bin/csp-client"
    PATH=$PATH:/usr/local/bin
    
    {
       # Tell client to take picture
       echo "nanoncam snap" 
       # Exit csp-client
       echo exit
    } | csp-client -k/dev/ttyUSB1
    

    Now make that executable (only necessary once) with:

    chmod +x $HOME/csp-snap
    

    And then you can test it with:

    $HOME/csp-snap
    

    If that works, you can copy the script to /usr/local/bin with:

    cp $HOME/csp-snap /usr/local/bin
    

    You may need sudo at the start again.

    Then you should be able to take photos from anywhere just with:

    csp-snap
    

    Then your Python code becomes easy:

    os.system('/usr/local/bin/csp-snap')