diff --git a/src/main/c/Android/jni/SerialPort_Android.c b/src/main/c/Android/jni/SerialPort_Android.c index 5ba2b2c..e2a95f1 100644 --- a/src/main/c/Android/jni/SerialPort_Android.c +++ b/src/main/c/Android/jni/SerialPort_Android.c @@ -575,4 +575,20 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0); } +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CTS); +} + +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE); +} + #endif diff --git a/src/main/c/Linux/SerialPort_Linux.c b/src/main/c/Linux/SerialPort_Linux.c index 1c16f23..a92c9b6 100644 --- a/src/main/c/Linux/SerialPort_Linux.c +++ b/src/main/c/Linux/SerialPort_Linux.c @@ -534,4 +534,20 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0); } +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CTS); +} + +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE); +} + #endif diff --git a/src/main/c/OSX/SerialPort_OSX.c b/src/main/c/OSX/SerialPort_OSX.c index 977f3a7..3386871 100644 --- a/src/main/c/OSX/SerialPort_OSX.c +++ b/src/main/c/OSX/SerialPort_OSX.c @@ -553,4 +553,20 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0); } +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CTS); +} + +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + if (serialPortFD <= 0) + return JNI_FALSE; + int modemBits = 0; + return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE); +} + #endif diff --git a/src/main/c/Windows/SerialPort_Windows.c b/src/main/c/Windows/SerialPort_Windows.c index 75513f3..241ccbd 100644 --- a/src/main/c/Windows/SerialPort_Windows.c +++ b/src/main/c/Windows/SerialPort_Windows.c @@ -682,4 +682,22 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return EscapeCommFunction(serialPortHandle, CLRDTR); } +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + HANDLE serialPortHandle = (HANDLE)serialPortFD; + if (serialPortHandle == INVALID_HANDLE_VALUE) + return JNI_FALSE; + DWORD modemStatus = 0; + return GetCommModemStatus(serialPortHandle, &modemStatus) && (modemStatus & MS_CTS_ON); +} + +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEnv *env, jobject obj, jlong serialPortFD) +{ + HANDLE serialPortHandle = (HANDLE)serialPortFD; + if (serialPortHandle == INVALID_HANDLE_VALUE) + return JNI_FALSE; + DWORD modemStatus = 0; + return GetCommModemStatus(serialPortHandle, &modemStatus) && (modemStatus & MS_DSR_ON); +} + #endif diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 9263f92..5804884 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -456,6 +456,8 @@ public final class SerialPort private final native boolean clearRTS(long portHandle); // Clear RTS line to 0 private final native boolean setDTR(long portHandle); // Set DTR line to 1 private final native boolean clearDTR(long portHandle); // Clear DTR line to 0 + private final native boolean getCTS(long portHandle); // Returns whether the CTS signal is 1 + private final native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1 /** * Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately @@ -540,6 +542,16 @@ public final class SerialPort */ public final boolean clearDTR() { return clearDTR(portHandle); } + /** + * @return Whether or not the CTS line is asserted. + */ + public final boolean getCTS() { return getCTS(portHandle); } + + /** + * @return Whether or not the DSR line is asserted. + */ + public final boolean getDSR() { return getDSR(portHandle); } + // Default Constructor private SerialPort() {}