|
@@ -88,31 +88,16 @@ public class HelperUtility {
|
88
|
88
|
} catch (NullPointerException e) {
|
89
|
89
|
System.out.println("ERROR: Library name is null!");
|
90
|
90
|
return;
|
|
91
|
+ } catch (Exception e) {
|
|
92
|
+ System.out.println("ERROR: " + e.toString());
|
|
93
|
+ return;
|
91
|
94
|
}
|
92
|
95
|
System.out.println("Loaded Serial Library at \"" + path + "\"");
|
|
96
|
+ return;
|
93
|
97
|
} catch (Exception e) {
|
94
|
98
|
System.out.println("ERROR: Failed to load Serial Library:");
|
95
|
99
|
e.printStackTrace();
|
96
|
|
- }
|
97
|
|
- }
|
98
|
|
-
|
99
|
|
- // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
|
100
|
|
- private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
|
101
|
|
- ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
|
102
|
|
- while (src.read(buffer) != -1) {
|
103
|
|
- // prepare the buffer to be drained
|
104
|
|
- buffer.flip();
|
105
|
|
- // write to the channel, may block
|
106
|
|
- dest.write(buffer);
|
107
|
|
- // If partial transfer, shift remainder down
|
108
|
|
- // If buffer is empty, same as doing clear()
|
109
|
|
- buffer.compact();
|
110
|
|
- }
|
111
|
|
- // EOF will leave buffer in fill state
|
112
|
|
- buffer.flip();
|
113
|
|
- // make sure the buffer is fully drained.
|
114
|
|
- while (buffer.hasRemaining()) {
|
115
|
|
- dest.write(buffer);
|
|
100
|
+ return;
|
116
|
101
|
}
|
117
|
102
|
}
|
118
|
103
|
|
|
@@ -122,10 +107,11 @@ public class HelperUtility {
|
122
|
107
|
* @return Array of port names. First entry is "No serial ports!" if no
|
123
|
108
|
* others
|
124
|
109
|
*/
|
|
110
|
+ // Get ports as single String from getPortsOS() and put them in an array
|
125
|
111
|
public static String[] getPorts() {
|
126
|
112
|
String portLines = getPortsOS();
|
127
|
113
|
if (portLines == null) {
|
128
|
|
- String[] ports = { "Select serial port..." };
|
|
114
|
+ String[] ports = { "No serial ports!" };
|
129
|
115
|
return ports;
|
130
|
116
|
} else {
|
131
|
117
|
StringTokenizer sT = new StringTokenizer(portLines, "\n");
|
|
@@ -153,15 +139,9 @@ public class HelperUtility {
|
153
|
139
|
} else {
|
154
|
140
|
return getThePorts("tty");
|
155
|
141
|
}
|
156
|
|
- } catch (Exception e) {
|
157
|
|
- System.out.println("Exception: " + e.toString());
|
158
|
|
- return null;
|
159
|
142
|
} catch (UnsatisfiedLinkError e) {
|
160
|
143
|
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;
|
|
144
|
+ return "Serial Library Error!\n";
|
165
|
145
|
}
|
166
|
146
|
}
|
167
|
147
|
|
|
@@ -179,7 +159,6 @@ public class HelperUtility {
|
179
|
159
|
return openPortNative(name);
|
180
|
160
|
} catch (UnsatisfiedLinkError e) {
|
181
|
161
|
System.out.println("ERROR: Library not loaded! (openPort)");
|
182
|
|
- // System.out.println(e.toString());
|
183
|
162
|
return false;
|
184
|
163
|
}
|
185
|
164
|
}
|
|
@@ -194,7 +173,6 @@ public class HelperUtility {
|
194
|
173
|
closePortNative();
|
195
|
174
|
} catch (UnsatisfiedLinkError e) {
|
196
|
175
|
System.out.println("ERROR: Library not loaded! (closePort)");
|
197
|
|
- // System.out.println(e.toString());
|
198
|
176
|
}
|
199
|
177
|
}
|
200
|
178
|
|
|
@@ -212,7 +190,6 @@ public class HelperUtility {
|
212
|
190
|
return readDataNative(length);
|
213
|
191
|
} catch (UnsatisfiedLinkError e) {
|
214
|
192
|
System.out.println("ERROR: Library not loaded! (readData)");
|
215
|
|
- // System.out.println(e.toString());
|
216
|
193
|
return null;
|
217
|
194
|
}
|
218
|
195
|
}
|
|
@@ -232,9 +209,28 @@ public class HelperUtility {
|
232
|
209
|
writeDataNative(data, length);
|
233
|
210
|
} catch (UnsatisfiedLinkError e) {
|
234
|
211
|
System.out.println("ERROR: Library not loaded! (writeData)");
|
235
|
|
- // System.out.println(e.toString());
|
236
|
212
|
}
|
237
|
213
|
}
|
238
|
214
|
|
239
|
215
|
private static native void writeDataNative(short[] data, int length);
|
|
216
|
+
|
|
217
|
+ // http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
|
|
218
|
+ private static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
|
|
219
|
+ ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
|
|
220
|
+ while (src.read(buffer) != -1) {
|
|
221
|
+ // prepare the buffer to be drained
|
|
222
|
+ buffer.flip();
|
|
223
|
+ // write to the channel, may block
|
|
224
|
+ dest.write(buffer);
|
|
225
|
+ // If partial transfer, shift remainder down
|
|
226
|
+ // If buffer is empty, same as doing clear()
|
|
227
|
+ buffer.compact();
|
|
228
|
+ }
|
|
229
|
+ // EOF will leave buffer in fill state
|
|
230
|
+ buffer.flip();
|
|
231
|
+ // make sure the buffer is fully drained.
|
|
232
|
+ while (buffer.hasRemaining()) {
|
|
233
|
+ dest.write(buffer);
|
|
234
|
+ }
|
|
235
|
+ }
|
240
|
236
|
}
|