diff --git a/app/src/processing/app/windows/ListComPortsParser.java b/app/src/processing/app/windows/ListComPortsParser.java new file mode 100644 index 000000000..14a679956 --- /dev/null +++ b/app/src/processing/app/windows/ListComPortsParser.java @@ -0,0 +1,40 @@ +package processing.app.windows; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Interprets the output of listComPorts.exe + *

+ * https://github.com/todbot/usbSearch/ + */ +public class ListComPortsParser { + + private final Pattern vidRegExp; + private final Pattern pidRegExp; + + public ListComPortsParser() { + vidRegExp = Pattern.compile("VID_(\\w\\w\\w\\w)"); + pidRegExp = Pattern.compile("PID_(\\w\\w\\w\\w)"); + } + + public String extractVIDAndPID(String output, String serial) throws IOException { + BufferedReader reader = new BufferedReader(new StringReader(output)); + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith(serial.toUpperCase())) { + Matcher vidMatcher = vidRegExp.matcher(line); + Matcher pidMatcher = pidRegExp.matcher(line); + if (vidMatcher.find() && pidMatcher.find()) { + return vidMatcher.group(1).toUpperCase() + "_" + pidMatcher.group(1).toUpperCase(); + } + } + } + + return null; + } + +} diff --git a/app/src/processing/app/windows/Platform.java b/app/src/processing/app/windows/Platform.java index f45db5a68..9cac00bda 100644 --- a/app/src/processing/app/windows/Platform.java +++ b/app/src/processing/app/windows/Platform.java @@ -22,18 +22,24 @@ package processing.app.windows; -import java.io.File; -import java.io.UnsupportedEncodingException; - import com.sun.jna.Library; import com.sun.jna.Native; - +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.Executor; import processing.app.Base; import processing.app.Preferences; +import processing.app.debug.TargetPackage; +import processing.app.tools.ExternalProcessExecutor; import processing.app.windows.Registry.REGISTRY_ROOT_KEY; import processing.core.PApplet; import processing.core.PConstants; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Map; + // http://developer.apple.com/documentation/QuickTime/Conceptual/QT7Win_Update_Guide/Chapter03/chapter_3_section_1.html // HKEY_LOCAL_MACHINE\SOFTWARE\Apple Computer, Inc.\QuickTime\QTSysDir @@ -309,4 +315,27 @@ public class Platform extends processing.app.Platform { return PConstants.platformNames[PConstants.WINDOWS]; } + @Override + public String resolveDeviceAttachedTo(String serial, Map packages) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + Executor executor = new ExternalProcessExecutor(baos); + + try { + String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe").getCanonicalPath(); + + CommandLine toDevicePath = CommandLine.parse(listComPorts); + executor.execute(toDevicePath); + String vidPid = new ListComPortsParser().extractVIDAndPID(new String(baos.toByteArray()), serial); + + if (vidPid == null) { + return super.resolveDeviceAttachedTo(serial, packages); + } + + return super.resolveDeviceByVendorIdProductId(packages, vidPid); + } catch (IOException e) { + return super.resolveDeviceAttachedTo(serial, packages); + } + } + + } diff --git a/app/test/processing/app/windows/ListComPortsParserTest.java b/app/test/processing/app/windows/ListComPortsParserTest.java new file mode 100644 index 000000000..89539e205 --- /dev/null +++ b/app/test/processing/app/windows/ListComPortsParserTest.java @@ -0,0 +1,20 @@ +package processing.app.windows; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ListComPortsParserTest { + + @Test + public void shouldFindVIDPID() throws Exception { + String listComPortsOutput = "COM26 - FTDI - FTDIBUS\\VID_0403+PID_6001+A6004CCFA\\0000\nCOM24 - PJRC.COM, LLC. - USB\\VID_16C0&PID_0483\\12345"; + + ListComPortsParser parser = new ListComPortsParser(); + + assertEquals("0403_6001", parser.extractVIDAndPID(listComPortsOutput, "COM26")); + assertEquals("16C0_0483", parser.extractVIDAndPID(listComPortsOutput, "COM24")); + } + + +} diff --git a/build/build.xml b/build/build.xml index 22917e80f..a574c3fb6 100644 --- a/build/build.xml +++ b/build/build.xml @@ -669,9 +669,11 @@ + + diff --git a/build/windows/listComPorts.exe b/build/windows/listComPorts.exe new file mode 100644 index 000000000..ddb78d7bb Binary files /dev/null and b/build/windows/listComPorts.exe differ