Semi-Automatic Arduino Detection.

Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Semi-Automatic Arduino Detection.

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:-
Code:
#!/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:-
Code:
#!/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.
Code:
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...
# 2  
Actually, I like it. Smilie The same operation in pure shell would be big ugly loops.

This looks weird though: IFS=$'\n'"\n" You would have three separators that way -- newline, backslash, and n. I think
Code:
IFS="
"

would be sufficient, not to mention, portable to shells which don't have $'' syntax.

I would avoid all that mucking with arrays. Makes it longer and less clear.

Code:
IFS="
"

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)

difference=$(comm -23 <(echo "$second_listing") <(echo "$first_listing"))
# /usr/bin/comm ends here...
# ---------------------------------------------------------

# $1 becomes first device, $2 becomes second device, etc.
set -- $difference # MUST NOT be quoted!

echo "$*"

echo "It is the first one that I need..."
echo "----------------------------------"
while true
do
	if [ -e "/dev/$1" ]
	then
		echo "All is mighty fine here. :o)"
		# Do at least something useful with the device here.
		sleep 1
		echo "Unplug Arduino when ready..."
	else
		echo "WHO UNPLUGGED ME..."
		break
	fi
done
echo "----------------------------------"
echo "Arduino has been disconnected. ;o("
IFS="$ifs_str"

# 3  
It looks like you might want to add:
Code:
ifs_str="$IFS"

before the first line in Corona688's script.
# 4  
Thanks guys.

Corona688, I will adapt your modifications into the AudioScope.sh.
I have not decided whether to use this near the start of the program or as a command
inside the program as yet but Arduino will be used for LF and DC conditions. That removes
the FM, AM, DC polarity and DC level board construction that I uploaded recently.

Don, I was aware of the Corona688's missing "ifs_str"...

Bazza.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
You can square root a negative number with an imaginary number "i".
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines without a trailing semi colon

shell : bash os : RHEL 7.2 I have a file like below 61265388 1-11Y5C-7690 1-11Y4Q-6763 INSERT INTO emp VALUES('oramds:test.xref','CBS_01','MIGWO161265388','61265388','N',SYSDATE); INSERT INTO emp VALUES('oramds:test.xref','COMMON','MIGWO161265388','MIG1COMMON61265388','N',SYSDATE);... (3 Replies)
Discussion started by: kraljic
3 Replies

2. OS X (Apple)

Arduino Diecimila Board Access...

This is a very simple starter DEMO to access Arduino Diecimila Board for the Macbook Pro 13" OSX 10.7.5... A potentiometer is connected between 5V and Gnd with the wiper connected to ANALOG IN 0 on the Arduino. This was adjusted to give the Ms and Ls as seen... I now have DC in for this... (0 Replies)
Discussion started by: wisecracker
0 Replies

3. Shell Programming and Scripting

Use The Terminal To Test Arduino Is Working.

Hi all... (Apologies for any typos at all.) This is a step by step _script_ to check if your Arduino is talking to your Linux or Macbook Pro computer using the Terminal... It works on at least 3 Linux flavours and now the Macbook Pro... I hope you find it useful as a simple check for... (0 Replies)
Discussion started by: wisecracker
0 Replies

4. Programming

Parallel Processing Detection and Program Return Value Detection

Hey, for the purpose of a research project I need to know if a specific type of parallel processing is being utilized by any user-run programs. Is there a way to detect whether a program either returns a value to another program at the end of execution, or just utilizes any form of parallel... (4 Replies)
Discussion started by: azar.zorn
4 Replies

5. Shell Programming and Scripting

sort and semi-duplicate row - keep latest only

I have a pipe delimited file. Key is field 2, date is field 5 (as example, my real file is more complicated of course, but the KEY and DATE are accurate) There can be duplicate rows for a key with different dates. I need to keep only rows with latest date in this case. Example data: ... (4 Replies)
Discussion started by: LisaS
4 Replies

6. UNIX for Advanced & Expert Users

GNU tar automatic gz detection/decompression

I stumbled on this feature on a SLES10 system yesterday... if you tar tf filename.tar.gz or tar xf filename.tar.gz it automatically gunzips the data for you. Has this feature been around for a while? I have 1.12 on my system, which doesn't, but the 1.20 manual mentions it... (3 Replies)
Discussion started by: Annihilannic
3 Replies

7. Shell Programming and Scripting

delete semi-duplicate lines from file?

Ok here's what I'm trying to do. I need to get a listing of all the mountpoints on a system into a file, which is easy enough, just using something like "mount | awk '{print $1}'" However, on a couple of systems, they have some mount points looking like this: /stage /stand /usr /MFPIS... (2 Replies)
Discussion started by: paqman
2 Replies

Featured Tech Videos