Browse Source

Tried fixing windows library problems.

Serial library is still not loaded correctly on the PCs in our school...
Thomas Buck 12 years ago
parent
commit
bf5c1a3943

+ 23
- 3
CubeControl/Frame.java View File

@@ -94,6 +94,7 @@ public class Frame extends JFrame implements ListSelectionListener {
94 94
 	public cubeWorker worker = new cubeWorker(this);
95 95
 	private boolean fileSelected = false;
96 96
 	private Frame thisFrame;
97
+	private static Frame recentFrame;
97 98
 
98 99
 	// Ende Variablen
99 100
 
@@ -141,6 +142,23 @@ public class Frame extends JFrame implements ListSelectionListener {
141 142
 		JOptionPane.showOptionDialog(this, msg, title, JOptionPane.YES_OPTION,
142 143
 			JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
143 144
 	}
145
+
146
+	/**
147
+	 * Show an error message to the user via the most recent Frame.
148
+	 * @param s Error Message
149
+	 */
150
+	public static void errorMessageStat(String s) {
151
+		recentFrame.errorMessage(s);
152
+	}
153
+
154
+	/**
155
+	 *	Show an error message to the user via the most recent Frame.
156
+	 * @param title Title of message box
157
+	 * @param msg Error message.
158
+	 */
159
+	public static void errorMessageStat(String title, String msg) {
160
+		recentFrame.errorMessage(title, msg);
161
+	}
144 162
 	
145 163
 	/**
146 164
 	 * ListSelectionListener that updates Anim & Frame List
@@ -209,6 +227,7 @@ public class Frame extends JFrame implements ListSelectionListener {
209 227
 	public Frame(String title) {
210 228
 		super(title);
211 229
 		thisFrame = this;
230
+		recentFrame = this;
212 231
 		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
213 232
 
214 233
 		// Build serial port selection
@@ -241,9 +260,10 @@ public class Frame extends JFrame implements ListSelectionListener {
241 260
 		int frameHeight = 646;
242 261
 		setSize(frameWidth, frameHeight);
243 262
 		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
244
-		int x = (d.width - getSize().width) / 2;
245
-		int y = (d.height - getSize().height) / 2;
246
-		setLocation(x, y);
263
+		// int x = (d.width - getSize().width) / 2;
264
+		// int y = (d.height - getSize().height) / 2;
265
+		// setLocation(x, y);
266
+		setLocation(10, 10);
247 267
 		Container cp = getContentPane();
248 268
 		cp.setLayout(null);
249 269
 		Font font = new Font("Dialog", Font.PLAIN, 13);

+ 68
- 16
CubeControl/HelperUtility.java View File

@@ -62,32 +62,42 @@ public class HelperUtility {
62 62
 
63 63
 	/**
64 64
 	 * Puts library to temp dir and loads to memory
65
+	 * @param path Put in front of file name
66
+	 * @param Complete name of file to load (w/ lib & .dll)
65 67
 	 */
66 68
 	private static void loadLib(String path, String name) {
67 69
 		try {
68 70
 			// have to use a stream
69 71
 			InputStream in = HelperUtility.class.getResourceAsStream(name);
70
-			// System.out.println("Input Stream: " + in);
71
-			File fileOut = new File(System.getProperty("java.io.tmpdir") + "/"
72
-					+ path + name);
72
+			File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + name);
73 73
 			OutputStream out = new FileOutputStream(fileOut);
74 74
 			ReadableByteChannel inChannel = Channels.newChannel(in);
75 75
 			WritableByteChannel outChannel = Channels.newChannel(out);
76 76
 			fastChannelCopy(inChannel, outChannel);
77 77
 			inChannel.close();
78 78
 			outChannel.close();
79
-			System.load(fileOut.toString());
80
-			System.out.println("Loaded Serial Library!");
79
+			path = fileOut.getPath();
80
+			try {
81
+				System.load(path);
82
+			} catch (UnsatisfiedLinkError e) {
83
+				System.out.println("ERROR: Library does not exist!");
84
+				return;
85
+			} catch (SecurityException e) {
86
+				System.out.println("ERROR: Not allowed to load Library!");
87
+				return;
88
+			} catch (NullPointerException e) {
89
+				System.out.println("ERROR: Library name is null!");
90
+				return;
91
+			}
92
+			System.out.println("Loaded Serial Library at \"" + path + "\"");
81 93
 		} catch (Exception e) {
82
-			System.out.println("Failed to load Serial Library:");
94
+			System.out.println("ERROR: Failed to load Serial Library:");
83 95
 			e.printStackTrace();
84
-			System.exit(1);
85 96
 		}
86 97
 	}
87 98
 
88 99
 	// http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
89
-	private static void fastChannelCopy(ReadableByteChannel src,
90
-			WritableByteChannel dest) throws IOException {
100
+	private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
91 101
 		ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
92 102
 		while (src.read(buffer) != -1) {
93 103
 			// prepare the buffer to be drained
@@ -142,12 +152,16 @@ public class HelperUtility {
142 152
 				return getThePorts("tty.");
143 153
 			} else {
144 154
 				return getThePorts("tty");
145
-			}
155
+			} 
146 156
 		} catch (Exception e) {
147
-			// Unsatisfied linker error:
148
-			// Serial.dll was probably not found
149 157
 			System.out.println("Exception: " + e.toString());
150 158
 			return null;
159
+		} catch (UnsatisfiedLinkError e) {
160
+			System.out.println("ERROR: Library not loaded! (getPorts)");
161
+			// System.out.println(e.toString());
162
+			// Show error message because this is called in the initializer.
163
+			Frame.errorMessageStat("Could not load Serial Library!\nNo Serial functionality available!");
164
+			return null;
151 165
 		}
152 166
 	}
153 167
 
@@ -160,12 +174,31 @@ public class HelperUtility {
160 174
 	 * @param name
161 175
 	 *            Port to open
162 176
 	 */
163
-	public static native boolean openPort(String name);
177
+	public static boolean openPort(String name) {
178
+		try {
179
+			return openPortNative(name);
180
+		} catch (UnsatisfiedLinkError e) {
181
+			System.out.println("ERROR: Library not loaded! (openPort)");
182
+			// System.out.println(e.toString());
183
+			return false;
184
+		}
185
+	}
186
+
187
+	private static native boolean openPortNative(String name);
164 188
 
165 189
 	/**
166 190
 	 * Close Connection to port
167 191
 	 */
168
-	public static native void closePort();
192
+	public static void closePort() {
193
+		try {
194
+			closePortNative();
195
+		} catch (UnsatisfiedLinkError e) {
196
+			System.out.println("ERROR: Library not loaded! (closePort)");
197
+			// System.out.println(e.toString());
198
+		}
199
+	}
200
+
201
+	private static native void closePortNative();
169 202
 
170 203
 	/**
171 204
 	 * Read data from Cube
@@ -174,7 +207,17 @@ public class HelperUtility {
174 207
 	 *            Amount of data to read
175 208
 	 * @return Data read
176 209
 	 */
177
-	public static native short[] readData(int length);
210
+	public static short[] readData(int length) {
211
+		try {
212
+			return readDataNative(length);
213
+		} catch (UnsatisfiedLinkError e) {
214
+			System.out.println("ERROR: Library not loaded! (readData)");
215
+			// System.out.println(e.toString());
216
+			return null;
217
+		}
218
+	}
219
+
220
+	private static native short[] readDataNative(int length);
178 221
 
179 222
 	/**
180 223
 	 * Write data to Cube
@@ -184,5 +227,14 @@ public class HelperUtility {
184 227
 	 * @param length
185 228
 	 *            Length of data
186 229
 	 */
187
-	public static native void writeData(short[] data, int length);
230
+	public static void writeData(short[] data, int length) {
231
+		try {
232
+			writeDataNative(data, length);
233
+		} catch (UnsatisfiedLinkError e) {
234
+			System.out.println("ERROR: Library not loaded! (writeData)");
235
+			// System.out.println(e.toString());
236
+		}
237
+	}
238
+
239
+	private static native void writeDataNative(short[] data, int length);
188 240
 }

+ 2
- 2
CubeControl/libSerial/makefile View File

@@ -7,8 +7,8 @@ RM = del
7 7
 else
8 8
 ifdef SYSTEMROOT
9 9
 # Looks like Cygwin or Mingw shell
10
-HEADERPATH = C:\Programme\Java\jdk1.6.0_31\include
11
-HEADERPATH += -IC:\Programme\Java\jdk1.6.0_31\include\win32
10
+HEADERPATH = C:\Programme\Java\jdk1.6.0_07\include
11
+HEADERPATH += -IC:\Programme\Java\jdk1.6.0_07\include\win32
12 12
 RM = rm -rf
13 13
 else
14 14
 RM = rm -f

+ 4
- 4
CubeControl/libSerial/serialHelper.c View File

@@ -79,7 +79,7 @@ JNIEXPORT jstring JNICALL Java_HelperUtility_getThePorts(JNIEnv *env, jclass cla
79 79
 	return ret;
80 80
 }
81 81
 
82
-JNIEXPORT jshortArray JNICALL Java_HelperUtility_readData(JNIEnv *env, jclass class, jint length) {
82
+JNIEXPORT jshortArray JNICALL Java_HelperUtility_readDataNative(JNIEnv *env, jclass class, jint length) {
83 83
 	jshortArray arr = (*env)->NewShortArray(env, length);
84 84
 	int toBeRead = 0, read, i;
85 85
 	char *data = (char *)malloc(length * sizeof(char));
@@ -98,7 +98,7 @@ JNIEXPORT jshortArray JNICALL Java_HelperUtility_readData(JNIEnv *env, jclass cl
98 98
 	return arr;
99 99
 }
100 100
 
101
-JNIEXPORT void JNICALL Java_HelperUtility_writeData(JNIEnv *env, jclass class, jshortArray data, jint length) {
101
+JNIEXPORT void JNICALL Java_HelperUtility_writeDataNative(JNIEnv *env, jclass class, jshortArray data, jint length) {
102 102
 	int toWrite = length, written = 0, now, i;
103 103
 	char *dat = (char *)malloc(length * sizeof(char));
104 104
 	jshort *dat2 = (jshort *)malloc(length * sizeof(jshort));
@@ -114,11 +114,11 @@ JNIEXPORT void JNICALL Java_HelperUtility_writeData(JNIEnv *env, jclass class, j
114 114
 	}
115 115
 }
116 116
 
117
-JNIEXPORT void JNICALL Java_HelperUtility_closePort(JNIEnv * env, jclass class) {
117
+JNIEXPORT void JNICALL Java_HelperUtility_closePortNative(JNIEnv * env, jclass class) {
118 118
 	serialClose();
119 119
 }
120 120
 
121
-JNIEXPORT jboolean JNICALL Java_HelperUtility_openPort(JNIEnv *env, jclass class, jstring name) {
121
+JNIEXPORT jboolean JNICALL Java_HelperUtility_openPortNative(JNIEnv *env, jclass class, jstring name) {
122 122
 	jboolean isCopy;
123 123
 	const char *path = (*env)->GetStringUTFChars(env, name, &isCopy);
124 124
 	int ret = serialOpen((char *)path);

Loading…
Cancel
Save