Skip to content

Commit

Permalink
Remove special functions to preset/preclear RTS and DTR
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Dec 2, 2022
1 parent 8064359 commit bde01ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 251 deletions.
114 changes: 1 addition & 113 deletions src/main/c/Posix/SerialPort_Posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
130 changes: 1 addition & 129 deletions src/main/c/Windows/SerialPort_Windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/fazecast/jSerialComm/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit bde01ef

Please sign in to comment.