From 6ab3e83451b9ddc31b3e4221e3ad0bffe4354e3e Mon Sep 17 00:00:00 2001 From: scream3r Date: Fri, 19 Apr 2013 14:41:15 +0400 Subject: [PATCH] Added method "getPortNames(String searchPath, Pattern pattern, Comparator comparator)", added default PORTNAMES_REGEXP for Windows, modified "getUnixBasedPortNames()" method --- src/java/jssc/SerialPortList.java | 84 ++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/src/java/jssc/SerialPortList.java b/src/java/jssc/SerialPortList.java index 51546ba..f5fa667 100644 --- a/src/java/jssc/SerialPortList.java +++ b/src/java/jssc/SerialPortList.java @@ -25,7 +25,6 @@ package jssc; import java.io.File; -import java.util.Arrays; import java.util.Comparator; import java.util.TreeSet; import java.util.regex.Pattern; @@ -58,6 +57,11 @@ public class SerialPortList { PORTNAMES_PATH = "/dev/"; break; } + case SerialNativeInterface.OS_WINDOWS: { + PORTNAMES_REGEXP = Pattern.compile(""); + PORTNAMES_PATH = ""; + break; + } default: { PORTNAMES_REGEXP = null; PORTNAMES_PATH = null; @@ -159,22 +163,12 @@ public class SerialPortList { * with zero length will be returned (since jSSC-0.8 in previous versions null will be returned) */ public static String[] getPortNames() { - //since 2.1.0 -> - if(PORTNAMES_PATH != null){ - return getUnixBasedPortNames(PORTNAMES_PATH); - } - //<- since 2.1.0 - String[] portNames = serialInterface.getSerialPortNames(); - if(portNames == null){ - return new String[]{}; - } - TreeSet ports = new TreeSet(comparator); - ports.addAll(Arrays.asList(portNames)); - return ports.toArray(new String[ports.size()]); + return getPortNames(PORTNAMES_PATH, PORTNAMES_REGEXP, comparator); } /** - * Get sorted array of serial ports in the system. In Windows this method equals getPortNames() + * Get sorted array of serial ports in the system, located by setted searchPath. + * In Windows this method equals getPortNames() * * @param searchPath Path for searching serial ports. The default search paths:
* Linux, MacOSX: /dev/
@@ -185,36 +179,70 @@ public class SerialPortList { * * @since 2.3.0 */ - public static String[] getPortNames(String searchPath) { + /*public static String[] getPortNames(String searchPath) { if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){ return getPortNames(); } - if(searchPath == null){ - searchPath = ""; - } - if(!searchPath.equals("") && !searchPath.endsWith("/")){ - searchPath += "/"; - } - System.out.println("Result search path: " + searchPath); return getUnixBasedPortNames(searchPath); + }*/ + + /** + * Get sorted array of serial ports in the system + * + * @param searchPath Path for searching serial ports. The default search paths:
+ * Linux, MacOSX: /dev/
+ * Solaris: /dev/term/
+ * Windows: ingored + * + * + * @return String array. If there is no ports in the system String[] + * + * @since 2.3.0 + */ + public static String[] getPortNames(String searchPath, Pattern pattern, Comparator comparator) { + if(searchPath == null || pattern == null || comparator == null){ + return new String[]{}; + } + if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_WINDOWS){ + return getWindowsPortNames(pattern, comparator); + } + return getUnixBasedPortNames(searchPath, pattern, comparator); + } + + /** + * Get serial port names in Windows + * + * @since 2.3.0 + */ + private static String[] getWindowsPortNames(Pattern pattern, Comparator comparator) { + String[] portNames = serialInterface.getSerialPortNames(); + if(portNames == null){ + return new String[]{}; + } + TreeSet ports = new TreeSet(comparator); + for(String portName : portNames){ + if(pattern.matcher(portName).find()){ + ports.add(portName); + } + } + return ports.toArray(new String[ports.size()]); } /** * Universal method for getting port names of _nix based systems - * - * @return */ - private static String[] getUnixBasedPortNames(String searchPath) { + private static String[] getUnixBasedPortNames(String searchPath, Pattern pattern, Comparator comparator) { + searchPath = (searchPath.equals("") ? searchPath : (searchPath.endsWith("/") ? searchPath : searchPath + "/")); String[] returnArray = new String[]{}; - File dir = new File(/*PORTNAMES_PATH*/searchPath); + File dir = new File(searchPath); if(dir.exists() && dir.isDirectory()){ File[] files = dir.listFiles(); if(files.length > 0){ TreeSet portsTree = new TreeSet(comparator); for(File file : files){ String fileName = file.getName(); - if(!file.isDirectory() && !file.isFile() && PORTNAMES_REGEXP.matcher(fileName).find()){ - String portName = /*PORTNAMES_PATH*/searchPath + fileName; + if(!file.isDirectory() && !file.isFile() && pattern.matcher(fileName).find()){ + String portName = searchPath + fileName; if(SerialNativeInterface.getOsType() == SerialNativeInterface.OS_LINUX){ int portHandle = serialInterface.openPort(portName, false);//Open port without TIOCEXCL if(portHandle < 0 && portHandle != -1){//If port handle == -1 it's mean that it's busy