Setting up Processing under ubuntu /dev/ttyACM0 /dev/ttyUSB0 fix

Edit:
My sincere and heartfelt thanks go out to http://pblog.ebaker.me.uk/2011/09/processing-usb-ports-devttyacm0.html for the original idea.

I have just reinstalled linux and am in the middle of setting everything back up to play with my new Arduino Uno.

For some reason best left to the gods to fight over the naming convention for the USB serial device has changed breaking the functionality of Processing under linux with regard to Arduino Uno.

Previously when an Arduino was attached to a USB port the system would identify it as a standard serial port and call it /dev/ttyUSBn where n is a number (for example /dev/ttyUSB0). Now the serial port is identified as /dev/ttyACMn (for example /dev/ttyACM0) which Processing does not recognise as a valid serial port and the command Arduino.list() fails to return it.

The Processing dev team are aware of the issue and have fixed it in the next release but that does not help us.

There are two ways of dealing with this problem, a simple way and a difficult way:

Simple: rename the USB serial ports
Pros: easy
Cons: you need to monitor the /dev/ directory for new devices
Method: run a script that polls /dev/ and watches for /dev/ACM* creation or removal and link or unlink /dev/ttyS8* devices

Difficult: Fix the code in Processing
Pros: When it is fixed it is fixed
Cons: time consuming
Method: You would need to unpack the .jar file, find the code that enumerates serial ports and add /dev/ttyACM* as a recognised device and then repack the .jar file. I will look into this and amend this post at some point.

Here is a bash script to fix the issue the simple way, install it as a root cron job to run @REBOOT
#!/bin/bash
# Check if Arduino virtual serial ports exist or have been removed
# Fix for Processing Arduino enumeration issue
# Automatically creates or removes a serial port at /dev/ttyS80 and above if an Arduino is plugged in via usb
# Assuming Arduino is /dev/ttyACM6 new serial port will be /dev/ttyS86, /dev/ttyACM0 will be /dev/ttyS80 etc
# Basically, add 80 to the ttyACM{n} to give you ttyS8{n}
#
# Written by and for http://labby.co.uk/2012/02/setting-up-processing-under-ubuntu-devttyacm0-devttyusb0-fix

lockfile=/var/tmp/autoserial.lock

onexit () # clean up the lockfile so this script can be run again
{
echo Removing lockfile $lockfile
rm -f "$lockfile"
exit
}

# check if the script is running as root
w=`whoami`
if [ "$w" != "root" ]; then
echo "This script needs to be run as root"
exit
fi

# check if this script is already running
if [ -e "$lockfile" ]; then
# lockfile exists, exit gracefully
echo lockfile $lockfile exists, please remove it before restarting this script
exit
fi

# create the initial lockfile
touch $lockfile

# run the onexit() function when the script exits
trap onexit SIGINT SIGSEGV SIGQUIT SIGTERM

while true; do # run forever
for i in {0..9}; do
if [ -c /dev/ttyACM$i ]; then # check if /dev/ttyACM$i exists and is a character file
if [ ! -e /dev/ttyS8$i ]; then # check if /dev/ttyS8$i does not exist
ln -s /dev/ttyACM$i /dev/ttyS8$i # it does not exist, create a symbolic link to /dev/ttyACM$i
fi
else
if [ -e /dev/ttyS8$i ]; then # check if /dev/ttyS8$i exists
rm /dev/ttyS8$i # it does and it should not exist - someone unplugged the arduino?
fi
fi
done
sleep 1 # wait a second before starting checking again
done

When this script runs it automatically creates or removes a serial port at /dev/ttyS80 and above if an Arduino is plugged in via usb

This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Setting up Processing under ubuntu /dev/ttyACM0 /dev/ttyUSB0 fix

  1. Kuro says:

    This isn’t working for me, even after fixing the missing quotation mark after the echo statement in the root check.

    Looks like a great quick fix though

    Google Chrome 12.0.742.112 GNU/Linux
    Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30
    • Greg says:

      Thanks for spotting the missing “, I amended the line.

      What is the error you are getting?

      Google Chrome 18.0.1025.162 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
  2. NewtownGuy says:

    Just a thought. Not tested on your machine, but it’s helping me with Ubuntu 12.04.

    The names stay the same in /dev/serial/by-id/. Use them (e.g., /dev/serial/by-id/usb-Prolific_Technology_Inc._USB_Serial_Controller_if00-port0) instead of the shorthand (e.g., ttyUSB0).

    Google Chrome 25.0.1364.172 Windows Vista
    Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
  3. NewtownGuy says:

    Sorry. I tried it on a RaspberryPi, but /dev does not work the same way as on Ubuntu 10.04 or 12.04. There aren’t any /dev/serial/by-id or by-path folders there that my suggestion relies on. How many quirks are there in the RaspberryPi’s Ubuntu OS ???

    Firefox 19.0 Windows Vista
    Mozilla/5.0 (Windows NT 6.0; rv:19.0) Gecko/20100101 Firefox/19.0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge