Add calls to get RTS/DTR/RI line statuses, suppress terminal output from pre-setting DTR/RTS lines, and set estimated device xmit/receive buffer sizes from runtime code

This commit is contained in:
hedgecrw85 2019-10-15 14:31:48 -05:00
parent 6899d23887
commit d2243293b7
29 changed files with 166 additions and 26 deletions

View File

@ -2,7 +2,7 @@
* SerialPort_Android.c
*
* Created on: Mar 13, 2015
* Last Updated on: Jul 08, 2019
* Last Updated on: Oct 15, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -259,6 +259,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
}*/
// Attempt to set the transmit buffer size and any necessary custom baud rates
(*env)->SetIntField(env, obj, receiveDeviceQueueSizeField, sysconf(_SC_PAGESIZE));
ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
@ -612,7 +613,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JN
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -626,7 +627,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -656,7 +657,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JN
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -670,7 +671,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -701,4 +702,28 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEn
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CAR);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_DTR);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_RTS);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRI(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_RI);
}
#endif

View File

@ -261,6 +261,30 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD
(JNIEnv *, jobject, jlong);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: getDTR
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDTR
(JNIEnv *, jobject, jlong);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: getRTS
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRTS
(JNIEnv *, jobject, jlong);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: getRI
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRI
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif

View File

@ -5,14 +5,14 @@ COMPILE_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
COMPILE_ARM64 := aarch64-unknown-linux-gnueabi-gcc
COMPILE_SOLARIS_X86 := x86_64-sun-solaris2.10-gcc
COMPILE_SOLARIS_SPARC := sparc-sun-solaris2.10-gcc
COMPILE_APPLE := /usr/local/bin/gcc-8
COMPILE_APPLE := /usr/local/bin/gcc-9
LINK := gcc
LINK_ARM32 := arm32-unknown-linux-gnueabi-gcc
LINK_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
LINK_ARM64 := aarch64-unknown-linux-gnueabi-gcc
LINK_SOLARIS_X86 := x86_64-sun-solaris2.10-gcc
LINK_SOLARIS_SPARC := sparc-sun-solaris2.10-gcc
LINK_APPLE := /usr/local/bin/gcc-8
LINK_APPLE := /usr/local/bin/gcc-9
CFLAGS := -fPIC -Os -flto -static-libgcc -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
CFLAGS_POSIX := -fuse-linker-plugin
CFLAGS_APPLE :=
@ -55,6 +55,7 @@ OBJECTSsolarisSparc32 := $(BUILD_DIR)/solaris_sparc32/SerialPort_Posix.o $(BUIL
OBJECTSsolarisSparc64 := $(BUILD_DIR)/solaris_sparc64/SerialPort_Posix.o $(BUILD_DIR)/solaris_sparc64/PosixHelperFunctions.o
OBJECTSosx32 := $(BUILD_DIR)/osx_x86/SerialPort_Posix.o $(BUILD_DIR)/osx_x86/PosixHelperFunctions.o
OBJECTSosx64 := $(BUILD_DIR)/osx_x86_64/SerialPort_Posix.o $(BUILD_DIR)/osx_x86_64/PosixHelperFunctions.o
CPATH_APPLE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
FLAGSlinux32 := -m32
FLAGSlinux64 := -m64
FLAGSarmv5 := -marm -mthumb-interwork -march=armv5 -mfloat-abi=soft
@ -252,9 +253,9 @@ $(BUILD_DIR)/solaris_sparc32/%.o : %.c
$(BUILD_DIR)/solaris_sparc64/%.o : %.c
$(COMPILE_SOLARIS_SPARC) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSsolarisSparc64) -c $< -o $@
$(BUILD_DIR)/osx_x86/%.o : %.c
$(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx32) -c $< -o $@
CPATH=$(CPATH_APPLE) $(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx32) -c $< -o $@
$(BUILD_DIR)/osx_x86_64/%.o : %.c
$(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx64) -c $< -o $@
CPATH=$(CPATH_APPLE) $(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx64) -c $< -o $@
# Rule to build JNI header file
$(JNI_HEADER) : $(JAVA_CLASS)

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Jul 08, 2019
* Last Updated on: Oct 15, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -361,7 +361,10 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
}
#else
(*env)->SetIntField(env, obj, sendDeviceQueueSizeField, sysconf(_SC_PAGESIZE));
#endif
(*env)->SetIntField(env, obj, receiveDeviceQueueSizeField, sysconf(_SC_PAGESIZE));
if (nonStandardBaudRate)
setBaudRateCustom(serialPortFD, baudRate);
@ -721,9 +724,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JN
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s hupcl", portName);
sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);
@ -739,9 +742,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s -hupcl", portName);
sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);
@ -773,9 +776,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JN
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s hupcl", portName);
sprintf(commandString, "stty -f %s hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);
@ -791,9 +794,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl >>/dev/null 2>&1", portName);
#else
sprintf(commandString, "stty -f %s -hupcl", portName);
sprintf(commandString, "stty -f %s -hupcl >>/dev/null 2>&1", portName);
#endif
int result = system(commandString);
@ -824,3 +827,27 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEn
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CAR);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_DTR);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_RTS);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRI(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_RI);
}

View File

@ -2,7 +2,7 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Jul 08, 2019
* Last Updated on: Oct 15, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -804,7 +804,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JN
if (comPort != NULL)
{
char commandString[32];
sprintf(commandString, "MODE %s rts=on", comPort + 1);
sprintf(commandString, "cmd.exe /C \"MODE %s rts=on > nul 2>&1\"", comPort + 1);
result = system(commandString);
}
@ -823,7 +823,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(
if (comPort != NULL)
{
char commandString[32];
sprintf(commandString, "MODE %s rts=off", comPort + 1);
sprintf(commandString, "cmd.exe /C \"MODE %s rts=off > nul 2>&1\"", comPort + 1);
result = system(commandString);
}
@ -858,7 +858,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JN
if (comPort != NULL)
{
char commandString[32];
sprintf(commandString, "MODE %s dtr=on", comPort + 1);
sprintf(commandString, "cmd.exe /C \"MODE %s dtr=on > nul 2>&1\"", comPort + 1);
result = system(commandString);
}
@ -877,7 +877,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(
if (comPort != NULL)
{
char commandString[32];
sprintf(commandString, "MODE %s dtr=off", comPort + 1);
sprintf(commandString, "cmd.exe /C \"MODE %s dtr=off > nul 2>&1\"", comPort + 1);
result = system(commandString);
}
@ -912,4 +912,29 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDCD(JNIEn
return GetCommModemStatus(serialPortHandle, &modemStatus) && (modemStatus & MS_RLSD_ON);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return JNI_FALSE;
return env->GetBooleanField(obj, isDtrEnabledField);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE)
return JNI_FALSE;
return env->GetBooleanField(obj, isRtsEnabledField);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getRI(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_RING_ON);
}
#endif

View File

@ -2,7 +2,7 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Sep 03, 2019
* Last Updated on: Oct 15, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -86,6 +86,7 @@ public final class SerialPort
getpropProcess.waitFor();
buildProperties.close();
}
catch (InterruptedException e) { Thread.currentThread().interrupt(); }
catch (Exception e) { e.printStackTrace(); }
if (libraryPath.isEmpty())
@ -419,7 +420,7 @@ public final class SerialPort
{
if (process == null)
return false;
try { process.waitFor(); } catch (InterruptedException e) { return false; }
try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
try { process.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getErrorStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
@ -524,6 +525,9 @@ public final class SerialPort
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
private final native boolean getDCD(long portHandle); // Returns whether the DCD signal is 1
private final native boolean getDTR(long portHandle); // Returns whether the DTR signal is 1
private final native boolean getRTS(long portHandle); // Returns whether the RTS signal is 1
private final native boolean getRI(long portHandle); // Returns whether the RI signal is 1
/**
* Returns the number of bytes available without blocking if {@link #readBytes(byte[], long)} were to be called immediately
@ -603,6 +607,18 @@ public final class SerialPort
* @return The number of bytes successfully written, or -1 if there was an error writing to the port.
*/
public final int writeBytes(byte[] buffer, long bytesToWrite, long offset) { return writeBytes(portHandle, buffer, bytesToWrite, offset); }
/**
* Returns the underlying transmit buffer size used by the serial port device driver. The device or operating system may choose to misrepresent this value.
* @return The underlying device transmit buffer size.
*/
public final int getDeviceWriteBufferSize() { return sendDeviceQueueSize; }
/**
* Returns the underlying receive buffer size used by the serial port device driver. The device or operating system may choose to misrepresent this value.
* @return The underlying device receive buffer size.
*/
public final int getDeviceReadBufferSize() { return receiveDeviceQueueSize; }
/**
* Sets the BREAK signal on the serial control line.
@ -674,6 +690,28 @@ public final class SerialPort
*/
public final boolean getDCD() { return getDCD(portHandle); }
/**
* Returns whether the DTR line is currently asserted.
* <p>
* Note that polling this line's status is not supported on Windows, so results may be incorrect.
* @return Whether or not the DTR line is asserted.
*/
public final boolean getDTR() { return getDTR(portHandle); }
/**
* Returns whether the RTS line is currently asserted.
* <p>
* Note that polling this line's status is not supported on Windows, so results may be incorrect.
* @return Whether or not the RTS line is asserted.
*/
public final boolean getRTS() { return getRTS(portHandle); }
/**
* Returns whether the RI line is currently asserted.
* @return Whether or not the RI line is asserted.
*/
public final boolean getRI() { return getRI(portHandle); }
// Default Constructor
private SerialPort() {}
@ -1268,7 +1306,7 @@ public final class SerialPort
eventFlags = 0;
configEventFlags(portHandle);
eventFlags = oldEventFlags;
try { serialEventThread.join(); } catch (InterruptedException e) {}
try { serialEventThread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
serialEventThread = null;
}