mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Communication with Arduino problem with pyserial

Mon Dec 03, 2012 1:36 pm

Hi Guys,
I have the following stupid problem:

I'm trying to communicate with one (or more but now only with one) Arduino over USB via pySerial lib, but when i type this:

Code: Select all

 $ python
 >>> import serial
 >>> arduino = serial.Serial('/dev/ttyACM0', 9600) 
It takes a few seconds at least 5-6 to connect and after that when i type:

Code: Select all

>>> arduino.write('something')
it takes also at least 5-6 seconds and also the load of the raspberry grows to 4-5 and also sometimes raspberry crashes and must be restarted.

Did you have any idea what's happend?

Thanks in advance :))

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: Communication with Arduino problem with pyserial

Mon Dec 03, 2012 4:24 pm

did you remove the getty on ttyAMA0 ?
see http://www.irrational.net/2012/04/19/us ... rial-port/

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 9:48 am

Hi,
thank you very much about the advice, i wasn't, but now there is another problem:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "relay.py", line 5, in send_cmd
    arduino = serial.Serial('/dev/ttyACM0', 9600)
  File "/usr/lib/python2.6/dist-packages/serial/serialutil.py", line 166, in __init__
    self.open()
  File "/usr/lib/python2.6/dist-packages/serial/serialposix.py", line 175, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 5] Input/output error: '/dev/ttyACM0'
I can't connect to the arduino. I tried to restart the ardino several times but nothing different happens

Any ideas?

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 9:57 am

I'm sorry, I didn't noticed you are trying to communicate with the USB port of the Arduino.
Try make a search on "FTDI" : http://raspberrypi.org/phpBB3/search.php?keywords=ftdi

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 9:59 am


WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 10:01 am

One way of seeing what your Arduino is appearing as is:
ls /dev/tty*
Plug in you Arduino
ls /dev/tty*
Look at the difference.

It is possible your Arduino is appearing as /dev/ttyUSB0 or /dev/ttyACM1 instead.

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 10:22 am

Hi :),
thank you very much for fast answer. In this link are some many different cases, problems with USB hubs, problem with drivers and so on. Tonight after i get home i will try all of them and i will write what is happened :)

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 10:25 am

croston wrote:One way of seeing what your Arduino is appearing as is:
ls /dev/tty*
Plug in you Arduino
ls /dev/tty*
Look at the difference.

It is possible your Arduino is appearing as /dev/ttyUSB0 or /dev/ttyACM1 instead.
Hi Croston,
my Arduino is on ACM0 :)

make sure yourself:

Code: Select all

@pi on #raspberrypi in ~  
# ls /dev/tty*
/dev/tty    /dev/tty12  /dev/tty17  /dev/tty21  /dev/tty26  /dev/tty30  /dev/tty35  /dev/tty4   /dev/tty44  /dev/tty49  /dev/tty53  /dev/tty58  /dev/tty62  /dev/ttyACM0
/dev/tty0   /dev/tty13  /dev/tty18  /dev/tty22  /dev/tty27  /dev/tty31  /dev/tty36  /dev/tty40  /dev/tty45  /dev/tty5   /dev/tty54  /dev/tty59  /dev/tty63  /dev/ttyAMA0
/dev/tty1   /dev/tty14  /dev/tty19  /dev/tty23  /dev/tty28  /dev/tty32  /dev/tty37  /dev/tty41  /dev/tty46  /dev/tty50  /dev/tty55  /dev/tty6   /dev/tty7   /dev/ttyS7@
/dev/tty10  /dev/tty15  /dev/tty2   /dev/tty24  /dev/tty29  /dev/tty33  /dev/tty38  /dev/tty42  /dev/tty47  /dev/tty51  /dev/tty56  /dev/tty60  /dev/tty8
/dev/tty11  /dev/tty16  /dev/tty20  /dev/tty25  /dev/tty3   /dev/tty34  /dev/tty39  /dev/tty43  /dev/tty48  /dev/tty52  /dev/tty57  /dev/tty61  /dev/tty9

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 11:44 am

Something that I find useful when dealing with ttys, is looking in /dev/tty is full of virtual terminals which most folks never use (anyone used /dev/tty60 to /dev/tty64).

One thing you can do which makes it easier to look at this directory for debugging is create a file called

/etc/udev/rules.d/99-my-tty-rules.rules

Put this one line in

Code: Select all

KERNEL=="tty[1-9][0-9]", RUN="/bin/rm /dev/%k" OPTIONS+="ignore_devices"
then at start up you have only /dev/tty0 to /dev/tty9 the list is so much easier to look through.

This is a variation of this page

Next to no performance enhancement just a nicety when searching through lists. No extra hassle done at every boot, if you dont like delete the file and reboot.

Simples :-P
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 11:53 am

Nice idea Paul :)) Thanks :)

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 1:31 pm

better way here to find serial devices :
http://stackoverflow.com/questions/2530 ... ening-them

Code: Select all

1.7 TTY info in /proc/tty
-------------------------

Information about  the  available  and actually used tty's can be found in the
directory /proc/tty.You'll  find  entries  for drivers and line disciplines in
this directory, as shown in Table 1-11.


Table 1-11: Files in /proc/tty
..............................................................................
 File          Content                                        
 drivers       list of drivers and their usage                
 ldiscs        registered line disciplines                    
 driver/serial usage statistic and status of single tty lines 
..............................................................................

To see  which  tty's  are  currently in use, you can simply look into the file
/proc/tty/drivers:

  > cat /proc/tty/drivers 
  pty_slave            /dev/pts      136   0-255 pty:slave 
  pty_master           /dev/ptm      128   0-255 pty:master 
  pty_slave            /dev/ttyp       3   0-255 pty:slave 
  pty_master           /dev/pty        2   0-255 pty:master 
  serial               /dev/cua        5   64-67 serial:callout 
  serial               /dev/ttyS       4   64-67 serial 
  /dev/tty0            /dev/tty0       4       0 system:vtmaster 
  /dev/ptmx            /dev/ptmx       5       2 system 
  /dev/console         /dev/console    5       1 system:console 
  /dev/tty             /dev/tty        5       0 system:/dev/tty 
  unknown              /dev/tty        4    1-63 console 

the previous link I give suggest to add the pi user to the dialout group, but you can also try to run as root ;)

Code: Select all

sudo usermod -a -G dialout pi
what is your arduino ? Uno or previous one ?

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 2:37 pm

interesting discussion is formed here :)

I tried to add pi user dialout group but nothing different happens, also i tried and with root user but is the same ....

About the Arduino actualy is UNO.

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 3:24 pm

Ok, so /dev/ttyACM0 is okay regarding your previous ls command
Do you already used your Arduino under Linux ?
If no, when do you bought it ?
You may suffer the early 8U2 firmware bug (before december 2010)...
http://www.strangeparty.com/2010/12/13/ ... for-linux/

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 3:50 pm

mignev wrote:interesting discussion is formed here :)

I tried to add pi user dialout group but nothing different happens, also i tried and with root user but is the same ....

About the Arduino actualy is UNO.
You need to log out and back in for the group change to take effect. To check what groups you are in at the moment:

Code: Select all

groups

mignev
Posts: 20
Joined: Mon Dec 03, 2012 1:24 pm

Re: Communication with Arduino problem with pyserial

Tue Dec 04, 2012 7:32 pm

Hi guys,
i am very confused ... this stinker works whenever it wants ... I just pull the cable and after few seconds i push it back ... and works, but from time to time it breaks and from time to time fix by itself ... i dont know !?! I really am very confused

chirag
Posts: 18
Joined: Fri Dec 14, 2012 1:04 pm

Re: Communication with Arduino problem with pyserial

Sun Jan 27, 2013 7:50 am

python code:

import serial
ser=serial.Serial("/dev/ttyACM0",9600)
x=input("")
while 1:
ser.open()
ser.write(x)
im just trying to write to that file whatever i give the input its working fine but after 2 seconds il get


serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 24] Too many open files: '/dev/ttyACM0'

infinate loop because i want to write to that file whatever i enter you have any idea why this is happening..??

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Communication with Arduino problem with pyserial

Sun Jan 27, 2013 2:00 pm

You need to close ser after the write. Without that you open it every time and run out of file handlers.
Or move taht open before while.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Communication with Arduino problem with pyserial

Sun Jan 27, 2013 8:16 pm

I found I had to put a delay between sending strings from my Pi to Arduino

Gordon77

Code: Select all

import serial
            ser = serial.Serial('/dev/ttyACM0',9600)
            ser.write(bytes(Vcorrt.encode('ascii')))
            t1 = time.time()
            while time.time() < t1 + 0.2:
               t2 = time.time()
            ser.write(bytes(Hcorrt.encode('ascii')))

Return to “Python”