Spyder can’t read serial port

Asked

Viewed 201 times

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!

It always falls on except, as if it really had nothing at the door. When I shoot Try/except, it says it has no 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 works perfectly.

inserir a descrição da imagem aqui

1 answer

1


So - the problem is just that you don’t have permission on the serial device.

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

If you call your application with sudo - suddo python meuprog.py It works, too, you’ll see. By its output you are running the program directly from within an IDE - and it will obviously not let you put a "sudo" in front. A file .py on disk is a program like any other. Make sure that IDE is helping and don’t let her get in your way.

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

  • n has how to configure the IDE for it to access as root?

  • by the way chmod 666 /devttyUSB0 worked. dei a --help I understood the command, I just did not understand why of the 666 but every case thank you very much!

  • 1

    The chmod command is one of the few places where numbers are used in Octal. Each digit is a number from 0 to 7, which in binary is represented by 3 bits ; The first digit controls the permissions of the owner of the file (in this case, the owner is "root"), the second digit contravenes permissions for those in the same group (of users) that the file, and the third digit controls the permissions of all system users. In each of the three digits, the first bit controls access to read, the second to write and the third to run. Thus, "666" says that everyone can read and write in the file.

  • got it, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.