NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Python script collapsing after a while

March 10, 2011
by lcruz007
lcruz007's Avatar

Hi,

My python program serves as the interface between the microcontroller and the PC using the serial port. After a while, however, my program closes by itself and I receive this error:

luis@luis-laptop:~/eyeboard$ python eog.py
Traceback (most recent call last):
  File "eog.py", line 6, in <module>
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
  File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 175, in open
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 24] Too many open files: '/dev/ttyUSB0'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/apport_python_hook.py", line 52, in apport_excepthook
ImportError: No module named tempfile

Original exception was:
Traceback (most recent call last):
  File "eog.py", line 6, in <module>
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
  File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 175, in open
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 24] Too many open files: '/dev/ttyUSB0'

Any ideas?

Thank you.

March 10, 2011
by Ralphxyz
Ralphxyz's Avatar

Chances are your Python code is opening (calling) a program or file and you are looping around and

calling it again and again.

You need to close the program with each call or change your program to just open the program once.

This is called "...". Darn I cannot remember what it is called (the AGE virus strikes again) but it is a common error.

Especially when you a calling (opening) a database which probable is not your case but just opening

a text file or lookup table has to be specifically closed each time.

Ralph

March 10, 2011
by lcruz007
lcruz007's Avatar

Thanks, That makes sense since I'm opening a text file. I am making sure to close the file at each call though, but going to look at the code more closely and see if I find something (I'm not an expert with python! =p).

See if you can see something wrong with it though!

#Python serial port program
#Author: Luis Cruz

    while True:
     import serial 
     serial = serial.Serial("/dev/ttyUSB0", 115200, timeout=1)
    #uses the serial port
     data = serial.read()

    #right
     if data == "0": 
      f = open("file.txt", "w")
      f.write("0")
      f.close()

    #center
     if data == "1": 
      f = open("file.txt", "w")
      f.write("1")
      f.close()

    #left
     if data == "2": 
      f = open("file.txt", "w")
      f.write("2")
      f.close()
March 10, 2011
by bretm
bretm's Avatar

It's the serial port that you're opening over and over again without closing it. Move the serial.Serial(...) outside of the while loop so that you only open it once.

March 10, 2011
by lcruz007
lcruz007's Avatar

Thanks, I think it solved my problem =) Thanks a lot, didn't think about that as a problem. Again not an expert with python! =P

Thank you both for your help! :)

March 10, 2011
by Ralphxyz
Ralphxyz's Avatar

So post you working code, hat will help all of us.

Ralph

March 11, 2011
by missle3944
missle3944's Avatar

I have a weird problem were I open the python program and then it suddenly closes. I tried raw_input, but it always closes right after I open it

Post a Reply

Please log in to post a reply.

Did you know that signed numbers need to be sign-extended when chaging variable sizes? Learn more...