Add support for set/clear BREAK/RTS/DTR signals in Windows.

This commit is contained in:
Josh Lubawy 2016-10-04 18:36:58 -07:00 committed by Josh Lubawy
parent 46790676e6
commit 6953a70fe8
8 changed files with 68 additions and 5 deletions

View File

@ -629,4 +629,52 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return (result == TRUE) ? numBytesWritten : -1; return (result == TRUE) ? numBytesWritten : -1;
} }
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
SetCommBreak(serialPortHandle);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
ClearCommBreak(serialPortHandle);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
EscapeCommFunction(serialPortHandle, SETRTS);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
EscapeCommFunction(serialPortHandle, CLRRTS);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
EscapeCommFunction(serialPortHandle, SETDTR);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return;
EscapeCommFunction(serialPortHandle, CLRDTR);
}
#endif #endif

View File

@ -175,13 +175,13 @@ public final class SerialPort
FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary); FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary);
byte transferBuffer[] = new byte[4096]; byte transferBuffer[] = new byte[4096];
int numBytesRead; int numBytesRead;
while ((numBytesRead = fileContents.read(transferBuffer)) > 0) while ((numBytesRead = fileContents.read(transferBuffer)) > 0)
destinationFileContents.write(transferBuffer, 0, numBytesRead); destinationFileContents.write(transferBuffer, 0, numBytesRead);
fileContents.close(); fileContents.close();
destinationFileContents.close(); destinationFileContents.close();
// Load native library // Load native library
System.load(tempFileName); System.load(tempFileName);
initializeLibrary(); initializeLibrary();
@ -189,7 +189,7 @@ public final class SerialPort
} }
catch (Exception e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
} }
// Static symbolic link testing function // Static symbolic link testing function
private static boolean isSymbolicLink(File file) throws IOException private static boolean isSymbolicLink(File file) throws IOException
{ {
@ -227,7 +227,7 @@ public final class SerialPort
// Resolve home directory ~ // Resolve home directory ~
if (portDescriptor.startsWith("~" + File.separator)) if (portDescriptor.startsWith("~" + File.separator))
portDescriptor = System.getProperty("user.home") + portDescriptor.substring(1); portDescriptor = System.getProperty("user.home") + portDescriptor.substring(1);
// See what kind of descriptor was passed in // See what kind of descriptor was passed in
if (isWindows) if (isWindows)
portDescriptor = "\\\\.\\" + portDescriptor.substring(portDescriptor.lastIndexOf('\\')+1); portDescriptor = "\\\\.\\" + portDescriptor.substring(portDescriptor.lastIndexOf('\\')+1);
@ -448,6 +448,12 @@ public final class SerialPort
private final native int bytesAwaitingWrite(long portHandle); // Returns number of bytes still waiting to be written 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 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 private final native int writeBytes(long portHandle, byte[] buffer, long bytesToWrite); // Write bytes to serial port
private final native void setBreak(long portHandle);
private final native void clearBreak(long portHandle);
private final native void setRTS(long portHandle);
private final native void clearRTS(long portHandle);
private final native void setDTR(long portHandle);
private final native void clearDTR(long portHandle);
/** /**
* Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately * Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately
@ -496,6 +502,15 @@ public final class SerialPort
*/ */
public final int writeBytes(byte[] buffer, long bytesToWrite) { return writeBytes(portHandle, buffer, bytesToWrite); } public final int writeBytes(byte[] buffer, long bytesToWrite) { return writeBytes(portHandle, buffer, bytesToWrite); }
public final void setBreak() { setBreak(portHandle); }
public final void clearBreak() { clearBreak(portHandle); }
public final void setRTS() { setRTS(portHandle); }
public final void clearRTS() { clearRTS(portHandle); }
public final void setDTR() { setDTR(portHandle); }
public final void clearDTR() { clearDTR(portHandle); }
// Default Constructor // Default Constructor
private SerialPort() {} private SerialPort() {}