mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
working on #223: Auto-detection of serial ports. Windows version ready
This commit is contained in:
parent
66a811eeee
commit
1445529d1c
40
app/src/processing/app/windows/ListComPortsParser.java
Normal file
40
app/src/processing/app/windows/ListComPortsParser.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
20
app/test/processing/app/windows/ListComPortsParserTest.java
Normal file
20
app/test/processing/app/windows/ListComPortsParserTest.java
Normal 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"));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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">
|
||||
|
BIN
build/windows/listComPorts.exe
Normal file
BIN
build/windows/listComPorts.exe
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user