Interface with C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Pearson

    #1

    Interface with C

    I have a program that is developed in C, that has a simple text
    interface. I would like to use the same program, but with a GUI. I
    would like to use Python to interface with it, so that Python will
    simply read and write the code that would be used from a normal user,
    but using a TK GUI. For example, in the C program, if you type 0, it
    will send a ping. I would like to build a program that will run the C
    program, simply inputing the values that the Text interface would
    use, but with the graphical interface. EI, the following should work.

    -----------
    | Ping |
    -----------

    When this button is hit, it will send a code of 0 to the C program.
    Interfacing directly would cause lots of problems, especially whereas
    we would like to continue the source of the 2 programs, because
    sometimes we have to remotely acess our system, and a text interface
    is much better for that than a GUI, but the GUI would be able to give
    more functionality. I would prefer a simple interface, perhaps
    something like this. BTW, gui.py is the python code, and mcp is the
    name of the program.

    ../mcp | python gui.py

    I've forgotten the correct naming to this, but, that's what I would
    want, or perhaps it would need to be reversed. Is there a way to do
    this with Python, to be able to read the output of the mcp program,
    and to send data to it, as if it were just a person at the computer
    converting? Thanks!



  • Eric Lavigne

    #2
    Re: Interface with C

    [color=blue]
    > When this button is hit, it will send a code of 0 to the C
    > program.
    >
    > ./mcp | python gui.py[/color]

    Your pipe is backwards. Try this:
    python gui.py | ./mcp

    When your gui.py notices that a button got hit, it can just print the
    number 0 (followed by a newline character) to standard output. This
    becomes the input to ./mcp, which responds by sending a ping.

    Of course, this only works if gui.py does not need to see the output of
    ../mcp
    [color=blue]
    > Is there a way to do this with Python, to be able
    > to read the output of the mcp program, and to
    > send data to it, as if it were just a person at the
    > computer converting?[/color]

    Ouch, so you need two way interaction. Are you sure about this? Will
    your gui program need to change its behavior based on the output of
    mcp? If so, then the simple interface offered by pipes is no longer an
    option. You will need to call mcp from within your script. To see how
    this sort of thing is done, study the following short script:

    # mimics the following shell command:
    # debug\curve-fit <input.txt >output.txt

    import os

    inputfilename = 'input.txt'
    outputfilename = 'output.txt'

    inputfile = open(inputfilen ame,'r')
    outputfile = open(outputfile name,'w')
    inputstream,out putstream = os.popen2("debu g\\curve-fit")
    inputstream.wri te(inputfile.re ad())
    inputfile.close ()
    inputstream.clo se()
    outputfile.writ e(outputstream. read())
    outputstream.cl ose()
    outputfile.clos e()

    Comment

    Working...