Skip to content

Commit 8c60054

Browse files
author
Federico Fissore
committed
Windows: ListComPortsParser was reporting the same VID/PID for both COM12 and COM1. Fixes arduino#3333
1 parent b282e2a commit 8c60054

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

app/test/processing/app/windows/ListComPortsParserTest.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.Test;
3333

3434
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNull;
3536

3637
public class ListComPortsParserTest {
3738

@@ -46,12 +47,22 @@ public void shouldFindVIDPID() throws Exception {
4647
@Test
4748
public void shouldFindVIDPID2() throws Exception {
4849
String listComPortsOutput = "COM1 - (Standard port types) - ACPI\\PNP0501\\1\n" +
49-
"COM3 - IVT Corporation - {F12D3CF8-B11D-457E-8641-BE2AF2D6D204}\\IVTCOMM\\1&27902E60&2&0001\n" +
50-
"COM4 - IVT Corporation - {F12D3CF8-B11D-457E-8641-BE2AF2D6D204}\\IVTCOMM\\1&27902E60&2&0002\n" +
51-
"COM18 - FTDI - FTDIBUS\\VID_0403+PID_0000+A9EPHBR7A\\0000";
50+
"COM3 - IVT Corporation - {F12D3CF8-B11D-457E-8641-BE2AF2D6D204}\\IVTCOMM\\1&27902E60&2&0001\n" +
51+
"COM4 - IVT Corporation - {F12D3CF8-B11D-457E-8641-BE2AF2D6D204}\\IVTCOMM\\1&27902E60&2&0002\n" +
52+
"COM18 - FTDI - FTDIBUS\\VID_0403+PID_0000+A9EPHBR7A\\0000";
5253

5354
assertEquals("0X0403_0X0000", new ListComPortsParser().extractVIDAndPID(listComPortsOutput, "COM18"));
5455
}
5556

57+
@Test
58+
public void shouldNotBeFooledByCOMPortsWithSimilarNames() throws Exception {
59+
String listComPortsOutput = "COM1 - (Standard port types) - ACPI\\PNP0501\\1\n" +
60+
"COM2 - (Standard port types) - ACPI\\PNP0501\\2\n" +
61+
"COM12 - Arduino LLC (www.arduino.cc) - USB\\VID_2341&PID_8041&MI_00\\8&AB76839&0&0000\n" +
62+
"COM3 - FTDI - FTDIBUS\\VID_0403+PID_6015+DA00WSEWA\\0000";
63+
64+
assertEquals("0X2341_0X8041", new ListComPortsParser().extractVIDAndPID(listComPortsOutput, "COM12"));
65+
assertNull(new ListComPortsParser().extractVIDAndPID(listComPortsOutput, "COM1"));
66+
}
5667

5768
}

arduino-core/src/processing/app/windows/ListComPortsParser.java

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
130
package processing.app.windows;
231

332
import java.io.BufferedReader;
@@ -8,7 +37,7 @@
837

938
/**
1039
* Interprets the output of listComPorts.exe
11-
* <p/>
40+
* <p>
1241
* https://github.com/todbot/usbSearch/
1342
*/
1443
public class ListComPortsParser {
@@ -25,9 +54,11 @@ public String extractVIDAndPID(String output, String serial) throws IOException
2554
BufferedReader reader = new BufferedReader(new StringReader(output));
2655
String line;
2756
while ((line = reader.readLine()) != null) {
28-
if (line.startsWith(serial.toUpperCase())) {
29-
Matcher vidMatcher = vidRegExp.matcher(line);
30-
Matcher pidMatcher = pidRegExp.matcher(line);
57+
String[] lineParts = line.split(" ");
58+
if (lineParts.length > 0 && lineParts[0].toUpperCase().equals(serial.toUpperCase())) {
59+
String vidPidPart = lineParts[lineParts.length - 1];
60+
Matcher vidMatcher = vidRegExp.matcher(vidPidPart);
61+
Matcher pidMatcher = pidRegExp.matcher(vidPidPart);
3162
if (vidMatcher.find() && pidMatcher.find()) {
3263
return ("0x" + vidMatcher.group(1) + "_0x" + pidMatcher.group(1)).toUpperCase();
3364
}

0 commit comments

Comments
 (0)