Added ability to use this library with a user-defined serial port desriptor.
This commit is contained in:
parent
2ac7b35390
commit
ec08bff500
|
@ -38,7 +38,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 <will.hedgecock@fazecast.com>
|
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
||||||
* @version 1.1.1
|
* @version 1.2.0
|
||||||
* @see java.io.InputStream
|
* @see java.io.InputStream
|
||||||
* @see java.io.OutputStream
|
* @see java.io.OutputStream
|
||||||
*/
|
*/
|
||||||
|
@ -169,10 +169,33 @@ public final class SerialPort
|
||||||
* <p>
|
* <p>
|
||||||
* All serial port parameters or timeouts can be changed at any time after the port has been opened.
|
* 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();
|
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
|
// Parity Values
|
||||||
static final public int NO_PARITY = 0;
|
static final public int NO_PARITY = 0;
|
||||||
static final public int ODD_PARITY = 1;
|
static final public int ODD_PARITY = 1;
|
||||||
|
@ -302,7 +325,7 @@ public final class SerialPort
|
||||||
public final native int writeBytes(byte[] buffer, long bytesToWrite);
|
public final native int writeBytes(byte[] buffer, long bytesToWrite);
|
||||||
|
|
||||||
// Default Constructor
|
// Default Constructor
|
||||||
public SerialPort() {}
|
private SerialPort() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a {@link SerialPortDataListener} to the serial port interface.
|
* Adds a {@link SerialPortDataListener} to the serial port interface.
|
||||||
|
|
|
@ -61,10 +61,19 @@ public class SerialPortTest
|
||||||
System.out.println("\nAvailable Ports:\n");
|
System.out.println("\nAvailable Ports:\n");
|
||||||
for (int i = 0; i < ports.length; ++i)
|
for (int i = 0; i < ports.length; ++i)
|
||||||
System.out.println(" " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName());
|
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;
|
int serialPortChoice = 0;
|
||||||
try { serialPortChoice = (new Scanner(System.in)).nextInt(); } catch (Exception e) {}
|
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];
|
byte[] readBuffer = new byte[2048];
|
||||||
|
|
||||||
System.out.println("\nOpening " + ubxPort.getDescriptivePortName() + ": " + ubxPort.openPort());
|
System.out.println("\nOpening " + ubxPort.getDescriptivePortName() + ": " + ubxPort.openPort());
|
||||||
|
|
Loading…
Reference in New Issue