spyder can not read serial port

0

I created the following test program:

import serial
porta = '/dev/ttyUSB0'
baud_rate = 9600


try:

       Obj_porta = serial.Serial(porta, baud_rate)
       valor = Obj_porta.read()
       print valor
       Obj_porta.close()
except serial.SerialException:
       print"ERRO: Verifique se ha algum dispositivo conectado na porta!"

It displays the following error message:

 runfile('/home/joannis/.spyder2/.temp.py', wdir=r'/home/joannis/.spyder2')
ERRO: Verifique se ha algum dispositivo conectado na porta!

Always fall into the except, as if there really was nothing at the door. When I try / except, it says it does not have access. (permission denied)

runfile('/home/joannis/.spyder2/.temp.py', wdir=r'/home/joannis/.spyder2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "/home/joannis/.spyder2/.temp.py", line 20, in <module>
    Obj_porta = serial.Serial(porta, baud_rate)
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'

When I manually read through the terminal it works perfectly.

    
asked by anonymous 19.08.2015 / 21:09

1 answer

1

So - the problem is that you do not have permission on the serial device.

You have noticed that all the commands that you type test you type "sudo" before - that is, the applications access the serial port as the root user.

If you call your application with sudo - suddo python meuprog.py you will see that it works as well. By its output you are running the program straight from within an IDE - and it obviously will not let you put a "sudo" up front. A .py file on disk is a program like any other. Make sure that IDE is helping and do not let it get in your way.

Aside from running the program with sudo, the most permanent solution is to change the permissions of your serial port: in some distributions, simply doing chmod 666 /dev/ttyUSB0 may be enough.

    
20.08.2015 / 14:33