Interface with C

B

Ben Pearson

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!
 
E

Eric Lavigne

When this button is hit, it will send a code of 0 to the C
program.

./mcp | python gui.py

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
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?

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(inputfilename,'r')
outputfile = open(outputfilename,'w')
inputstream,outputstream = os.popen2("debug\\curve-fit")
inputstream.write(inputfile.read())
inputfile.close()
inputstream.close()
outputfile.write(outputstream.read())
outputstream.close()
outputfile.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,317
Members
48,003
Latest member
coldDuece

Latest Threads

Top