0

I'm executing a python script from a php page like so: exec("python ./test.py");

This script runs fine if I don't open a serial port in it. If I do, however, (and this is the whole point of calling the python script in the first place), the script doesn't execute properly.

If I call a simple python script that prints a statement -

print "This works!"

Then I get the desired output in my php page. But, if I open a serial port, I no longer get the output of "This works!", and the serial data is not getting sent to the receiving device -

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
print "This works!"

Both scripts run fine from the command line.

Is this a php limitation? I have tried other methods of execution such as popen and system, but they didn't work for me either.

6
  • 3
    Running from the command line is done with YOUR permissions. Running it under Apache is doine with Apache's permissions. Check that apache can open ttyACM0. Commented Jul 11, 2012 at 2:52
  • Change exec() to system() to see the output. Commented Jul 11, 2012 at 2:55
  • @MarcB How would I go about doing that? Commented Jul 11, 2012 at 3:02
  • @RomanNewaza tried system() again, it didn't work. Commented Jul 11, 2012 at 3:04
  • run python -c'import sys; print >>sys.stderr, "stderr is visible"' Commented Jul 11, 2012 at 3:06

1 Answer 1

2

Perhaps you aren't getting complete error reporting from your Python execution. Try adding raise Exception('Boo!') as the first line of you Python program to find out if you are or not. If you don't get the exception and a traceback, then your program is probably failing on the serial.Serial line, but you aren't hearing about it.

Sign up to request clarification or add additional context in comments.

4 Comments

I get the exception when I run from command line, but nothing when run from php. Edit: That is with the exception before anything else.
If I add a print statement before the exception, I get the output of it. But it still doesn't output the exception
It sounds like you are getting stdout but not stderr. Either fix your process spawning so you do get stderr, or wrap your entire Python program so that error handling goes to stdout.
Alright, sent stderr to stdout and am now getting all the error messages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.