35
35
import org .osgi .framework .FrameworkUtil ;
36
36
import org .osgi .framework .ServiceRegistration ;
37
37
38
- import it .baeyens .arduino .common .ArduinoConst ;
39
38
import it .baeyens .arduino .common .Common ;
39
+ import it .baeyens .arduino .common .Const ;
40
+ import jssc .SerialNativeInterface ;
40
41
import jssc .SerialPort ;
41
42
import jssc .SerialPortEvent ;
42
43
import jssc .SerialPortEventListener ;
@@ -55,19 +56,22 @@ public class Serial implements SerialPortEventListener {
55
56
// the static class would have an object that could be closed
56
57
57
58
/**
58
- * General error reporting, all correlated here just in case I think of something slightly more intelligent to do.
59
+ * General error reporting, all correlated here just in case I think of
60
+ * something slightly more intelligent to do.
59
61
*/
60
62
static public void errorMessage (String where , Throwable e ) {
61
- Common .log (new Status (IStatus .WARNING , ArduinoConst .CORE_PLUGIN_ID , "Error inside Serial. " + where , e )); //$NON-NLS-1$
63
+ Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID , "Error inside Serial. " + where , e )); //$NON-NLS-1$
62
64
63
65
}
64
66
65
67
/**
66
- * If this just hangs and never completes on Windows, it may be because the DLL doesn't have its exec bit set. Why the hell that'd be the case,
67
- * who knows.
68
+ * If this just hangs and never completes on Windows, it may be because the
69
+ * DLL doesn't have its exec bit set. Why the hell that'd be the case, who
70
+ * knows.
68
71
*/
69
72
public static Vector <String > list () {
70
73
try {
74
+ SerialNativeInterface tt = new SerialNativeInterface ();
71
75
String [] portNames ;
72
76
String OS = System .getProperty ("os.name" ).toLowerCase (); //$NON-NLS-1$
73
77
if (OS .indexOf ("mac" ) >= 0 ) { //$NON-NLS-1$
@@ -77,8 +81,9 @@ public static Vector<String> list() {
77
81
}
78
82
return new Vector <>(Arrays .asList (portNames ));
79
83
} catch (Error e ) {
80
- Common .log (new Status (IStatus .ERROR , ArduinoConst .CORE_PLUGIN_ID ,
81
- "There is a config problem on your system.\n For more detail see https://github.com/jantje/arduino-eclipse-plugin/issues/252" , e )); //$NON-NLS-1$
84
+ Common .log (new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID ,
85
+ "There is a config problem on your system.\n For more detail see https://github.com/jantje/arduino-eclipse-plugin/issues/252" , //$NON-NLS-1$
86
+ e ));
82
87
Vector <String > ret = new Vector <>();
83
88
ret .add ("config error:" ); //$NON-NLS-1$
84
89
ret .add ("see https://github.com/jantje/arduino-eclipse-plugin/issues/252" ); //$NON-NLS-1$
@@ -146,7 +151,8 @@ public void removeListener(MessageConsumer consumer) {
146
151
}
147
152
148
153
/**
149
- * Returns the number of bytes that have been read from serial and are waiting to be dealt with by the user.
154
+ * Returns the number of bytes that have been read from serial and are
155
+ * waiting to be dealt with by the user.
150
156
*/
151
157
public int available () {
152
158
return (this .bufferLast - this .bufferIndex );
@@ -180,23 +186,25 @@ public void connect(int maxTries) {
180
186
// handle exception
181
187
if (++count == maxTries ) {
182
188
if (SerialPortException .TYPE_PORT_BUSY .equals (e .getExceptionType ())) {
183
- Common .log (new Status (IStatus .ERROR , ArduinoConst .CORE_PLUGIN_ID , "Serial port " + this .PortName //$NON-NLS-1$
184
- + " already in use. Try quiting any programs that may be using it" , e )); //$NON-NLS-1$
189
+ Common .log (new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID ,
190
+ "Serial port " + this .PortName //$NON-NLS-1$
191
+ + " already in use. Try quiting any programs that may be using it" , //$NON-NLS-1$
192
+ e ));
185
193
} else if (SerialPortException .TYPE_PORT_NOT_FOUND .equals (e .getExceptionType ())) {
186
- Common .log (
187
- new Status (IStatus .ERROR , ArduinoConst .CORE_PLUGIN_ID ,
188
- "Serial port " + this .PortName //$NON-NLS-1$
189
- + " not found. Did you select the right one from the project properties -> Arduino -> Arduino?" , //$NON-NLS-1$
190
- e ));
194
+ Common .log (new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID , "Serial port " //$NON-NLS-1$
195
+ + this .PortName
196
+ + " not found. Did you select the right one from the project properties -> Arduino -> Arduino?" , //$NON-NLS-1$
197
+ e ));
191
198
} else {
192
- Common .log (new Status (IStatus .ERROR , ArduinoConst .CORE_PLUGIN_ID , "Error opening serial port " + this .PortName , e )); //$NON-NLS-1$
199
+ Common .log (new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID ,
200
+ "Error opening serial port " + this .PortName , e )); //$NON-NLS-1$
193
201
}
194
202
return ;
195
203
}
196
204
try {
197
205
Thread .sleep (200 );
198
206
} catch (InterruptedException e1 ) {
199
- Common .log (new Status (IStatus .WARNING , ArduinoConst .CORE_PLUGIN_ID , "Sleep failed" , e1 )); //$NON-NLS-1$
207
+ Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID , "Sleep failed" , e1 )); //$NON-NLS-1$
200
208
}
201
209
}
202
210
// If an exception was thrown, delete port variable
@@ -246,7 +254,8 @@ private void notifyConsumersOfEvent(String message) {
246
254
}
247
255
248
256
/**
249
- * Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there was no byte (although the user should
257
+ * Returns a number between 0 and 255 for the next byte that's waiting in
258
+ * the buffer. Returns -1 if there was no byte (although the user should
250
259
* first check available() to see if things are ready to avoid this)
251
260
*/
252
261
public int read () {
@@ -264,8 +273,9 @@ public int read() {
264
273
}
265
274
266
275
/**
267
- * Return a byte array of anything that's in the serial buffer. Not particularly memory/speed efficient, because it creates a byte array on each
268
- * read, but it's easier to use than readBytes(byte b[]) (see below).
276
+ * Return a byte array of anything that's in the serial buffer. Not
277
+ * particularly memory/speed efficient, because it creates a byte array on
278
+ * each read, but it's easier to use than readBytes(byte b[]) (see below).
269
279
*/
270
280
public byte [] readBytes () {
271
281
if (this .bufferIndex == this .bufferLast )
@@ -283,10 +293,12 @@ public byte[] readBytes() {
283
293
}
284
294
285
295
/**
286
- * Grab whatever is in the serial buffer, and stuff it into a byte buffer passed in by the user. This is more memory/time efficient than
296
+ * Grab whatever is in the serial buffer, and stuff it into a byte buffer
297
+ * passed in by the user. This is more memory/time efficient than
287
298
* readBytes() returning a byte[] array.
288
299
*
289
- * Returns an int for how many bytes were read. If more bytes are available than can fit into the byte array, only those that will fit are read.
300
+ * Returns an int for how many bytes were read. If more bytes are available
301
+ * than can fit into the byte array, only those that will fit are read.
290
302
*/
291
303
public int readBytes (byte outgoing []) {
292
304
if (this .bufferIndex == this .bufferLast )
@@ -308,7 +320,8 @@ public int readBytes(byte outgoing[]) {
308
320
}
309
321
310
322
/**
311
- * Reads from the serial port into a buffer of bytes up to and including a particular character. If the character isn't in the serial buffer, then
323
+ * Reads from the serial port into a buffer of bytes up to and including a
324
+ * particular character. If the character isn't in the serial buffer, then
312
325
* 'null' is returned.
313
326
*/
314
327
public byte [] readBytesUntil (int interesting ) {
@@ -338,10 +351,12 @@ public byte[] readBytesUntil(int interesting) {
338
351
}
339
352
340
353
/**
341
- * Reads from the serial port into a buffer of bytes until a particular character. If the character isn't in the serial buffer, then 'null' is
354
+ * Reads from the serial port into a buffer of bytes until a particular
355
+ * character. If the character isn't in the serial buffer, then 'null' is
342
356
* returned.
343
357
*
344
- * If outgoing[] is not big enough, then -1 is returned, and an error message is printed on the console. If nothing is in the buffer, zero is
358
+ * If outgoing[] is not big enough, then -1 is returned, and an error
359
+ * message is printed on the console. If nothing is in the buffer, zero is
345
360
* returned. If 'interesting' byte is not in the buffer, then 0 is returned.
346
361
*/
347
362
public int readBytesUntil (int interesting , byte outgoing []) {
@@ -362,8 +377,10 @@ public int readBytesUntil(int interesting, byte outgoing[]) {
362
377
363
378
int length = found - this .bufferIndex + 1 ;
364
379
if (length > outgoing .length ) {
365
- Common .log (new Status (IStatus .WARNING , ArduinoConst .CORE_PLUGIN_ID , "readBytesUntil() byte buffer is too small for the " + length //$NON-NLS-1$
366
- + " bytes up to and including char " + interesting , null )); //$NON-NLS-1$
380
+ Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID ,
381
+ "readBytesUntil() byte buffer is too small for the " + length //$NON-NLS-1$
382
+ + " bytes up to and including char " + interesting , //$NON-NLS-1$
383
+ null ));
367
384
return -1 ;
368
385
}
369
386
// byte outgoing[] = new byte[length];
@@ -379,7 +396,8 @@ public int readBytesUntil(int interesting, byte outgoing[]) {
379
396
}
380
397
381
398
/**
382
- * Returns the next byte in the buffer as a char. Returns -1, or 0xffff, if nothing is there.
399
+ * Returns the next byte in the buffer as a char. Returns -1, or 0xffff, if
400
+ * nothing is there.
383
401
*/
384
402
public char readChar () {
385
403
if (this .bufferIndex == this .bufferLast )
@@ -388,9 +406,11 @@ public char readChar() {
388
406
}
389
407
390
408
/**
391
- * Return whatever has been read from the serial port so far as a String. It assumes that the incoming characters are ASCII.
409
+ * Return whatever has been read from the serial port so far as a String. It
410
+ * assumes that the incoming characters are ASCII.
392
411
*
393
- * If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte
412
+ * If you want to move Unicode data, you can first convert the String to a
413
+ * byte stream in the representation of your choice (i.e. UTF8 or two-byte
394
414
* Unicode data), and send it as a byte array.
395
415
*/
396
416
public String readString () {
@@ -400,9 +420,11 @@ public String readString() {
400
420
}
401
421
402
422
/**
403
- * Combination of readBytesUntil and readString. See caveats in each function. Returns null if it still hasn't found what you're looking for.
423
+ * Combination of readBytesUntil and readString. See caveats in each
424
+ * function. Returns null if it still hasn't found what you're looking for.
404
425
*
405
- * If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte
426
+ * If you want to move Unicode data, you can first convert the String to a
427
+ * byte stream in the representation of your choice (i.e. UTF8 or two-byte
406
428
* Unicode data), and send it as a byte array.
407
429
*/
408
430
public String readStringUntil (int interesting ) {
@@ -413,7 +435,8 @@ public String readStringUntil(int interesting) {
413
435
}
414
436
415
437
public void registerService () {
416
- this .fServiceRegistration = FrameworkUtil .getBundle (getClass ()).getBundleContext ().registerService (Serial .class , this , null );
438
+ this .fServiceRegistration = FrameworkUtil .getBundle (getClass ()).getBundleContext ().registerService (Serial .class ,
439
+ this , null );
417
440
}
418
441
419
442
public void reset () {
@@ -529,20 +552,24 @@ public void write(int what) { // will also cover char
529
552
}
530
553
531
554
/**
532
- * Write a String to the output. Note that this doesn't account for Unicode (two bytes per char), nor will it send UTF8 characters.. It assumes
533
- * that you mean to send a byte buffer (most often the case for networking and serial i/o) and will only use the bottom 8 bits of each char in the
555
+ * Write a String to the output. Note that this doesn't account for Unicode
556
+ * (two bytes per char), nor will it send UTF8 characters.. It assumes that
557
+ * you mean to send a byte buffer (most often the case for networking and
558
+ * serial i/o) and will only use the bottom 8 bits of each char in the
534
559
* string. (Meaning that internally it uses String.getBytes)
535
560
*
536
- * If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte
561
+ * If you want to move Unicode data, you can first convert the String to a
562
+ * byte stream in the representation of your choice (i.e. UTF8 or two-byte
537
563
* Unicode data), and send it as a byte array.
538
564
*/
539
565
public void write (String what ) {
540
566
write (what .getBytes ());
541
567
}
542
568
543
569
public void write (String what , String LineEnd ) {
544
- notifyConsumersOfEvent (System .getProperty ("line.separator" ) + ">>Send to " + this .PortName + ": \" " + what + "\" <<" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
545
- + System .getProperty ("line.separator" )); //$NON-NLS-1$
570
+ notifyConsumersOfEvent (
571
+ System .getProperty ("line.separator" ) + ">>Send to " + this .PortName + ": \" " + what + "\" <<" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
572
+ + System .getProperty ("line.separator" )); //$NON-NLS-1$
546
573
write (what .getBytes ());
547
574
if (LineEnd .length () > 0 ) {
548
575
write (LineEnd .getBytes ());
0 commit comments