Help needed with pyserial (Arduino)


20 posts
by jeb365 » Tue May 29, 2012 1:43 am
I've seen a few posts now where people have got an Rpi talking to an arduino using pyserial. I use python/pyserial to send serial data to my arduino on my laptop (ubuntu) with no problem. When I come to do the same thing on my Rpi I have no such luck. I installed pyserial from the terminal with sudo apt-get install python-serial as instructed in other posts. When I run my test program:
Code: Select all
#!/usr/bin/env python

import serial                                 # imports serial library
import time                                    # imports time library
ser = serial.Serial("/dev/ttyACM0", 9600)            # init serial coms
                              

ser.write ("x")                                 # send void byte to reset arduino
time.sleep (2)                                 # wait for arduino reset cycle

ser.write ("A")                                 # send byte to turn on led   

I get this error:
Code: Select all
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    ser = serial.Serial("/dev/ttyACM0", 9600)            # init serial coms
  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 apologise if I'm wasting peoples time with something well obvious but I really could do with some help. Cheers
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by jbeale » Tue May 29, 2012 2:20 am
I don't have a Pi myself yet. But in case this is relevant- normally, unless reconfigured, the inbuilt serial port using the GPIO pins is already in use as the root console- for example it sends out the kernel boot messages when you boot up. So unless that's been unconfigured, the port will be in use.

Confirm you are using the right device. From the below link the GPIO serial device on the Pi is /dev/ttyAMA0
http://www.irrational.net/2012/04/19/us ... rial-port/
User avatar
Posts: 1885
Joined: Tue Nov 22, 2011 11:51 pm
by hexelpdkk » Tue May 29, 2012 5:10 am
Are you connecting using USB, or via the GPIO pins? I have successfully done this over USB, on the debian build, using "/dev/ttyUSB0" at 115200 baud. To open /dev/ttyUSB0 as a normal user, you need to add yourself to the dialout group, for example (for the default "pi" user)
Code: Select all
sudo usermod -a -G dialout pi
User avatar
Posts: 128
Joined: Fri Feb 24, 2012 4:40 pm
by jeb365 » Tue May 29, 2012 12:23 pm
Yeah sorry. Should of mentioned I'm connecting using the standard USB on the arduino. Cheers for the quick responses, I'll have another crack at it later. If anyone has the time to post a 'noob friendly' guide on this subject I'm sure it would go down well. I'm very new to programming so having a link to the real world (albeit flashing leds) is an incredibly useful learning aid.

To JBEALE and the rest of you not lucky enough to own a Pi yet. If theres anything you would like me to test on mine, send us a message an I'd be happy to help.
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by marsman2020 » Wed May 30, 2012 4:51 pm
I think adding your user to the dialout group as shown above will solve the problem. I haven't talked to an Arduino on the Pi yet but I have on a Sheeva Plug and that was a required step. The current Arduino Uno and Mega do show up as /dev/ttyACM* as the cdc-acm driver is used.
Posts: 70
Joined: Sun May 20, 2012 11:26 pm
by jeb365 » Wed May 30, 2012 6:29 pm
Quick update - Tried the above suggestion of adding user to dialout group but still received the same error message. Tried running with sudo. No change. Tried a fresh install of the debian image, installed pyserial and added user to dialout group. Still getting same error. My test program and arduino uno function as intended with ubuntu just cant get the same outcome with my Pi. Can anyone make sense of it from the error messages? Or does anyone know of a good guide for what I'm trying to do?

Cheers
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by hexelpdkk » Wed May 30, 2012 7:15 pm
My Duemilanove comes up as /dev/ttyUSB0 when I plug it in, so slight difference. I have added the goup dialout to the pi user, and simply do the following:
Code: Select all
>>> import serial
>>> connection = serial.Serial('/dev/ttyUSB0', 115200, timeout = 1)
>>> connection.write('!hw\r')
4
>>> connection.readline()
'HW:?\r\n'


As your Uno comes up with a different port, can you check it using
Code: Select all
 ls -l /dev/ttyACM0
. Post the line that comes up back here if you are still having problems.

For information, I am using raspbian distro, python 2.7, pyserial 2.6
User avatar
Posts: 128
Joined: Fri Feb 24, 2012 4:40 pm
by jeb365 » Wed May 30, 2012 8:29 pm
To hexelpdkk. Tried the code you posted. Still same error.
Hope this is some use -
Code: Select all
nick@raspberrypi:~$ sudo usermod -a -G dialout nick
[sudo] password for nick:
nick@raspberrypi:~$ ls -l /dev/ttyACM0
crw-rw---- 1 root dialout 166, 0 Jan  1  1970 /dev/ttyACM0
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by hexelpdkk » Wed May 30, 2012 9:26 pm
A few thoughts:

If it was permissions, your error would be "permission denied", so the usermod didn't help, I think, although you would have had to log out and back in to enable the new group for your user.

The only similar issue I can find is https://bugs.launchpad.net/ubuntu/jaunty/+source/linux/+bug/376128. If it was this, then your connection would work from a clean reboot, so maybe powercycle your pi and try it.

Other than that, I would suggest trying something like minicom. Install it, then run "sudo minicom -s", set the serial port, baud rate and turn off flow control. Again, this all "just works" for me at 115200 baud, 8N1 setting.
User avatar
Posts: 128
Joined: Fri Feb 24, 2012 4:40 pm
by jeb365 » Wed May 30, 2012 11:02 pm
Thanks again hexelpdkk. I'd kinda come to a similar conclusion. Just tried installing the arduino ide using this http://raspberrypi.homelabs.org.uk/raspberrypi-the-arduino-development-tool/. The ide works fine up until you try to communicate with the board. In the tool bar 'serial port' is greyed out, which is normal for serial port issues however, in the bottom right of the gui it states my arduino as a uno on com1, which should be correct. Not sure if 'uno on com1' is some sort of default output but it is consistent with everything else I've noticed (pi knows arduino is there and what it is but no talking). In the comments of the guide I used to install the ide, someone mentions ftdi issues. I think I no longer have a python problem anyway. I'll try the other fix and report back if I have any good news.
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by eleventigers » Wed May 30, 2012 11:54 pm
Try to add this as 55-odd.rules to /etc/udev/rules.d/

Code: Select all
KERNEL=="ttyACM0", SYMLINK+="ttyS7"


This will add a new device link to your original ttyACM0, enabling Arduino IDE and hopefully your pyserial to see your connected device as ttyS7.

Hope that helps ;]
Posts: 1
Joined: Wed May 30, 2012 11:44 pm
by jeb365 » Thu May 31, 2012 4:48 pm
No joy. Seems to be a driver issue. Looking into alternative interfacing now.
Posts: 15
Joined: Thu Mar 15, 2012 7:37 pm
by texy » Fri Jun 01, 2012 12:16 pm
marmrk has serial comms working with an arduino in this thread :
viewtopic.php?f=32&t=6319
Maybe he can help.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Moderator
Moderator
Posts: 2204
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by theunit » Tue Jun 05, 2012 7:25 pm
I am having the exact same problems. I haven'y tried Python but I cannot get communication working with C++ or LinuxTTY. All i get is an Input/output error.

Has anyone found a fix for this yet?
Posts: 31
Joined: Fri May 11, 2012 7:52 am
by theunit » Tue Jun 05, 2012 7:42 pm
Try plugging the Arduino in a different USB port (maybe directly to the RP)

I just rearranged the ordering of my Arduino and Keyboard to not be through my hub and now it all works! Really surprising since the Arduino is being powered directly from Raspberry Pi.
Posts: 31
Joined: Fri May 11, 2012 7:52 am
by tjs343 » Mon Jun 11, 2012 7:17 pm
Sorry to hear you're having issues with this. I managed to get my Arduino Uno (R3) to communicate with my raspberry pi, through the normal USB interface, with this tutorial:

http://www.doctormonk.com/2012/04/raspb ... duino.html

I'm not so good with Linux but the tutorial is written so well it doesn't matter. Hope this helps

Stevie
Posts: 1
Joined: Wed May 23, 2012 11:13 pm
by nebs » Sat Jun 30, 2012 10:26 pm
Just for the record, I had the same problem as jeb. Couldn't communicate with my Adruino Uno R2 via pyserial. I tried plugging my arduino into different usb ports with no luck. BUT, the suggestion by eleventigers worked for me!

Try to add this as 55-odd.rules to /etc/udev/rules.d/

CODE: SELECT ALL
KERNEL=="ttyACM0", SYMLINK+="ttyS7"


I now have /dev/ttyS7 in my list of devices, but I still communicate via /dev/ACM0.
Posts: 3
Joined: Sat Jun 30, 2012 6:56 pm
by nebs » Sat Jun 30, 2012 10:53 pm
Ok, so I spoke too soon :D

Upon further inspection, my arduino started to work after I unplugged other peripherals, so I think it might be a power issue in my case.

When I plug the arduino directly into my pi OR directly into a usb hub (which is powered) with no other usb devices connected, I could communicate via pyserial. But, after I plugged in my keyboard and mouse into the powered usb hub, it stopped communicating via pyserial and I got this error:

Code: Select all
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 5] Input/output error: '/dev/ttyACM0'


The combination of keyboard+arduino works for me, but not keyboard+mouse+arduino. Maybe it's my usb hub that doesn't work?
Posts: 3
Joined: Sat Jun 30, 2012 6:56 pm
by maartenv » Wed Jul 18, 2012 11:31 pm
I also had problems communicating with my arduino (Nano and Duemilanove) from my raspberry over usb/serial. The fix I found was on http://iijean.blogspot.nl/2012/05/ftdi-usb-driver-woes-on-debian-livecd.html

It seems to be a problem with the ftdi driver that can be fixed in the line creating the connection.

Not working:
self._port = serial.Serial(self._comport, 9600)

Working
self._port = serial.Serial(self._comport, 9600, rtscts=1)

I noticed the problem can vary over de linux versions. For me it disappeared with Wheezy (so removed the addition), but oddly reappeared with Raspbian (so re-introduced the addition).

Hope this helps
Posts: 3
Joined: Tue Feb 28, 2012 6:17 pm
by Beppe5 » Thu Nov 08, 2012 10:54 am
Hi,
I had the same problem but after a firmware update it's working fine.
Look at https://github.com/Hexxeh/rpi-update/
/B
Posts: 1
Joined: Thu Nov 08, 2012 10:47 am