From 6a3cc212dba64c4ec024f9cbc7581814508b2417 Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Wed, 23 Mar 2016 16:51:07 -0500 Subject: [PATCH] Fix bug where reconfiguring ports after setting timeouts negates the timeout settings. --- INSTALL | 14 +++---- src/main/c/Linux/SerialPort_Linux.c | 6 +-- src/main/c/OSX/SerialPort_OSX.c | 6 +-- .../com/fazecast/jSerialComm/SerialPort.java | 38 +++++++++++-------- .../jSerialComm/SerialPortDataListener.java | 2 +- .../fazecast/jSerialComm/SerialPortEvent.java | 2 +- .../jSerialComm/SerialPortPacketListener.java | 2 +- .../fazecast/jSerialComm/SerialPortTest.java | 2 +- 8 files changed, 40 insertions(+), 32 deletions(-) diff --git a/INSTALL b/INSTALL index 33cdb27..fc3b808 100644 --- a/INSTALL +++ b/INSTALL @@ -123,29 +123,29 @@ Maven: com.fazecast jSerialComm - 1.3.11 + [1.0.0,2.0.0) Ivy: - + Groovy: -@Grab(group='com.fazecast', module='jSerialComm', version='1.3.11') +@Grab(group='com.fazecast', module='jSerialComm', version='[1.0.0,2.0.0)') Gradle: -compile 'com.fazecast:jSerialComm:1.3.11' +compile 'com.fazecast:jSerialComm:[1.0.0,2.0.0)' Buildr: -compile.with 'com.fazecast:jSerialComm:jar:1.3.11' +compile.with 'com.fazecast:jSerialComm:jar:[1.0.0,2.0.0)' Scala/SBT: -libraryDependencies += "com.fazecast" % "jSerialComm" % "1.3.11" +libraryDependencies += "com.fazecast" % "jSerialComm" % "[1.0.0,2.0.0)" Leiningen: -[com.fazecast/jSerialComm "1.3.11"] +[com.fazecast/jSerialComm "[1.0.0,2.0.0)"] diff --git a/src/main/c/Linux/SerialPort_Linux.c b/src/main/c/Linux/SerialPort_Linux.c index 8bbeb15..0eb5b53 100644 --- a/src/main/c/Linux/SerialPort_Linux.c +++ b/src/main/c/Linux/SerialPort_Linux.c @@ -119,6 +119,9 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative( int serialPortFD = -1; if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0) { + // Clear any serial port flags + fcntl(serialPortFD, F_SETFL, 0); + // Configure the port parameters and timeouts if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD)) @@ -154,9 +157,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB; tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR); - // Clear any serial port flags - fcntl(serialPortFD, F_SETFL, 0); - // Set raw-mode to allow the use of tcsetattr() and ioctl() tcgetattr(serialPortFD, &options); cfmakeraw(&options); diff --git a/src/main/c/OSX/SerialPort_OSX.c b/src/main/c/OSX/SerialPort_OSX.c index 2bcacb1..bd4431f 100644 --- a/src/main/c/OSX/SerialPort_OSX.c +++ b/src/main/c/OSX/SerialPort_OSX.c @@ -170,6 +170,9 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative( int serialPortFD = -1; if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0) { + // Clear any serial port flags + fcntl(serialPortFD, F_SETFL, 0); + // Configure the port parameters and timeouts if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configFlowControl(env, obj, serialPortFD) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD)) @@ -204,9 +207,6 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB; tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR); - // Clear any serial port flags - fcntl(serialPortFD, F_SETFL, 0); - // Set raw-mode to allow the use of tcsetattr() and ioctl() tcgetattr(serialPortFD, &options); cfmakeraw(&options); diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index be04217..6068913 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -40,7 +40,7 @@ import java.util.Date; * This class provides native access to serial ports and devices without requiring external libraries or tools. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.3.11 + * @version 1.3.12 * @see java.io.InputStream * @see java.io.OutputStream */ @@ -165,21 +165,29 @@ public final class SerialPort try { InputStream fileContents = SerialPort.class.getResourceAsStream("/" + libraryPath + "/" + fileName); - FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary); - byte transferBuffer[] = new byte[4096]; - int numBytesRead; - - while ((numBytesRead = fileContents.read(transferBuffer)) > 0) - destinationFileContents.write(transferBuffer, 0, numBytesRead); - - fileContents.close(); - destinationFileContents.close(); + if (fileContents == null) + { + System.err.println("Could not locate or access the native jSerialComm shared library."); + System.err.println("If you are using multiple projects with interdependencies, you may need to fix your build settings to ensure that library resources are copied properly."); + } + else + { + FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary); + byte transferBuffer[] = new byte[4096]; + int numBytesRead; + + while ((numBytesRead = fileContents.read(transferBuffer)) > 0) + destinationFileContents.write(transferBuffer, 0, numBytesRead); + + fileContents.close(); + destinationFileContents.close(); + + // Load native library + System.load(tempFileName); + initializeLibrary(); + } } catch (Exception e) { e.printStackTrace(); } - - // Load native library - System.load(tempFileName); - initializeLibrary(); } // Static symbolic link testing function @@ -337,7 +345,7 @@ public final class SerialPort } } - try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } + try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } if ((portHandle = openPortNative()) > 0) { inputStream = new SerialPortInputStream(); diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java b/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java index e92b42b..c90d5a0 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java @@ -31,7 +31,7 @@ import java.util.EventListener; * This interface must be implemented to enable simple event-based serial port I/O. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.3.11 + * @version 1.3.12 * @see java.util.EventListener */ public interface SerialPortDataListener extends EventListener diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java b/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java index 21b05c3..2f415d0 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java @@ -31,7 +31,7 @@ import java.util.EventObject; * This class describes an asynchronous serial port event. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.3.11 + * @version 1.3.12 * @see java.util.EventObject */ public final class SerialPortEvent extends EventObject diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java b/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java index 8546d21..210454b 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java @@ -31,7 +31,7 @@ package com.fazecast.jSerialComm; * Note: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.3.11 + * @version 1.3.12 * @see com.fazecast.jSerialComm.SerialPortDataListener * @see java.util.EventListener */ diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index 92e800b..93e0aa2 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -32,7 +32,7 @@ import java.util.Scanner; * This class provides a test case for the jSerialComm library. * * @author Will Hedgecock <will.hedgecock@gmail.com> - * @version 1.3.11 + * @version 1.3.12 * @see java.io.InputStream * @see java.io.OutputStream */