Просмотр исходного кода

Deal with random device names -- locate ports by physical USB port

Zachary Kotlarek 10 лет назад
Родитель
Сommit
69006a82ff
2 измененных файлов с 48 добавлено и 2 удалено
  1. 36 0
      serial/findDevice.sh
  2. 12 2
      serial/serial.pl

+ 36 - 0
serial/findDevice.sh

@@ -0,0 +1,36 @@
+#!/bin/bash
+
+# CLI Parameters
+USB_LOCATION="${1}"
+if [ -z "${USB_LOCATION}" ]; then
+	echo "Usage: ${0} USB_LOCATION" 1>&2
+	exit 1
+fi
+
+# Find the serial adapter
+TTY_ID="`ioreg -c IOSerialBSDClient | \
+	grep -C 6 "USB-Serial Controller@${USB_LOCATION}" | \
+	grep IOSerialBSDClient | \
+	cut -d '<' -f 2 | \
+	awk '$1 == "class" {print $4}' | \
+	cut -d 'x' -f 2 | \
+	cut -d ',' -f 1`"
+if [ -z "${TTY_ID}" ]; then
+	echo "Unable to find TTY_ID for USB_LOCATION: ${USB_LOCATION}" 1>&2
+	exit 2
+fi
+
+# Find the related device file
+DEV="`ioreg -c IOSerialBSDClient | \
+	grep -C 12 "${TTY_ID}" | \
+	grep 'IODialinDevice' | \
+	cut -d '=' -f 2 | \
+	awk -F '"' '{print $2}'`"
+if [ -z "${DEV}" ]; then
+	echo "Unable to find DEV for TTY_ID: ${TTY_ID}" 1>&2
+	exit 2
+fi
+
+# Display and exit
+echo "${DEV}"
+exit 0

+ 12 - 2
serial/serial.pl

@@ -55,7 +55,6 @@ if (basename($0) =~ /PROJECTOR/i) {
 	);
 } elsif (basename($0) =~ /AMPLIFIER/i) {
 	$DEV       = 'Amplifier';
-	$PORT      = '/dev/tty.usbserial-AM01C96Q';
 	$BLUETOOTH = 0;
 	$CRLF      = "\r";
 	$DELIMITER = "\r";
@@ -83,6 +82,12 @@ if (basename($0) =~ /PROJECTOR/i) {
 
 	);
 
+	$PORT = `$ENV{'HOME'}/bin/video/serial/findDevice.sh 1d111200`;
+	$PORT =~ s/\s*$//;
+	if (!($PORT =~ /^\/dev\/tty\.usbserial/)) {
+		die("Unable to find serial device by USB location\n");
+	}
+
 	%STATUS_CMDS = (
 		'STATUS' => { 'MATCH' => [ qr/^PW/, qr/$CMDS{'ON'}/ ] },
 		'MODE'   => { 'EVAL'  => [ qr/^MS/, 'if ($a =~ /STEREO/i) { $a = "STEREO" } elsif ($a =~ /MS(?:DOLBY|DTS)/i) { $a = "SURROUND" }' ] },
@@ -178,7 +183,6 @@ if (basename($0) =~ /PROJECTOR/i) {
 	);
 } elsif (basename($0) =~ /HDMI/i) {
 	$DEV       = 'HDMI';
-	$PORT      = '/dev/tty.usbserial-AL0096TO';
 	$BLUETOOTH = 0;
 	$BAUD      = 19200;
 	$CRLF      = "\r";
@@ -195,6 +199,12 @@ if (basename($0) =~ /PROJECTOR/i) {
 		'POD_OFF' => 'pod off',
 	);
 
+	$PORT = `$ENV{'HOME'}/bin/video/serial/findDevice.sh 1d111300`;
+	$PORT =~ s/\s*$//;
+	if (!($PORT =~ /^\/dev\/tty\.usbserial/)) {
+		die("Unable to find serial device by USB location\n");
+	}
+
 	%STATUS_CMDS = ();
 } else {
 	die("No device specified\n");