Added ability to resolve symbolic links.

This commit is contained in:
hedgecrw85 2016-02-18 11:09:27 -06:00
parent f55a1af7e7
commit cbc7f952b4
2 changed files with 38 additions and 14 deletions

14
INSTALL
View File

@ -123,29 +123,29 @@ Maven:
<dependency> <dependency>
<groupId>com.fazecast</groupId> <groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId> <artifactId>jSerialComm</artifactId>
<version>1.3.9</version> <version>1.3.11</version>
</dependency> </dependency>
Ivy: Ivy:
<dependency org="com.fazecast" name="jSerialComm" rev="1.3.9"/> <dependency org="com.fazecast" name="jSerialComm" rev="1.3.11"/>
Groovy: Groovy:
@Grab(group='com.fazecast', module='jSerialComm', version='1.3.9') @Grab(group='com.fazecast', module='jSerialComm', version='1.3.11')
Gradle: Gradle:
compile 'com.fazecast:jSerialComm:1.3.9' compile 'com.fazecast:jSerialComm:1.3.11'
Buildr: Buildr:
compile.with 'com.fazecast:jSerialComm:jar:1.3.9' compile.with 'com.fazecast:jSerialComm:jar:1.3.11'
Scala/SBT: Scala/SBT:
libraryDependencies += "com.fazecast" % "jSerialComm" % "1.3.9" libraryDependencies += "com.fazecast" % "jSerialComm" % "1.3.11"
Leiningen: Leiningen:
[com.fazecast/jSerialComm "1.3.9"] [com.fazecast/jSerialComm "1.3.11"]

View File

@ -40,7 +40,7 @@ import java.util.Date;
* This class provides native access to serial ports and devices without requiring external libraries or tools. * This class provides native access to serial ports and devices without requiring external libraries or tools.
* *
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt; * @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 1.3.10 * @version 1.3.11
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */
@ -181,6 +181,13 @@ public final class SerialPort
System.load(tempFileName); System.load(tempFileName);
initializeLibrary(); initializeLibrary();
} }
// Static symbolic link testing function
private static boolean isSymbolicLink(File file) throws IOException
{
File canonicalFile = (file.getParent() == null) ? file : new File(file.getParentFile().getCanonicalFile(), file.getName());
return !canonicalFile.getCanonicalFile().equals(canonicalFile.getAbsoluteFile());
}
/** /**
* Returns a list of all available serial ports on this machine. * Returns a list of all available serial ports on this machine.
@ -207,12 +214,29 @@ public final class SerialPort
static public SerialPort getCommPort(String portDescriptor) static public SerialPort getCommPort(String portDescriptor)
{ {
// Correct port descriptor, if needed // Correct port descriptor, if needed
if (isWindows) try
portDescriptor = "\\\\.\\" + portDescriptor.substring(portDescriptor.lastIndexOf('\\')+1); {
else if (portDescriptor.contains("/pts/")) // Resolve home directory ~
portDescriptor = "/dev/pts/" + portDescriptor.substring(portDescriptor.lastIndexOf('/')+1); if (portDescriptor.startsWith("~" + File.separator))
else if (!((new File(portDescriptor)).exists())) portDescriptor = System.getProperty("user.home") + portDescriptor.substring(1);
portDescriptor = "/dev/" + portDescriptor.substring(portDescriptor.lastIndexOf('/')+1);
// See what kind of descriptor was passed in
if (isWindows)
portDescriptor = "\\\\.\\" + portDescriptor.substring(portDescriptor.lastIndexOf('\\')+1);
else if (isSymbolicLink(new File(portDescriptor)))
portDescriptor = (new File(portDescriptor)).getCanonicalPath();
else if (portDescriptor.contains("/pts/"))
portDescriptor = "/dev/pts/" + portDescriptor.substring(portDescriptor.lastIndexOf('/')+1);
else if (!((new File(portDescriptor)).exists()))
portDescriptor = "/dev/" + portDescriptor.substring(portDescriptor.lastIndexOf('/')+1);
}
catch (Exception e)
{
SerialPort serialPort = new SerialPort();
serialPort.comPort = "/dev/null";
serialPort.portString = "Bad Port";
return serialPort;
}
// Create SerialPort object // Create SerialPort object
SerialPort serialPort = new SerialPort(); SerialPort serialPort = new SerialPort();