By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 935 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Interface with C

P: n/a
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!

Sep 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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()

Sep 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.