I am working on a semi-auto detection idea for Arduino for the Scope project.
It does require a little user intervention but minimal.
It works by just responding to two on screen prompts to unplug and plug Arduino
into a USB port.
There are two versions and both work perfectly well and give almost exactly the same
results on screen.
The first uses /usr/bin/comm
and the second uses __builtins__ only.
The code is public domain for whoever might find use for it, however......
I have one question, I really want to use __builtins__ and "/bin" only, not commands
from "/usr/bin", so:-
In Code 2 the lines between......
# ---------------
see code
# ---------------
......look ugly.
Is there more elegant way of extracting a substring from the difference between two
strings.
I don't want you to code it for me just point me in the right direction
Code 1:-
#!/bin/bash
# This is for OSX 10.7.5, default bash terminal.
# Not tested on Linux as /dev/ttyUSBx or /dev/ttyACMx where x = 0 to 7.
# Tested on CygWin and gave /dev/ttyS11 on a Windows Vista machine.
ifs_str="$IFS"
IFS=$'\n'"\n"
read -p "Remove Arduino if it is connected, then press <CR> to continue:- "
sleep 1
first_listing=$(ls /dev)
read -p "Now replace Arduino, then press <CR> to continue:- "
sleep 1
second_listing=$(ls /dev)
if [ ${#first_listing} -ge ${#second_listing} ]
then
exit 1
fi
# ---------------------------------------------------------
# /usr/bin/comm starts here...
difference=$(comm -23 <(echo "$second_listing") <(echo "$first_listing"))
# /usr/bin/comm ends here...
# ---------------------------------------------------------
printf "$difference\n"
echo "It is the first one that I need..."
echo "----------------------------------"
difference_array=($difference)
difference_array='/dev/'"${difference_array[0]}"
# For the second USB Arduino device in OSX 10.7.5 use:-
# difference_array='/dev/'"${difference_array[1]}"
echo "$difference_array"
while true
do
# Redirect 'stderr'...
if [ "$(ls $difference_array 2>/dev/null)" == "$difference_array" ]
then
echo "All is mighty fine here. :o)"
echo "Unplug Arduino when ready..."
# Do at least something useful with the device here.
sleep 1
else
echo "WHO UNPLUGGED ME..."
break
fi
done
echo "----------------------------------"
echo "Arduino has been disconnected. ;o("
IFS="$ifs_str"
Code 2:-
#!/bin/bash
# This is for OSX 10.7.5, default bash terminal.
# Not tested on Linux as /dev/ttyUSBx or /dev/ttyACMx where x = 0 to 7.
# Tested on CygWin and gave /dev/ttyS11 on a Windows Vista machine.
ifs_str="$IFS"
IFS=$'\n'"\n"
read -p "Remove Arduino if it is connected, then press <CR> to continue:- "
sleep 1
first_listing=$(ls /dev)
read -p "Now replace Arduino, then press <CR> to continue:- "
sleep 1
second_listing=$(ls /dev)
if [ ${#first_listing} -ge ${#second_listing} ]
then
exit 1
fi
# ---------------------------------------------------------
# Builtins version starts here...
arrayone=($second_listing)
arraytwo=($first_listing)
arrayonestep=0
arraytwostep=0
difference=""
while [ "${arrayone[arrayonestep]}" != "" ]
do
if [ "${arrayone[arrayonestep]}" == "${arraytwo[arraytwostep]}" ]
then
arrayonestep=$[ ( $arrayonestep + 1 ) ]
arraytwostep=$[ ( $arraytwostep + 1 ) ]
else
difference=$difference"${arrayone[arrayonestep]}\n"
arrayonestep=$[ ( $arrayonestep + 1 ) ]
fi
done
# Builtins version ends here...
# ---------------------------------------------------------
printf "$difference\n"
echo "It is the first one that I need..."
echo "----------------------------------"
difference_array=($difference)
difference_array='/dev/'"${difference_array[0]}"
# For the second USB Arduino device in OSX 10.7.5 use:-
# difference_array='/dev/'"${difference_array[1]}"
echo "$difference_array"
while true
do
# Redirect 'stderr'...
if [ "$(ls $difference_array 2>/dev/null)" == "$difference_array" ]
then
echo "All is mighty fine here. :o)"
echo "Unplug Arduino when ready..."
# Do at least something useful with the device here.
sleep 1
else
echo "WHO UNPLUGGED ME..."
break
fi
done
echo "----------------------------------"
echo "Arduino has been disconnected. ;o("
IFS="$ifs_str"
Results using the four possible conditions of pluggin and unplugging. Only one works.
Last login: Thu Feb 13 18:52:23 on ttys000
AMIGA:barrywalker~> ./Arduino_detect_builtins.sh
Remove Arduino if it is connected, then press <CR> to continue:-
Now replace Arduino, then press <CR> to continue:-
cu.usbserial-A7007cvs
tty.usbserial-A7007cvs
It is the first one that I need...
----------------------------------
/dev/cu.usbserial-A7007cvs
All is mighty fine here. :o)
Unplug Arduino when ready...
All is mighty fine here. :o)
Unplug Arduino when ready...
All is mighty fine here. :o)
Unplug Arduino when ready...
WHO UNPLUGGED ME...
----------------------------------
Arduino has been disconnected. ;o(
AMIGA:barrywalker~> ./Arduino_detect_builtins.sh
Remove Arduino if it is connected, then press <CR> to continue:-
Now replace Arduino, then press <CR> to continue:-
AMIGA:barrywalker~> ./Arduino_detect_builtins.sh
Remove Arduino if it is connected, then press <CR> to continue:-
Now replace Arduino, then press <CR> to continue:-
AMIGA:barrywalker~> ./Arduino_detect_builtins.sh
Remove Arduino if it is connected, then press <CR> to continue:-
Now replace Arduino, then press <CR> to continue:-
AMIGA:barrywalker~> _
This works on OSX 10.7.5 and CygWin using bash.
Not tested on Linux flavours yet...