Added bytesAwaitingWrite() method and allow enumeration of rfcomm Bluetooth ports.

This commit is contained in:
hedgecrw85 2016-12-05 14:25:23 -06:00
parent d9aba7118e
commit 73b0d5918c
18 changed files with 284 additions and 49 deletions

View File

@ -5,7 +5,7 @@ apply plugin: 'osgi'
group = 'com.fazecast'
archivesBaseName = 'jSerialComm'
version = '1.3.12'
version = '1.4.0'
sourceCompatibility = 1.6
targetCompatibility = 1.6
@ -13,9 +13,12 @@ javadoc.options.links("http://docs.oracle.com/javase/7/docs/api/")
jar {
manifest {
instruction 'Bundle-Vendor', 'Fazecast, Inc.'
instruction 'Bundle-Description', 'Java Serial Communications Library'
instruction 'Sealed', 'true'
instruction 'Bundle-Description', 'jSerialComm: Java Serial Communications Library'
instruction 'Bundle-Vendor', 'Fazecast, Inc.'
attributes 'Implementation-Title': 'jSerialComm: Java Serial Communications Library',
'Implementation-Version': version,
'Implementation-Vendor': 'Fazecast, Inc.',
'Sealed': 'true'
}
}

View File

@ -2,10 +2,10 @@
* AndroidHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Mar 10, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -252,6 +252,13 @@ void setBaudRate(int portFD, int baudRate)
ioctl(portFD, TCGETS2, &options);
else
return;
// {
// struct usbdevfs_ioctl requestWrapper;
// requestWrapper.ifno = 1;// TODO
// requestWrapper.ioctl_code = TCGETS2;
// requestWrapper.data = &options;
// ioctl(portFD, USBDEVFS_IOCTL, &requestWrapper);
// }
options.c_cflag &= ~CBAUD;
options.c_cflag |= BOTHER;
options.c_ispeed = baudRate;
@ -260,18 +267,39 @@ void setBaudRate(int portFD, int baudRate)
ioctl(portFD, TCSETS2, &options);
else
return;
// {
// struct usbdevfs_ioctl requestWrapper;
// requestWrapper.ifno = 1;// TODO
// requestWrapper.ioctl_code = TCSETS2;
// requestWrapper.data = &options;
// ioctl(portFD, USBDEVFS_IOCTL, &requestWrapper);
// }
#else
struct termios options = { 0 };
if (isatty(portFD))
ioctl(portFD, TCGETS, &options);
else
return;
// {
// struct usbdevfs_ioctl requestWrapper;
// requestWrapper.ifno = 1;// TODO
// requestWrapper.ioctl_code = TCGETS;
// requestWrapper.data = &options;
// ioctl(portFD, USBDEVFS_IOCTL, &requestWrapper);
// }
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
if (isatty(portFD))
ioctl(portFD, TCSETS, &options);
else
return;
// {
// struct usbdevfs_ioctl requestWrapper;
// requestWrapper.ifno = 1;// TODO
// requestWrapper.ioctl_code = TCSETS;
// requestWrapper.data = &options;
// ioctl(portFD, USBDEVFS_IOCTL, &requestWrapper);
// }
#endif
}

View File

@ -2,10 +2,10 @@
* AndroidHelperFunctions.h
*
* Created on: Mar 10, 2015
* Last Updated on: Mar 10, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* SerialPort_Android.c
*
* Created on: Mar 13, 2015
* Last Updated on: Oct 09, 2015
* Last Updated on: Dec 05, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -118,6 +118,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_uninitializeLibr
JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(JNIEnv *env, jobject obj)
{
// TODO: SET A FLAG SAYING THAT WE ARE NOT USING USBFS
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
@ -152,6 +153,29 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
return serialPortFD;
}
/*JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_associateNativeHandle(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// TODO: SET A FLAG SAYING THAT WE ARE USING USBFS
// Attempt to determine which serial port file this descriptor belongs to
char serialPortFdChars[16];
sprintf(serialPortFdChars, "%d", (int)serialPortFD);
char* fileDescriptorName = (char*)malloc(256), *portName = (char*)malloc(256);
strcpy(fileDescriptorName, "/proc/self/fd/");
strcat(fileDescriptorName, serialPortFdChars);
ssize_t result = readlink(fileDescriptorName, portName, 256);
free(fileDescriptorName);
if (result < 0)
{
free(portName);
return JNI_FALSE;
}
// Set the port name in the Java object
(*env)->SetObjectField(env, obj, comPortField, (*env)->NewStringUTF(env, portName));
free(portName);
return JNI_TRUE;
}*/
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
@ -187,7 +211,14 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
}
else
return JNI_FALSE;
return JNI_FALSE;/*
{
struct usbdevfs_ioctl requestWrapper;
requestWrapper.ifno = 1;// TODO
requestWrapper.ioctl_code = TCGETS;
requestWrapper.data = &options;
ioctl(serialPortFD, USBDEVFS_IOCTL, &requestWrapper);
}*/
// Set baud rate
unsigned int baudRateCode = getBaudRateCode(baudRate);
@ -202,7 +233,14 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
if (isatty(serialPortFD))
retVal = ioctl(serialPortFD, TCSETS, &options);
else
return JNI_FALSE;
return JNI_FALSE;/*
{
struct usbdevfs_ioctl requestWrapper;
requestWrapper.ifno = 1;// TODO
requestWrapper.ioctl_code = TCSETS;
requestWrapper.data = &options;
retVal = ioctl(serialPortFD, USBDEVFS_IOCTL, &requestWrapper);
}*/
if (baudRateCode == 0) // Set custom baud rate
setBaudRate(serialPortFD, baudRate);
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
@ -223,7 +261,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
if (isatty(serialPortFD))
ioctl(serialPortFD, TCGETS, &options);
else
return JNI_FALSE;
return JNI_FALSE;/*
{
struct usbdevfs_ioctl requestWrapper;
requestWrapper.ifno = 1;// TODO
requestWrapper.ioctl_code = TCGETS;
requestWrapper.data = &options;
if (ioctl(serialPortFD, USBDEVFS_IOCTL, &requestWrapper) < 0)
LOGD("ERROR GETTING tcgetattr PORT SETTINGS = %d\n", errno);
}*/
int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1)
return JNI_FALSE;
@ -273,7 +319,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
if (isatty(serialPortFD))
retVal = ioctl(serialPortFD, TCSETS, &options);
else
return JNI_FALSE;
return JNI_FALSE;/*
{
struct usbdevfs_ioctl requestWrapper;
requestWrapper.ifno = 1;// TODO
requestWrapper.ioctl_code = TCSETS;
requestWrapper.data = &options;
if (ioctl(serialPortFD, USBDEVFS_IOCTL, &requestWrapper) < 0)
LOGD("ERROR SETTING ioctl PORT SETTINGS = %d\n", errno);
}*/
}
if (baudRateCode == 0) // Set custom baud rate
setBaudRate(serialPortFD, baudRate);
@ -355,6 +409,15 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(J
return numBytesAvailable;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAwaitingWrite(JNIEnv *env, jobject obj, jlong serialPortFD)
{
int numBytesToWrite = -1;
if (serialPortFD > 0)
ioctl(serialPortFD, TIOCOUTQ, &numBytesToWrite);
return numBytesToWrite;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv *env, jobject obj, jlong serialPortFD, jbyteArray buffer, jlong bytesToRead)
{
// Get port handle and read timeout from Java class

View File

@ -2,10 +2,10 @@
* LinuxHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Mar 10, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -132,7 +132,9 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
if ((directoryEntry->d_name[0] != '.') && (strcmp(directoryEntry->d_name, "virtual") != 0))
{
// See if the directory names a potential serial port
if ((strlen(directoryEntry->d_name) > 3) && (directoryEntry->d_name[0] == 't') && (directoryEntry->d_name[1] == 't') && (directoryEntry->d_name[2] == 'y'))
if ((strlen(directoryEntry->d_name) > 3) &&
(((directoryEntry->d_name[0] == 't') && (directoryEntry->d_name[1] == 't') && (directoryEntry->d_name[2] == 'y')) ||
((directoryEntry->d_name[0] == 'r') && (directoryEntry->d_name[1] == 'f') && (directoryEntry->d_name[2] == 'c'))))
{
// Determine system name of port
char* systemName = (char*)malloc(256);
@ -160,7 +162,14 @@ void recursiveSearchForComPorts(charPairVector* comPorts, const char* fullPathTo
int fd = open(systemName, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd >= 0)
{
if (((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[3] == 'A') && (directoryEntry->d_name[4] == 'M') && (directoryEntry->d_name[5] == 'A')) ||
if ((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[0] == 'r') && (directoryEntry->d_name[1] == 'f') && (directoryEntry->d_name[2] == 'c') &&
(directoryEntry->d_name[3] == 'o') && (directoryEntry->d_name[4] == 'm') && (directoryEntry->d_name[5] == 'm'))
{
strcpy(friendlyName, "Bluetooth Port ");
strcat(friendlyName, directoryEntry->d_name);
push_back(comPorts, systemName, friendlyName);
}
else if (((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[3] == 'A') && (directoryEntry->d_name[4] == 'M') && (directoryEntry->d_name[5] == 'A')) ||
((ioctl(fd, TIOCGSERIAL, &serialInfo) == 0) && (serialInfo.type != PORT_UNKNOWN)))
{
strcpy(friendlyName, "Physical Port ");
@ -244,6 +253,26 @@ void lastDitchSearchForComPorts(charPairVector* comPorts)
free(systemName);
free(friendlyName);
}
else if ((strlen(directoryEntry->d_name) >= 6) && (directoryEntry->d_name[0] == 'r') && (directoryEntry->d_name[1] == 'f') && (directoryEntry->d_name[2] == 'c') &&
(directoryEntry->d_name[3] == 'o') && (directoryEntry->d_name[4] == 'm') && (directoryEntry->d_name[5] == 'm'))
{
// Determine system name of port
char* systemName = (char*)malloc(256);
strcpy(systemName, "/dev/");
strcat(systemName, directoryEntry->d_name);
// Set static friendly name
char* friendlyName = (char*)malloc(256);
strcpy(friendlyName, "Bluetooth-Based Serial Port");
// Determine if port is already in the list, and add it if not
if (!keyExists(comPorts, systemName))
push_back(comPorts, systemName, friendlyName);
// Clean up memory
free(systemName);
free(friendlyName);
}
directoryEntry = readdir(directoryIterator);
}

View File

@ -2,10 +2,10 @@
* LinuxHelperFunctions.h
*
* Created on: Mar 10, 2015
* Last Updated on: Mar 10, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* SerialPort_Linux.c
*
* Created on: Feb 25, 2012
* Last Updated on: Oct 09, 2015
* Last Updated on: Dec 05, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -145,6 +145,11 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
return serialPortFD;
}
//JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_associateNativeHandle(JNIEnv *env, jobject obj, jlong serialPortFD)
//{
// ;
//}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
@ -336,6 +341,15 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(J
return numBytesAvailable;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAwaitingWrite(JNIEnv *env, jobject obj, jlong serialPortFD)
{
int numBytesToWrite = -1;
if (serialPortFD > 0)
ioctl(serialPortFD, TIOCOUTQ, &numBytesToWrite);
return numBytesToWrite;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv *env, jobject obj, jlong serialPortFD, jbyteArray buffer, jlong bytesToRead)
{
// Get port handle and read timeout from Java class
@ -438,6 +452,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
jbyte *writeBuffer = (*env)->GetByteArrayElements(env, buffer, 0);
int numBytesWritten, result = 0;
// Set the DTR line to high if using RS-422
//ioctl(serialPortFD, TIOCMGET, &result);
//result |= TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
// Write to port
do { numBytesWritten = write(serialPortFD, writeBuffer, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
if (numBytesWritten == -1)
@ -451,6 +470,20 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
// Clear the DTR line if using RS-422
//#ifdef TIOCSERGETLSR
//do
//{
//result = ioctl(serialPortFD, TIOCSERGETLSR);
//if (result != TIOCSER_TEMT)
//usleep(100);
//} while (result != TIOCSER_TEMT);
//#endif
//ioctl(serialPortFD, TIOCMGET, &result);
//result &= ~TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
//do { result = tcflush(serialPortFD, TCIFLUSH); } while ((result < 0) && (errno == EINTR));
// Return number of bytes written if successful
(*env)->ReleaseByteArrayElements(env, buffer, writeBuffer, JNI_ABORT);
return numBytesWritten;

View File

@ -2,10 +2,10 @@
* OSXHelperFunctions.c
*
* Created on: Jul 1, 2015
* Last Updated on: Jul 1, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* OSXHelperFunctions.h
*
* Created on: Jul 1, 2015
* Last Updated on: Jul 1, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* SerialPort_OSX.c
*
* Created on: Feb 25, 2012
* Last Updated on: Oct 09, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: May 19, 2015
* Last Updated on: Dec 05, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -516,6 +516,20 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(J
return (jint)numBytesAvailable;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAwaitingWrite(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return -1;
COMSTAT commInfo;
if (!ClearCommError(serialPortHandle, NULL, &commInfo))
return -1;
DWORD numBytesToWrite = commInfo.cbOutQue;
return (jint)numBytesToWrite;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv *env, jobject obj, jlong serialPortFD, jbyteArray buffer, jlong bytesToRead)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
@ -578,6 +592,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
DWORD numBytesWritten = 0;
BOOL result;
// Set the DTR line to high if using RS-422
//EscapeCommFunction(serialPortHandle, SETDTR);
// Write to serial port
if ((result = WriteFile(serialPortHandle, writeBuffer, bytesToWrite, &numBytesWritten, &overlappedStruct)) == FALSE)
{
@ -601,6 +618,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
}
}
// Clear the DTR line if using RS-422
//COMSTAT commInfo;
//do { ClearCommError(serialPortHandle, NULL, &commInfo); } while (commInfo.cbOutQue > 0);
//EscapeCommFunction(serialPortHandle, CLRDTR);
// Return number of bytes written if successful
CloseHandle(overlappedStruct.hEvent);
env->ReleaseByteArrayElements(buffer, writeBuffer, JNI_ABORT);

View File

@ -2,10 +2,10 @@
* WindowsHelperFunctions.c
*
* Created on: May 05, 2015
* Last Updated on: May 05, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* WindowsHelperFunctions.h
*
* Created on: May 05, 2015
* Last Updated on: May 05, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*

View File

@ -2,10 +2,10 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Jul 18, 2015
* Last Updated on: Dec 05, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -40,7 +40,7 @@ import java.util.Date;
* 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;
* @version 1.3.12
* @version 1.4.0
* @see java.io.InputStream
* @see java.io.OutputStream
*/
@ -254,6 +254,40 @@ public final class SerialPort
return serialPort;
}
// /**
// * Allocates a {@link SerialPort} object corresponding to a previously opened serial port file descriptor.
// * <p>
// * Using this method to create a {@link SerialPort} object may not allow you to change some port characteristics
// * like baud rate, flow control, or parity, depending on the methodology that was used to initially open the native
// * file descriptor.
// * <p>
// * Use of this constructor is not recommended <b>except</b> for use with Android-specific applications. In this
// * case, you can use the Android USB Host APIs to allow the user to grant permission to use the port, and then
// * return the native file descriptor to this library via the Android UsbDeviceConnection.getFileDescriptor() method.
// * <p>
// * For non-Android applications, any of the other constructors are recommended; however, this method may still be
// * used if you have a specific need for it in your application.
// *
// * @param nativeFileDescriptor A pre-opened file descriptor corresponding to the serial port you would like to use with this library.
// * @return A SerialPort object.
// */
/* static public SerialPort getCommPort(long nativeFileDescriptor)
{
// Create SerialPort object and associate it with the native file descriptor
SerialPort serialPort = new SerialPort();
if (!serialPort.associateNativeHandle(nativeFileDescriptor))
serialPort.comPort = "UNKNOWN";
serialPort.portString = "User-Specified Port";
serialPort.portHandle = nativeFileDescriptor;
serialPort.isOpened = true;
// Create the Input/OutputStream interfaces
serialPort.inputStream = serialPort.new SerialPortInputStream();
serialPort.outputStream = serialPort.new SerialPortOutputStream();
return serialPort;
}*/
// Parity Values
static final public int NO_PARITY = 0;
static final public int ODD_PARITY = 1;
@ -387,12 +421,14 @@ public final class SerialPort
private static native void initializeLibrary(); // Initializes the JNI code
private static native void uninitializeLibrary(); // Un-initializes the JNI code
private final native long openPortNative(); // Opens serial port
//private final native boolean associateNativeHandle(long portHandle);// Associates an already opened file descriptor with this class
private final native boolean closePortNative(long portHandle); // Closes serial port
private final native boolean configPort(long portHandle); // Changes/sets serial port parameters as defined by this class
private final native boolean configTimeouts(long portHandle); // Changes/sets serial port timeouts as defined by this class
private final native boolean configEventFlags(long portHandle); // Changes/sets which serial events to listen for as defined by this class
private final native int waitForEvent(long portHandle); // Waits for serial event to occur as specified in eventFlags
private final native int bytesAvailable(long portHandle); // Returns number of bytes available for reading
private final native int bytesAwaitingWrite(long portHandle); // Returns number of bytes still waiting to be written
private final native int readBytes(long portHandle, byte[] buffer, long bytesToRead); // Reads bytes from serial port
private final native int writeBytes(long portHandle, byte[] buffer, long bytesToWrite); // Write bytes to serial port
@ -403,6 +439,13 @@ public final class SerialPort
* @return The number of bytes currently available to be read, or -1 if the port is not open.
*/
public final int bytesAvailable() { return bytesAvailable(portHandle); }
/**
* Returns the number of bytes still waiting to be written in the device's output queue.
*
* @return The number of bytes currently waiting to be written, or -1 if the port is not open.
*/
public final int bytesAwaitingWrite() { return bytesAwaitingWrite(portHandle); }
/**
* Reads up to <i>bytesToRead</i> raw data bytes from the serial port and stores them in the buffer.

View File

@ -5,7 +5,7 @@
* Last Updated on: Mar 12, 2015
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -31,7 +31,7 @@ import java.util.EventListener;
* This interface must be implemented to enable simple event-based serial port I/O.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 1.3.12
* @version 1.4.0
* @see java.util.EventListener
*/
public interface SerialPortDataListener extends EventListener
@ -61,6 +61,8 @@ public interface SerialPortDataListener extends EventListener
/**
* Called whenever one of the serial port events specified by the {@link #getListeningEvents()} method occurs.
* <p>
* Note that your implementation of this function should always perform as little data processing as possible, as the speed at which this callback will fire is at the mercy of the underlying operating system. If you need to collect a large amount of data, application-level buffering should be implemented and data processing should occur on a separate thread.
*
* @param event A {@link SerialPortEvent} object containing information and/or data about the serial event that occurred.
* @see SerialPortEvent

View File

@ -5,7 +5,7 @@
* Last Updated on: Mar 12, 2015
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -31,7 +31,7 @@ import java.util.EventObject;
* This class describes an asynchronous serial port event.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 1.3.12
* @version 1.4.0
* @see java.util.EventObject
*/
public final class SerialPortEvent extends EventObject

View File

@ -5,7 +5,7 @@
* Last Updated on: Mar 12, 2015
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -31,7 +31,7 @@ package com.fazecast.jSerialComm;
* <i>Note</i>: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context.
*
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 1.3.12
* @version 1.4.0
* @see com.fazecast.jSerialComm.SerialPortDataListener
* @see java.util.EventListener
*/

View File

@ -2,10 +2,10 @@
* SerialPortTest.java
*
* Created on: Feb 27, 2015
* Last Updated on: May 05, 2015
* Last Updated on: Dec 05, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2015 Fazecast, Inc.
* Copyright (C) 2012-2017 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library.
*
* @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt;
* @version 1.3.12
* @version 1.4.0
* @see java.io.InputStream
* @see java.io.OutputStream
*/
@ -64,12 +64,20 @@ public class SerialPortTest
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) {}
try {
Scanner inputScanner = new Scanner(System.in);
serialPortChoice = inputScanner.nextInt();
inputScanner.close();
} catch (Exception e) {}
if (serialPortChoice == -1)
{
String serialPortDescriptor = "";
System.out.print("\nSpecify your desired serial port descriptor: ");
try { serialPortDescriptor = (new Scanner(System.in)).nextLine(); } catch (Exception e) {}
try {
Scanner inputScanner = new Scanner(System.in);
serialPortDescriptor = inputScanner.nextLine();
inputScanner.close();
} catch (Exception e) {}
ubxPort = SerialPort.getCommPort(serialPortDescriptor);
}
else
@ -180,7 +188,11 @@ public class SerialPortTest
SerialPort ubxPort2;
System.out.print("\nChoose your second desired serial port, or enter -1 to skip this test: ");
serialPortChoice = 0;
try { serialPortChoice = (new Scanner(System.in)).nextInt(); } catch (Exception e) {}
try {
Scanner inputScanner = new Scanner(System.in);
serialPortChoice = inputScanner.nextInt();
inputScanner.close();
} catch (Exception e) {}
if (serialPortChoice != -1)
{
ubxPort2 = ports[serialPortChoice];