1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 14:12:10 +02:00

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
This commit is contained in:
Doug Schaefer 2017-01-19 14:45:05 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent 0ade726481
commit db3d5974e6
5 changed files with 40 additions and 13 deletions

View file

@ -27,6 +27,10 @@ else
ifeq ($(UNAME),Darwin) ifeq ($(UNAME),Darwin)
LIBS = \ LIBS = \
$(OS_DIR)/macosx/x86_64/libserial.jnilib $(OS_DIR)/macosx/x86_64/libserial.jnilib
else
LIBS = \
$(OS_DIR)/win32/x86/serial.dll \
$(OS_DIR)/win32/x86_64/serial.dll
endif endif
endif endif
@ -39,11 +43,11 @@ rebuild: clean all
$(OS_DIR)/win32/x86/serial.dll: serial.c $(OS_DIR)/win32/x86/serial.dll: serial.c
mkdir -p $(dir $@) 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 $(OS_DIR)/win32/x86_64/serial.dll: serial.c
mkdir -p $(dir $@) 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 $(OS_DIR)/linux/x86/libserial.so: serial.c
mkdir -p $(dir $@) mkdir -p $(dir $@)

View file

@ -37,7 +37,7 @@ static void throwIOException(JNIEnv *env, const char *msg)
#ifndef __MINGW32__ #ifndef __MINGW32__
sprintf(buff, "%s: %s", msg, strerror(errno)); sprintf(buff, "%s: %s", msg, strerror(errno));
#else #else
sprintf_s(buff, sizeof(buff), "%s: %d", msg, GetLastError()); sprintf_s(buff, sizeof(buff), "%s (%d)", msg, GetLastError());
#endif #endif
jclass cls = (*env)->FindClass(env, "java/io/IOException"); jclass cls = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, cls, buff); (*env)->ThrowNew(env, cls, buff);
@ -167,10 +167,13 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
OPEN_EXISTING, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED,
NULL); NULL);
(*env)->ReleaseStringChars(env, portName, cportName);
if (handle == INVALID_HANDLE_VALUE) { if (handle == INVALID_HANDLE_VALUE) {
char msg[256]; 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); throwIOException(env, msg);
return -1; return -1;
} }
@ -275,8 +278,7 @@ JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong jhandle, jb
throwIOException(env, "Error reading from port"); throwIOException(env, "Error reading from port");
CloseHandle(olp.hEvent); CloseHandle(olp.hEvent);
return -1; return -1;
} } else {
else {
switch (WaitForSingleObject(olp.hEvent, INFINITE)) { switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
if (!GetOverlappedResult(handle, &olp, &nread, FALSE)) { if (!GetOverlappedResult(handle, &olp, &nread, FALSE)) {
@ -401,7 +403,7 @@ JNIEXPORT jstring FUNC(getPortName)(JNIEnv *env, jclass cls, jint i)
HKEY key; HKEY key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &key) != ERROR_SUCCESS) { 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; return NULL;
} }

View file

@ -95,7 +95,7 @@ public class SerialPort {
return n; return n;
} else { } else {
n = read1(handle, b, off, len); n = read1(handle, b, off, len);
if (n < 0 && isPaused) { if (n <= 0 && isPaused) {
synchronized (pauseMutex) { synchronized (pauseMutex) {
while (isPaused) { while (isPaused) {
try { try {
@ -232,9 +232,18 @@ public class SerialPort {
} else if (osName.startsWith("Windows")) { //$NON-NLS-1$ } else if (osName.startsWith("Windows")) { //$NON-NLS-1$
List<String> ports = new ArrayList<>(); List<String> ports = new ArrayList<>();
int i = 0; int i = 0;
for (String name = getPortName(i++); name != null; name = getPortName(i++)) { String name = null;
ports.add(name); 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()]); return ports.toArray(new String[ports.size()]);
} else { } else {
return new String[0]; return new String[0];
@ -265,19 +274,31 @@ public class SerialPort {
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
if (isOpen) { if (isOpen) {
close0(handle);
isOpen = false; isOpen = false;
close0(handle);
handle = 0; 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() { public boolean isOpen() {
return isOpen; return isOpen;
} }
public void pause() throws IOException { public void pause() throws IOException {
isPaused = true; isPaused = true;
close0(handle); 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 { public void resume() throws IOException {