Added ability to use this library with a user-defined serial port desriptor.

This commit is contained in:
hedgecrw85 2015-04-28 10:43:01 -05:00
parent 2ac7b35390
commit ec08bff500
2 changed files with 37 additions and 5 deletions

View File

@ -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
* <p>
* 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.
* <p>
* On Windows machines, this descriptor should be in the form of "COM[*]".<br>
* 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.

View File

@ -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());