Interface with C

Eric Lavigne lavigne.eric at gmail.com
Wed Sep 14 14:52:29 EDT 2005


> 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()




More information about the Python-list mailing list