Added "getPortNames(String searchPath)" method and maded a little changes in "getUnixBasedPortNames()" method

This commit is contained in:
Alexey Sokolov 2013-04-19 11:50:25 +03:00
parent e8df64618d
commit cd4f05d84c
1 changed files with 30 additions and 4 deletions

View File

@ -161,7 +161,7 @@ public class SerialPortList {
public static String[] getPortNames() {
//since 2.1.0 ->
if(PORTNAMES_PATH != null){
return getUnixBasedPortNames();
return getUnixBasedPortNames(PORTNAMES_PATH);
}
//<- since 2.1.0
String[] portNames = serialInterface.getSerialPortNames();
@ -173,14 +173,40 @@ public class SerialPortList {
return ports.toArray(new String[ports.size()]);
}
/**
* Get sorted array of serial ports in the system. In Windows this method equals <b>getPortNames()</b>
*
* @param searchPath Path for searching serial ports. The default search paths:<br>
* Linux, MacOSX: <b>/dev/</b><br>
* Solaris: <b>/dev/term/</b><br>
* Windows: ingored
*
* @return String array. If there is no ports in the system String[]
*
* @since 2.3.0
*/
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);
}
/**
* Universal method for getting port names of _nix based systems
*
* @return
*/
private static String[] getUnixBasedPortNames() {
private static String[] getUnixBasedPortNames(String searchPath) {
String[] returnArray = new String[]{};
File dir = new File(PORTNAMES_PATH);
File dir = new File(/*PORTNAMES_PATH*/searchPath);
if(dir.exists() && dir.isDirectory()){
File[] files = dir.listFiles();
if(files.length > 0){
@ -188,7 +214,7 @@ public class SerialPortList {
for(File file : files){
String fileName = file.getName();
if(!file.isDirectory() && !file.isFile() && PORTNAMES_REGEXP.matcher(fileName).find()){
String portName = PORTNAMES_PATH + fileName;
String portName = /*PORTNAMES_PATH*/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