Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 83 lines (72 sloc) 2.048 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#!/usr/bin/perl -w
#
# listArduinos.pl -- look for Arduino (UNOs) and print out useful information
# about them like VID/PID and serial port name
# For Mac OS X only at the moment
#
#
# http://github.com/todbot/usbSearch/
#
# 2012, Tod E. Kurt, http://todbot.com/blog/
#
#

my $debug = 0;

my $output = `/usr/sbin/system_profiler SPUSBDataType`;

my @lines = split "\n", $output;


print "Finding Arduino UNOs...\n";

#
sub make_portname {
    my ($location_id) = @_;
    my $base = substr( $location_id, 2,4 );
    return "/dev/tty.usbmodem" . $base . "1";
}

my $look_state = 0;
my $arduinos = {};
my $arduino = {};
foreach my $line (@lines) {
    chomp($line);
    print "line: '$line'\n" if( $debug>1 );
    $line =~ s/\s+/ /;
    if( $look_state == 0 ) {
        if( $line =~ /Arduino Uno/ ) {
            $look_state = 1;
        }
    }
    elsif( $look_state == 1 ) {
        if( $line =~ /^$/ ) {
            $look_state = 2;
        }
    }
    elsif( $look_state == 2 ) {
        if( $line =~ /Serial Number: (.+)$/ ) {
            $arduino->{'serial_num'} = $1;
        }
        if( $line =~ /Location ID: (.+)$/ ) {
            $arduino->{'location_id'} = $1;
        }
        if( $line =~ /Product ID: (.+)$/ ) {
            $arduino->{'pid'} = $1;
        }
        if( $line =~ /Vendor ID: (.+)$/ ) {
            $arduino->{'vid'} = $1;
        }
        if( $line =~ /^$/ ) {
            $look_state = 0;
            $arduino->{'portname'} = make_portname($arduino->{'location_id'});
            $arduinos->{ $arduino->{'serial_num'} } = $arduino;
            $arduino = {}; # reset
        }
    }
}

my @serial_nums = keys %$arduinos;
my $count = scalar @serial_nums;
foreach my $serial (@serial_nums) {
    my $ard = $arduinos->{$serial};
    printf("%s - %s/%s - %s\n",
           $ard->{'portname'},
           $ard->{'vid'},
           $ard->{'pid'},
           $ard->{'serial_num'}
        );
}

print "Found ". $count ." Arduino UNO". (($count==1)?"\n":"s\n");

Something went wrong with that request. Please try again.