From bde01eff9b668bd35ff05401f8470b70da9ae910 Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Fri, 2 Dec 2022 10:47:20 -0600 Subject: [PATCH] Remove special functions to preset/preclear RTS and DTR --- src/main/c/Posix/SerialPort_Posix.c | 114 +-------------- src/main/c/Windows/SerialPort_Windows.c | 130 +----------------- .../com/fazecast/jSerialComm/SerialPort.java | 14 +- 3 files changed, 7 insertions(+), 251 deletions(-) diff --git a/src/main/c/Posix/SerialPort_Posix.c b/src/main/c/Posix/SerialPort_Posix.c index fbb6f116..5b658fa6 100644 --- a/src/main/c/Posix/SerialPort_Posix.c +++ b/src/main/c/Posix/SerialPort_Posix.c @@ -2,7 +2,7 @@ * SerialPort_Posix.c * * Created on: Feb 25, 2012 - * Last Updated on: Dec 01, 2022 + * Last Updated on: Dec 02, 2022 * Author: Will Hedgecock * * Copyright (C) 2012-2022 Fazecast, Inc. @@ -1114,62 +1114,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNI return JNI_TRUE; } -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - - // Fix user permissions so that they can access the port, if allowed - int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0); - if (!userCanAccess && requestElevatedPermissions) - verifyAndSetUserPortGroup(portName); - - // Send a system command to preset the RTS mode of the serial port - char commandString[128]; -#if defined(__linux__) - sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName); -#else - sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName); -#endif - int result = system(commandString); - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result == 0); -} - -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - - // Fix user permissions so that they can access the port, if allowed - int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0); - if (!userCanAccess && requestElevatedPermissions) - verifyAndSetUserPortGroup(portName); - - // Send a system command to preclear the RTS mode of the serial port - char commandString[128]; -#if defined(__linux__) - sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName); -#else - sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName); -#endif - int result = system(commandString); - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result == 0); -} - JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortPointer) { const int modemBits = TIOCM_DTR; @@ -1196,62 +1140,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return JNI_TRUE; } -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - - // Fix user permissions so that they can access the port, if allowed - int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0); - if (!userCanAccess && requestElevatedPermissions) - verifyAndSetUserPortGroup(portName); - - // Send a system command to preset the DTR mode of the serial port - char commandString[128]; -#if defined(__linux__) - sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName); -#else - sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName); -#endif - int result = system(commandString); - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result == 0); -} - -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - unsigned char requestElevatedPermissions = (*env)->GetBooleanField(env, obj, requestElevatedPermissionsField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - - // Fix user permissions so that they can access the port, if allowed - int userCanAccess = (faccessat(0, portName, R_OK | W_OK, AT_EACCESS) == 0); - if (!userCanAccess && requestElevatedPermissions) - verifyAndSetUserPortGroup(portName); - - // Send a system command to preclear the DTR mode of the serial port - char commandString[128]; -#if defined(__linux__) - sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName); -#else - sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName); -#endif - int result = system(commandString); - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result == 0); -} - JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer) { int modemBits = 0; diff --git a/src/main/c/Windows/SerialPort_Windows.c b/src/main/c/Windows/SerialPort_Windows.c index 34a172e2..fa78076a 100644 --- a/src/main/c/Windows/SerialPort_Windows.c +++ b/src/main/c/Windows/SerialPort_Windows.c @@ -2,7 +2,7 @@ * SerialPort_Windows.c * * Created on: Feb 25, 2012 - * Last Updated on: Dec 01, 2022 + * Last Updated on: Dec 02, 2022 * Author: Will Hedgecock * * Copyright (C) 2012-2022 Fazecast, Inc. @@ -1180,70 +1180,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNI return JNI_TRUE; } -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char* comPort = strrchr(portName, '\\'); - - // Try to preset the RTS mode of the COM port using a Windows command - int result = 0; - if (comPort != NULL) - { - STARTUPINFO si; - PROCESS_INFORMATION pi; - char commandString[64]; - ZeroMemory(&si, sizeof(si)); - ZeroMemory(&pi, sizeof(pi)); - si.cb = sizeof(si); - si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; - si.wShowWindow = SW_HIDE; - sprintf(commandString, "mode.com %s rts=on", comPort + 1); - result = CreateProcess(NULL, commandString, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - } - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result != 0); -} - -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char* comPort = strrchr(portName, '\\'); - - // Try to preset the RTS mode of the COM port using a Windows command - int result = 0; - if (comPort != NULL) - { - STARTUPINFO si; - PROCESS_INFORMATION pi; - char commandString[64]; - ZeroMemory(&si, sizeof(si)); - ZeroMemory(&pi, sizeof(pi)); - si.cb = sizeof(si); - si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; - si.wShowWindow = SW_HIDE; - sprintf(commandString, "mode.com %s rts=off", comPort + 1); - result = CreateProcess(NULL, commandString, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - } - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result != 0); -} - JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortPointer) { serialPort *port = (serialPort*)(intptr_t)serialPortPointer; @@ -1268,70 +1204,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNI return JNI_TRUE; } -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char* comPort = strrchr(portName, '\\'); - - // Try to preset the DTR mode of the COM port using a Windows command - int result = 0; - if (comPort != NULL) - { - STARTUPINFO si; - PROCESS_INFORMATION pi; - char commandString[64]; - ZeroMemory(&si, sizeof(si)); - ZeroMemory(&pi, sizeof(pi)); - si.cb = sizeof(si); - si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; - si.wShowWindow = SW_HIDE; - sprintf(commandString, "mode.com %s dtr=on", comPort + 1); - result = CreateProcess(NULL, commandString, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - } - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result != 0); -} - -JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(JNIEnv *env, jobject obj) -{ - jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL); - if (checkJniError(env, __LINE__ - 1)) return JNI_FALSE; - const char* comPort = strrchr(portName, '\\'); - - // Try to preset the DTR mode of the COM port using a Windows command - int result = 0; - if (comPort != NULL) - { - STARTUPINFO si; - PROCESS_INFORMATION pi; - char commandString[64]; - ZeroMemory(&si, sizeof(si)); - ZeroMemory(&pi, sizeof(pi)); - si.cb = sizeof(si); - si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; - si.wShowWindow = SW_HIDE; - sprintf(commandString, "mode.com %s dtr=off", comPort + 1); - result = CreateProcess(NULL, commandString, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); - WaitForSingleObject(pi.hProcess, INFINITE); - CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); - } - - (*env)->ReleaseStringUTFChars(env, portNameJString, portName); - checkJniError(env, __LINE__ - 1); - return (result != 0); -} - JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortPointer) { DWORD modemStatus = 0; diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 5083b92a..04144344 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -2,7 +2,7 @@ * SerialPort.java * * Created on: Feb 25, 2012 - * Last Updated on: Jun 08, 2022 + * Last Updated on: Dec 02, 2022 * Author: Will Hedgecock * * Copyright (C) 2012-2022 Fazecast, Inc. @@ -638,12 +638,8 @@ public final synchronized boolean closePort() private native boolean clearBreak(long portHandle); // Clear BREAK status on serial line private native boolean setRTS(long portHandle); // Set RTS line to 1 private native boolean clearRTS(long portHandle); // Clear RTS line to 0 - private native boolean presetRTS(); // Set RTS line to 1 prior to opening - private native boolean preclearRTS(); // Clear RTS line to 0 prior to opening private native boolean setDTR(long portHandle); // Set DTR line to 1 private native boolean clearDTR(long portHandle); // Clear DTR line to 0 - private native boolean presetDTR(); // Set DTR line to 1 prior to opening - private native boolean preclearDTR(); // Clear DTR line to 0 prior to opening private native boolean getCTS(long portHandle); // Returns whether the CTS signal is 1 private native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1 private native boolean getDCD(long portHandle); // Returns whether the DCD signal is 1 @@ -793,7 +789,7 @@ public final int writeBytes(byte[] buffer, long bytesToWrite, long offset) public final boolean setRTS() { isRtsEnabled = true; - return (portHandle != 0) ? setRTS(portHandle) : presetRTS(); + return (portHandle != 0) ? setRTS(portHandle) : true; } /** @@ -803,7 +799,7 @@ public final boolean setRTS() public final boolean clearRTS() { isRtsEnabled = false; - return (portHandle != 0) ? clearRTS(portHandle) : preclearRTS(); + return (portHandle != 0) ? clearRTS(portHandle) : true; } /** @@ -813,7 +809,7 @@ public final boolean clearRTS() public final boolean setDTR() { isDtrEnabled = true; - return (portHandle != 0) ? setDTR(portHandle) : presetDTR(); + return (portHandle != 0) ? setDTR(portHandle) : true; } /** @@ -823,7 +819,7 @@ public final boolean setDTR() public final boolean clearDTR() { isDtrEnabled = false; - return (portHandle != 0) ? clearDTR(portHandle) : preclearDTR(); + return (portHandle != 0) ? clearDTR(portHandle) : true; } /**