Merge pull request #282 from gedankenexperimenter/bug/find-device-port-macos

Better serial port detection for macOS
pull/284/head
Jesse Vincent 7 years ago committed by GitHub
commit fe71904441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,80 @@
#!/usr/bin/env perl
# Based on listArduinos.pl from https://github.com/todbot/usbSearch (License: MIT)
# Original (C) 2012, Tod E. Kurt, http://todbot.com/blog/
# This version by Michael Richters <gedankenexperimenter@gmail.com>
use warnings;
use strict;
# ioreg might be more machine-readable than system_profiler, but I haven't been able to
# get it to produce useful output
my @output = qx(/usr/sbin/system_profiler SPUSBDataType);
my $parse_state = 0;
my $device = {};
LINE: foreach my $line (@output) {
chomp $line;
if ( $parse_state == 0 ) {
if ( $line =~ m/Model 01/ ) {
$parse_state = 1;
next LINE;
}
}
if ( $parse_state == 1 ) {
if ( $line =~ m/^\s*$/ ) {
$parse_state = 2;
next LINE;
}
}
if ( $parse_state == 2 ) {
if ( $line =~ m/Serial Number: (.+)$/ ) {
$device->{'serial_number'} = $1;
next LINE;
}
if ( $line =~ m/Location ID: (.+)$/ ) {
$device->{'location_id'} = $1;
next LINE;
}
if ( $line =~ m/Product ID: (.+)$/ ) {
$device->{'product_id'} = $1;
next LINE;
}
if ( $line =~ m/Vendor ID: (.+)$/ ) {
$device->{'vendor_id'} = $1;
next LINE;
}
if ( $line =~ m/^\s*$/ ) {
last LINE;
}
}
}
die "Can't find Model 01" if ( $device == {} );
my $serial_port_name = "";
if ( exists( $device->{'serial_number'} ) ) {
$serial_port_name = "/dev/cu.usbmodem" . $device->{'serial_number'};
if ( -e $serial_port_name ) {
print $serial_port_name;
exit 0;
}
}
if ( exists( $device->{'location_id'} ) ) {
my $loc = substr( $device->{'location_id'}, 2, 4 );
$serial_port_name = "/dev/cu.usbmodem" . $loc . 1;
if ( -e $serial_port_name ) {
print $serial_port_name;
exit 0;
}
}
die "Can't find Model 01 serial port name";

@ -62,11 +62,9 @@ MD5="md5sum"
if [ "${uname_S}" = "Darwin" ]; then if [ "${uname_S}" = "Darwin" ]; then
find_device_port() { find_device_port() {
DEVICE_PORT="$(ls /dev/cu.usbmodemkbio* 2> /dev/null || echo '')" DIR=$(dirname "$0")
DEVICE_PORT="${DEVICE_PORT:-$(ls /dev/cu.usbmodemCkbio* 2> /dev/null || echo '')}" DEVICE_PORT_PROBER="${DIR}/find-device-port-macos"
DEVICE_PORT="${DEVICE_PORT:-$(ls /dev/cu.usbmodemHID* 2> /dev/null || echo '')}" DEVICE_PORT="$(perl ${DEVICE_PORT_PROBER})"
DEVICE_PORT="${DEVICE_PORT:-$(ls /dev/cu.usbmodemCHID* 2> /dev/null || echo '')}"
DEVICE_PORT="${DEVICE_PORT:-$(ls /dev/cu.usbmodem14* 2> /dev/null || echo '')}"
} }
reset_device_cmd() { reset_device_cmd() {
@ -80,8 +78,9 @@ if [ "${uname_S}" = "Darwin" ]; then
MD5="md5" MD5="md5"
find_bootloader_ports() { find_bootloader_ports() {
DEVICE_PORT_BOOTLOADER="$(ls /dev/cu.usbmodemkbio* 2> /dev/null || echo '')" DIR=$(dirname "$0")
DEVICE_PORT_BOOTLOADER="${DEVICE_PORT_BOOTLOADER:-$(ls /dev/cu.usbmodem14* 2> /dev/null || echo '')}" DEVICE_PORT_PROBER="${DIR}/find-device-port-macos"
DEVICE_PORT_BOOTLOADER="$(perl ${DEVICE_PORT_PROBER})"
} }
fi fi

Loading…
Cancel
Save