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:
parent
0ade726481
commit
db3d5974e6
5 changed files with 40 additions and 13 deletions
|
@ -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 $@)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -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++)) {
|
||||
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,9 +274,15 @@ 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -278,6 +293,12 @@ public class SerialPort {
|
|||
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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue