diff --git a/src/main/cpp/OSX/SerialPort_OSX.cpp b/src/main/cpp/OSX/SerialPort_OSX.cpp index 4e3cabf..3e54bc7 100644 --- a/src/main/cpp/OSX/SerialPort_OSX.cpp +++ b/src/main/cpp/OSX/SerialPort_OSX.cpp @@ -212,7 +212,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; } - else // Non-blocking + else // Non-blocking { flags |= O_NONBLOCK; options.c_cc[VMIN] = 0; @@ -224,6 +224,53 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou return (tcsetattr(serialFD, TCSAFLUSH, &options) == 0) ? JNI_TRUE : JNI_FALSE; } +JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(JNIEnv *env, jobject obj) +{ + jclass serialCommClass = env->GetObjectClass(obj); + int serialFD = (int)env->GetLongField(obj, env->GetFieldID(serialCommClass, "portHandle", "J")); + + // Get event flags from Java class + int eventsToMonitor = env->GetIntField(obj, env->GetFieldID(serialCommClass, "eventFlags", "I")); + + // Change read timeouts if we are monitoring data received + if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0) + { + struct termios options; + tcgetattr(serialFD, &options); + int flags = fcntl(serialFD, F_GETFL); + flags &= ~O_NONBLOCK; + options.c_cc[VMIN] = 0; + options.c_cc[VTIME] = 10; + fcntl(serialFD, F_SETFL, flags); + tcsetattr(serialFD, TCSAFLUSH, &options); + } + else + Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj); + + // Apply changes + return JNI_TRUE; +} + +JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj) +{ + jclass serialCommClass = env->GetObjectClass(obj); + int serialFD = (int)env->GetLongField(obj, env->GetFieldID(serialCommClass, "portHandle", "J")); + + // Initialize the waiting set and the timeouts + struct timeval timeout; + fd_set waitingSet; + FD_ZERO(&waitingSet); + FD_SET(serialFD, &waitingSet); + timeout.tv_sec = 1; + timeout.tv_usec = 0; + + // Wait for a serial port event + int retVal = select(serialFD + 1, &waitingSet, NULL, NULL, &timeout); + if (retVal <= 0) + return 0; + return (FD_ISSET(serialFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0; +} + JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj) { // Close port diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 03b8dce..b5cb079 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -764,7 +764,6 @@ public final class SerialPort throw new IOException("This port appears to have been shutdown or disconnected."); return read(b, 0, b.length); - } @Override diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index 08666e2..e7bdfaf 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -43,7 +43,7 @@ public class SerialPortTest System.out.println("\nPorts:\n"); for (int i = 0; i < ports.length; ++i) System.out.println(" " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName()); - SerialPort ubxPort = ports[1]; + SerialPort ubxPort = ports[0]; byte[] readBuffer = new byte[2048]; System.out.println("\nOpening " + ubxPort.getDescriptivePortName() + ": " + ubxPort.openPort());