|
@@ -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
|
}
|