Skip to main content
summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorDoug Schaefer2017-01-19 14:45:05 -0500
committerGerrit Code Review @ Eclipse.org2017-01-20 09:59:51 -0500
commit26569fbf726bba32493454d9ffb42a5ad6f49a2f (patch)
tree618050ba24a831ab1bf620d7c6bfe170e2c4808f /native
parent89d1521d24ab1a66a3bfa4321ef618b5de748cde (diff)
downloadorg.eclipse.cdt-26569fbf726bba32493454d9ffb42a5ad6f49a2f.tar.gz
org.eclipse.cdt-26569fbf726bba32493454d9ffb42a5ad6f49a2f.tar.xz
org.eclipse.cdt-26569fbf726bba32493454d9ffb42a5ad6f49a2f.zip
Fixes for Windows Serial Port.
Cleaned up some error messages. Especially when a system has no serial ports. Also added a delay when ports are closed since some serial port drivers (FDTI in particular) take a bit longer to properly close. Especially important in pause situations where we want to use the serial port for some other purpose right away. Change-Id: Ifc9b7171682962e0aed8e9ed9ebf5472fc9e8206 (cherry picked from commit db3d5974e66ac443ee773e062f62fe8537933f3c)
Diffstat (limited to 'native')
-rw-r--r--native/org.eclipse.cdt.native.serial/jni/Makefile8
-rw-r--r--native/org.eclipse.cdt.native.serial/jni/serial.c12
-rwxr-xr-xnative/org.eclipse.cdt.native.serial/os/win32/x86/serial.dllbin95288 -> 336648 bytes
-rwxr-xr-xnative/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dllbin125772 -> 383701 bytes
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java33
5 files changed, 40 insertions, 13 deletions
diff --git a/native/org.eclipse.cdt.native.serial/jni/Makefile b/native/org.eclipse.cdt.native.serial/jni/Makefile
index 926ee23d5b..6dd2a2fdb9 100644
--- a/native/org.eclipse.cdt.native.serial/jni/Makefile
+++ b/native/org.eclipse.cdt.native.serial/jni/Makefile
@@ -27,6 +27,10 @@ else
ifeq ($(UNAME),Darwin)
LIBS = \
$(OS_DIR)/macosx/x86_64/libserial.jnilib
+else
+LIBS = \
+ $(OS_DIR)/win32/x86/serial.dll \
+ $(OS_DIR)/win32/x86_64/serial.dll
endif
endif
@@ -39,11 +43,11 @@ rebuild: clean all
$(OS_DIR)/win32/x86/serial.dll: serial.c
mkdir -p $(dir $@)
- i686-w64-mingw32-gcc -Wl,--kill-at -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/win32 -shared -o $@ serial.c
+ i686-w64-mingw32-gcc -Wl,--kill-at -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/win32" -shared -o $@ serial.c
$(OS_DIR)/win32/x86_64/serial.dll: serial.c
mkdir -p $(dir $@)
- x86_64-w64-mingw32-gcc -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/win32 -shared -o $@ serial.c
+ x86_64-w64-mingw32-gcc -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/win32" -shared -o $@ serial.c
$(OS_DIR)/linux/x86/libserial.so: serial.c
mkdir -p $(dir $@)
diff --git a/native/org.eclipse.cdt.native.serial/jni/serial.c b/native/org.eclipse.cdt.native.serial/jni/serial.c
index 43f6a79af6..1ffacffc93 100644
--- a/native/org.eclipse.cdt.native.serial/jni/serial.c
+++ b/native/org.eclipse.cdt.native.serial/jni/serial.c
@@ -37,7 +37,7 @@ static void throwIOException(JNIEnv *env, const char *msg)
#ifndef __MINGW32__
sprintf(buff, "%s: %s", msg, strerror(errno));
#else
- sprintf_s(buff, sizeof(buff), "%s: %d", msg, GetLastError());
+ sprintf_s(buff, sizeof(buff), "%s (%d)", msg, GetLastError());
#endif
jclass cls = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, cls, buff);
@@ -167,10 +167,13 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
+ (*env)->ReleaseStringChars(env, portName, cportName);
if (handle == INVALID_HANDLE_VALUE) {
char msg[256];
- sprintf_s(msg, sizeof(msg), "Error opening %s", cportName);
+ const char * name = (*env)->GetStringUTFChars(env, portName, NULL);
+ sprintf_s(msg, sizeof(msg), "Error opening %s", name);
+ (*env)->ReleaseStringUTFChars(env, portName, name);
throwIOException(env, msg);
return -1;
}
@@ -275,8 +278,7 @@ JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong jhandle, jb
throwIOException(env, "Error reading from port");
CloseHandle(olp.hEvent);
return -1;
- }
- else {
+ } else {
switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
case WAIT_OBJECT_0:
if (!GetOverlappedResult(handle, &olp, &nread, FALSE)) {
@@ -401,7 +403,7 @@ JNIEXPORT jstring FUNC(getPortName)(JNIEnv *env, jclass cls, jint i)
HKEY key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &key) != ERROR_SUCCESS) {
- throwIOException(env, "Can not find registry key");
+ // There are none
return NULL;
}
diff --git a/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll b/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll
index 336c922f8f..52ed0cd3dc 100755
--- a/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll
+++ b/native/org.eclipse.cdt.native.serial/os/win32/x86/serial.dll
Binary files differ
diff --git a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
index 988cc17022..a63fa4db15 100755
--- a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
+++ b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
Binary files differ
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
index 716fc6b15d..681b74c47e 100644
--- a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
@@ -95,7 +95,7 @@ public class SerialPort {
return n;
} else {
n = read1(handle, b, off, len);
- if (n < 0 && isPaused) {
+ if (n <= 0 && isPaused) {
synchronized (pauseMutex) {
while (isPaused) {
try {
@@ -232,9 +232,18 @@ public class SerialPort {
} else if (osName.startsWith("Windows")) { //$NON-NLS-1$
List<String> ports = new ArrayList<>();
int i = 0;
- for (String name = getPortName(i++); name != null; name = getPortName(i++)) {
- ports.add(name);
- }
+ String name = null;
+ do {
+ try {
+ name = getPortName(i++);
+ if (name != null) {
+ ports.add(name);
+ }
+ } catch (IOException e) {
+ // TODO log the exception
+ e.printStackTrace();
+ }
+ } while (name != null);
return ports.toArray(new String[ports.size()]);
} else {
return new String[0];
@@ -265,19 +274,31 @@ public class SerialPort {
public synchronized void close() throws IOException {
if (isOpen) {
- close0(handle);
isOpen = false;
+ close0(handle);
handle = 0;
+ try {
+ // Sleep for a second since some serial ports take a while to actually close
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ // nothing to do
+ }
}
}
public boolean isOpen() {
return isOpen;
}
-
+
public void pause() throws IOException {
isPaused = true;
close0(handle);
+ try {
+ // Sleep for a second since some serial ports take a while to actually close
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ // nothing to do
+ }
}
public void resume() throws IOException {

Back to the top