From ec08bff5007e4fdd98dbb77430a6519dac175061 Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Tue, 28 Apr 2015 10:43:01 -0500 Subject: [PATCH] Added ability to use this library with a user-defined serial port desriptor. --- .../com/fazecast/jSerialComm/SerialPort.java | 29 +++++++++++++++++-- .../fazecast/jSerialComm/SerialPortTest.java | 13 +++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 632d585..727b434 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -38,7 +38,7 @@ import java.util.Date; * This class provides native access to serial ports and devices without requiring external libraries or tools. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.1.1 + * @version 1.2.0 * @see java.io.InputStream * @see java.io.OutputStream */ @@ -169,10 +169,33 @@ public final class SerialPort *

* All serial port parameters or timeouts can be changed at any time after the port has been opened. * - * @return An array of SerialPort objects. + * @return An array of {@link SerialPort} objects. */ static public native SerialPort[] getCommPorts(); + /** + * Allocates a {@link SerialPort} object corresponding to the user-specified port descriptor. + *

+ * On Windows machines, this descriptor should be in the form of "COM[*]".
+ * On Linux machines, the descriptor will look similar to "/dev/tty[*]". + * + * @param portDescriptor The desired serial port to use with this library. + * @return A SerialPort object. + */ + static public SerialPort getCommPort(String portDescriptor) + { + // Correct Windows port descriptor, if needed + if (portDescriptor.contains("COM")) + portDescriptor = "\\\\.\\" + portDescriptor.substring(portDescriptor.lastIndexOf('\\')+1); + + // Create SerialPort object + SerialPort serialPort = new SerialPort(); + serialPort.comPort = portDescriptor; + serialPort.portString = "User-Specified Port"; + + return serialPort; + } + // Parity Values static final public int NO_PARITY = 0; static final public int ODD_PARITY = 1; @@ -302,7 +325,7 @@ public final class SerialPort public final native int writeBytes(byte[] buffer, long bytesToWrite); // Default Constructor - public SerialPort() {} + private SerialPort() {} /** * Adds a {@link SerialPortDataListener} to the serial port interface. diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index bdff15b..30fc2e9 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -61,10 +61,19 @@ public class SerialPortTest System.out.println("\nAvailable Ports:\n"); for (int i = 0; i < ports.length; ++i) System.out.println(" " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName()); - System.out.print("\nChoose your desired serial port: "); + SerialPort ubxPort; + System.out.print("\nChoose your desired serial port or enter -1 to specify a port directly: "); int serialPortChoice = 0; try { serialPortChoice = (new Scanner(System.in)).nextInt(); } catch (Exception e) {} - SerialPort ubxPort = ports[serialPortChoice]; + if (serialPortChoice == -1) + { + String serialPortDescriptor = ""; + System.out.print("\nSpecify your desired serial port descriptor: "); + try { serialPortDescriptor = (new Scanner(System.in)).nextLine(); } catch (Exception e) {} + ubxPort = SerialPort.getCommPort(serialPortDescriptor); + } + else + ubxPort = ports[serialPortChoice]; byte[] readBuffer = new byte[2048]; System.out.println("\nOpening " + ubxPort.getDescriptivePortName() + ": " + ubxPort.openPort());