I’m looking for some help with an error I’m getting. I think it’s just a syntax sorta thing.
Here’s my python code:
from subprocess import call
ser = serial.Serial( “/dev/ttyACM0” , 9600 )
ard = ser.readline()
if(‘smile’ in ard):
call([“bin/video_player.py -s 80x32 -l bin/smile.mp4”])
elif(‘laugh’ in ard):
call([“bin/video_player.py -s 80x32 -l bin/laugh.mp4”])
elif(‘wink’ in ard):
call([“bin/video_player.py -s 80x32 -l bin/wink.mp4”])
print(‘Data could not be read’)
Yeah I saw that, but rushed through it. I’ll take a closer look. Thanks for helping the n00b.
Keep in mind I am not all that familiar with Python, but have been programming in multiple other languages since the 90’s. Meaning, I dont write code in Python, but can usually read through it ok.
Anyhow, according to that post this error is due to namespace resolution. Meaning . …
import serial should instead be from serial import serial
The second answer although seemingly rewarded with more votes is FUD, and would be what I’d consider very bad programming technique. Never name your project the same as an already existing module.
Nope, it's not namespacing. In python it's safe to just "import serial"
You know the problem is due the "call" function.
* except* ser.SerialTimeoutException:
print('Data could not be read')
print 'Error', sys.exc_info()
And see a better log message.
Don't forget to "import sys"
I belive the problem is the "video_player.py" or the ".mp4" files are not in /bin. Or you don't have permission in those files...
That exception class is an attribute of the serial module, not of the Serial object instance, "ser", that you have there.
So catch serial.SerialException.
I think you're importing and handling the exceptions fine. For importing, you won't be able to access all the other stuff in the serial module if you just import the Serial class. Also, polluting the global name space makes things confusing for those reading your code ("where did this come from?" rather than, "this is obviously from the serial module").
For the exception, there's no reason to catch all here. Catch what you expect and leave the regular traceback for the rest.
I got it working by adding shell=True to my call function:
call([“bin/video_player.py -s 80x32 -l bin/stufy_animation.mp4”], shell=True)