working on #223: Auto-detection of serial ports. Windows version ready

This commit is contained in:
Federico Fissore 2013-02-04 15:09:01 +01:00
parent 66a811eeee
commit 1445529d1c
5 changed files with 95 additions and 4 deletions

View File

@ -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
* <p/>
* 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;
}
}

View File

@ -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<String, TargetPackage> 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);
}
}
}

View File

@ -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"));
}
}

View File

@ -669,9 +669,11 @@
<!-- Copy bossac.exe tool -->
<copy todir="windows/work/hardware/tools">
<fileset file="windows/bossac.exe" />
<fileset file="windows/listComPorts.exe" />
</copy>
<chmod perm="755">
<fileset file="windows/work/hardware/tools/bossac.exe" />
<fileset file="windows/work/hardware/tools/listComPorts.exe" />
</chmod>
<antcall target="assemble">

Binary file not shown.