Implement model control line signaling on Linux/OSX.

This commit is contained in:
hedgecrw85 2018-01-03 12:41:00 -06:00
parent 68de6c05a7
commit 14c9d4eff7
5 changed files with 180 additions and 36 deletions

View File

@ -530,4 +530,50 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return numBytesWritten;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCSBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCCBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
#endif

View File

@ -2,10 +2,10 @@
* SerialPort_Linux.c
*
* Created on: Feb 25, 2012
* Last Updated on: Dec 05, 2016
* Last Updated on: Jan 03, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2017 Fazecast, Inc.
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -489,4 +489,50 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return numBytesWritten;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCSBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCCBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
#endif

View File

@ -2,10 +2,10 @@
* SerialPort_OSX.c
*
* Created on: Feb 25, 2012
* Last Updated on: Mar 25, 2016
* Last Updated on: Jan 03, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2017 Fazecast, Inc.
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -515,4 +515,50 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return numBytesWritten;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCSBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCCBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
#endif

View File

@ -2,10 +2,10 @@
* SerialPort_Windows.c
*
* Created on: Feb 25, 2012
* Last Updated on: Dec 05, 2016
* Last Updated on: Jan 03, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2017 Fazecast, Inc.
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
@ -263,8 +263,9 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
else
{
// Close the port if there was a problem setting the parameters
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
}
@ -455,8 +456,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
{
// Problem reading, close port
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetLongField(obj, serialPortHandleField, -1l);
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
@ -552,8 +554,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
if (GetLastError() != ERROR_IO_PENDING) // Problem occurred
{
// Problem reading, close port
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetLongField(obj, serialPortHandleField, -1l);
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
@ -561,8 +564,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesRead, TRUE)) == FALSE)
{
// Problem reading, close port
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetLongField(obj, serialPortHandleField, -1l);
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
@ -601,8 +605,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
if (GetLastError() != ERROR_IO_PENDING)
{
// Problem writing, close port
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetLongField(obj, serialPortHandleField, -1l);
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
@ -610,8 +615,9 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
else if ((result = GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesWritten, TRUE)) == FALSE)
{
// Problem reading, close port
int numRetries = 10;
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
while (!CloseHandle(serialPortHandle));
while (!CloseHandle(serialPortHandle) && (numRetries-- > 0));
serialPortHandle = INVALID_HANDLE_VALUE;
env->SetLongField(obj, serialPortHandleField, -1l);
env->SetBooleanField(obj, isOpenedField, JNI_FALSE);
@ -629,52 +635,52 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return (result == TRUE) ? numBytesWritten : -1;
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return SetCommBreak(serialPortHandle);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return ClearCommBreak(serialPortHandle);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return EscapeCommFunction(serialPortHandle, SETRTS);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return EscapeCommFunction(serialPortHandle, CLRRTS);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return EscapeCommFunction(serialPortHandle, SETDTR);
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
JNIEXPORT jboolean 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);
return JNI_FALSE;
return EscapeCommFunction(serialPortHandle, CLRDTR);
}
#endif

View File

@ -450,12 +450,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 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 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);
private final native boolean setBreak(long portHandle); // Set BREAK status on serial line
private final native boolean clearBreak(long portHandle); // Clear BREAK status on serial line
private final native boolean setRTS(long portHandle); // Set RTS line to 1
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
/**
* Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately