User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

pyserial to Arduino....Ugh!

Sat Jan 04, 2014 11:29 pm

I'm stuck trying to fix this Raspberry Pi to Arduino serial communication problem. I'm able to get serial communication using http://inotool.org but when I close that terminal serial communication stops. How can I write my python scripts to take control of the serial port?

To illustrate the problem I've put together a screen capture of what's going. Sorry the video isn't that great but the sound is decent...

http://www.youtube.com/watch?v=VgBlNbY2 ... e=youtu.be

Here is my python script

Code: Select all

#!/usr/bin/python2

import serial
port = serial.Serial('/dev/ttyACM0', baudrate=9600)
port.write('0')
I have pySerial installed and using the current version. Not sure what else to do, any and all advice is appreciated.

Thanks
Rich

User avatar
DougieLawson
Posts: 39568
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 12:05 am

Have you taken the /dev/ttyAMA0 stuff out of /boot/cmdline.text and rebooted?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 12:12 am

Here is what it is now. Should I take out both references to AMA0?

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 12:40 am

I found this and gave it a try but it didn't work

http://www.irrational.net/2012/04/19/us ... rial-port/

User avatar
DougieLawson
Posts: 39568
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 12:48 am

richiepp wrote:Here is what it is now. Should I take out both references to AMA0?

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Yes
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 12:56 am

Yep I edited it down to this

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

But still not able to run the script without having the terminal open. Hmmmm.......

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Sun Jan 05, 2014 2:58 pm

Still stuck on this. I've scoured the web and as far as I can tell I've got everything configured to make this work. Are there any diagnostic tools I can use?

Stuck!

scotty101
Posts: 4010
Joined: Fri Jun 08, 2012 6:03 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 1:11 pm

richiepp wrote:

Code: Select all

#!/usr/bin/python2

import serial
port = serial.Serial('/dev/ttyACM0', baudrate=9600)
port.write('0')
What do you expect your python script to do?
What is your Arduino doing?
I have written a few python scripts to control Arduinos so might be able to help if you give a bit more detail about what you want to achieve.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 2:20 pm

I'm just trying to send a '0' with the python script which works, see the video above. The problem is I can't send a signal if the serial terminal is closed. I need the python script to take control of the port and send/receive over it. I feel like I've read every thread and tutorial on this but still can't get it to work.

How can I free up the serial port to be used by python scripts?

Thanks
Rich

scotty101
Posts: 4010
Joined: Fri Jun 08, 2012 6:03 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 2:53 pm

I see from above that you have removed the linux terminal output from the serial port so lets assume that you've done that correctly.

When you run the script that you have posted, does it return any errors? If the serial port is not available then it should return an error.

If it does return an error then some other process on your RPi has the ttyACM0 port open. You need to close anything like the Arduino software that might be accessing the serial port.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 3:59 pm

Did you comment out the following line in the /etc/inittab file

Code: Select all

#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 4:00 pm

Yes

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 4:10 pm

It appears I do have some python issues but these errors don't come up when I'm trying to run the python script with the terminal closed. Below is from my error log not sure how to interpret it.

Code: Select all

[Mon Jan 06 06:17:31 2014] [error] python_init: Python version mismatch, expected '2.7.2+', found '2.7.3'.
[Mon Jan 06 06:17:31 2014] [error] python_init: Python executable found '/usr/bin/python'.
[Mon Jan 06 06:17:31 2014] [error] python_init: Python path being used '/usr/lib/python2.7/:/usr/lib/python2.7/plat-linux2:/usr$
[Mon Jan 06 06:17:31 2014] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads.
[Mon Jan 06 06:17:31 2014] [notice] mod_python: using mutex_directory /tmp
[Mon Jan 06 06:17:32 2014] [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u7 mod_python/3.3.1 Python/2.7.3 configured -- resu$

scotty101
Posts: 4010
Joined: Fri Jun 08, 2012 6:03 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 4:12 pm

I have no idea what that is!

I would try a fresh install of the OS on to your SD card.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Mon Jan 06, 2014 4:16 pm

I would try a fresh install of the OS on to your SD card.
Yeah that's what I'm starting to think.....not my favorite solution but I guess that's how it goes.

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh!

Sat Feb 15, 2014 6:53 pm

I thought I'd format a card and start from scratch but I have so much invested in this image I'd like to figure it out. I've learned that I can run the serial terminal in the background

sudo picocom &

However I can't seem to get this start at reboot. With this running in the bacground I can call scripts that use the port. However when I try to call the python script from php I get the following error

Code: Select all

Traceback (most recent call last):
  File "cgi-bin/serial-to-arduino.py", line 4, in <module>
    port = serial.Serial('/dev/ttyACM0', baudrate=9600, timeout=0)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/local/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/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0'
I suspect the big problem here is the permissions issue at the end of the error. How can I give the web permission to execute python scripts?

Thanks
Rich

User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Re: pyserial to Arduino....Ugh! (SOLVED)

Wed Mar 05, 2014 5:20 am

Hey All,
After what seems like forever I've figured out how to hold the serial port open. It's a Linux program called Screen that allows you to run different screens in the background and toggle between them. Here's a quick overview of screen
http://www.rackaid.com/blog/linux-scree ... nd-how-to/

So glad to have solved this, thanks to everyone who has helped me get this far!

Ride On!
Rich

Return to “Python”