22
22
23
23
package processing .app ;
24
24
25
- import static processing .app .I18n ._ ;
25
+ import jssc .SerialPort ;
26
+ import jssc .SerialPortEvent ;
27
+ import jssc .SerialPortEventListener ;
28
+ import jssc .SerialPortException ;
26
29
27
30
import java .io .IOException ;
28
31
import java .util .Arrays ;
29
32
import java .util .List ;
30
33
31
- import jssc .SerialPort ;
32
- import jssc .SerialPortEvent ;
33
- import jssc .SerialPortEventListener ;
34
- import jssc .SerialPortException ;
34
+ import static processing .app .I18n ._ ;
35
35
36
36
37
37
public class Serial implements SerialPortEventListener {
@@ -45,39 +45,34 @@ public class Serial implements SerialPortEventListener {
45
45
// for the classloading problem.. because if code ran again,
46
46
// the static class would have an object that could be closed
47
47
48
- SerialPort port ;
49
-
50
- int rate ;
51
- int parity ;
52
- int databits ;
53
- int stopbits ;
48
+ private SerialPort port ;
54
49
55
50
public Serial () throws SerialException {
56
51
this (PreferencesData .get ("serial.port" ),
57
- PreferencesData .getInteger ("serial.debug_rate" ),
58
- PreferencesData .get ("serial.parity" ).charAt (0 ),
59
- PreferencesData .getInteger ("serial.databits" ),
60
- new Float (PreferencesData .get ("serial.stopbits" )). floatValue ( ));
52
+ PreferencesData .getInteger ("serial.debug_rate" ),
53
+ PreferencesData .get ("serial.parity" ).charAt (0 ),
54
+ PreferencesData .getInteger ("serial.databits" ),
55
+ Float . parseFloat (PreferencesData .get ("serial.stopbits" )));
61
56
}
62
57
63
58
public Serial (int irate ) throws SerialException {
64
59
this (PreferencesData .get ("serial.port" ), irate ,
65
- PreferencesData .get ("serial.parity" ).charAt (0 ),
66
- PreferencesData .getInteger ("serial.databits" ),
67
- new Float (PreferencesData .get ("serial.stopbits" )). floatValue ( ));
60
+ PreferencesData .get ("serial.parity" ).charAt (0 ),
61
+ PreferencesData .getInteger ("serial.databits" ),
62
+ Float . parseFloat (PreferencesData .get ("serial.stopbits" )));
68
63
}
69
64
70
65
public Serial (String iname , int irate ) throws SerialException {
71
66
this (iname , irate , PreferencesData .get ("serial.parity" ).charAt (0 ),
72
- PreferencesData .getInteger ("serial.databits" ),
73
- new Float (PreferencesData .get ("serial.stopbits" )). floatValue ( ));
67
+ PreferencesData .getInteger ("serial.databits" ),
68
+ Float . parseFloat (PreferencesData .get ("serial.stopbits" )));
74
69
}
75
70
76
71
public Serial (String iname ) throws SerialException {
77
72
this (iname , PreferencesData .getInteger ("serial.debug_rate" ),
78
- PreferencesData .get ("serial.parity" ).charAt (0 ),
79
- PreferencesData .getInteger ("serial.databits" ),
80
- new Float (PreferencesData .get ("serial.stopbits" )). floatValue ( ));
73
+ PreferencesData .get ("serial.parity" ).charAt (0 ),
74
+ PreferencesData .getInteger ("serial.databits" ),
75
+ Float . parseFloat (PreferencesData .get ("serial.stopbits" )));
81
76
}
82
77
83
78
public static boolean touchForCDCReset (String iname ) throws SerialException {
@@ -101,27 +96,23 @@ public static boolean touchForCDCReset(String iname) throws SerialException {
101
96
}
102
97
}
103
98
104
- public Serial (String iname , int irate , char iparity , int idatabits , float istopbits ) throws SerialException {
99
+ private Serial (String iname , int irate , char iparity , int idatabits , float istopbits ) throws SerialException {
105
100
//if (port != null) port.close();
106
101
//this.parent = parent;
107
102
//parent.attach(this);
108
103
109
- this .rate = irate ;
110
-
111
- parity = SerialPort .PARITY_NONE ;
104
+ int parity = SerialPort .PARITY_NONE ;
112
105
if (iparity == 'E' ) parity = SerialPort .PARITY_EVEN ;
113
106
if (iparity == 'O' ) parity = SerialPort .PARITY_ODD ;
114
107
115
- this .databits = idatabits ;
116
-
117
- stopbits = SerialPort .STOPBITS_1 ;
108
+ int stopbits = SerialPort .STOPBITS_1 ;
118
109
if (istopbits == 1.5f ) stopbits = SerialPort .STOPBITS_1_5 ;
119
110
if (istopbits == 2 ) stopbits = SerialPort .STOPBITS_2 ;
120
111
121
112
try {
122
113
port = new SerialPort (iname );
123
114
port .openPort ();
124
- port .setParams (rate , databits , stopbits , parity , true , true );
115
+ port .setParams (irate , idatabits , stopbits , parity , true , true );
125
116
port .addEventListener (this );
126
117
} catch (SerialPortException e ) {
127
118
if (e .getPortName ().startsWith ("/dev" ) && SerialPortException .TYPE_PERMISSION_DENIED .equals (e .getExceptionType ())) {
@@ -171,12 +162,9 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
171
162
/**
172
163
* This method is intented to be extended to receive messages
173
164
* coming from serial port.
174
- *
175
- * @param chars
176
- * @param length
177
165
*/
178
166
protected void message (char [] chars , int length ) {
179
- // Empty
167
+ // Empty
180
168
}
181
169
182
170
@@ -192,7 +180,7 @@ public void write(int what) { // will also cover char
192
180
}
193
181
194
182
195
- public void write (byte bytes []) {
183
+ private void write (byte bytes []) {
196
184
try {
197
185
port .writeBytes (bytes );
198
186
} catch (SerialPortException e ) {
@@ -208,7 +196,7 @@ public void write(byte bytes[]) {
208
196
* (most often the case for networking and serial i/o) and
209
197
* will only use the bottom 8 bits of each char in the string.
210
198
* (Meaning that internally it uses String.getBytes)
211
- * <p/ >
199
+ * <p>
212
200
* If you want to move Unicode data, you can first convert the
213
201
* String to a byte stream in the representation of your choice
214
202
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
@@ -242,92 +230,8 @@ static public List<String> list() {
242
230
* General error reporting, all corraled here just in case
243
231
* I think of something slightly more intelligent to do.
244
232
*/
245
- static public void errorMessage (String where , Throwable e ) {
233
+ private static void errorMessage (String where , Throwable e ) {
246
234
System .err .println (I18n .format (_ ("Error inside Serial.{0}()" ), where ));
247
235
e .printStackTrace ();
248
236
}
249
237
}
250
-
251
-
252
- /*
253
- class SerialMenuListener implements ItemListener {
254
- //public SerialMenuListener() { }
255
-
256
- public void itemStateChanged(ItemEvent e) {
257
- int count = serialMenu.getItemCount();
258
- for (int i = 0; i < count; i++) {
259
- ((CheckboxMenuItem)serialMenu.getItem(i)).setState(false);
260
- }
261
- CheckboxMenuItem item = (CheckboxMenuItem)e.getSource();
262
- item.setState(true);
263
- String name = item.getLabel();
264
- //System.out.println(item.getLabel());
265
- PdeBase.properties.put("serial.port", name);
266
- //System.out.println("set to " + get("serial.port"));
267
- }
268
- }
269
- */
270
-
271
-
272
- /*
273
- protected Vector buildPortList() {
274
- // get list of names for serial ports
275
- // have the default port checked (if present)
276
- Vector list = new Vector();
277
-
278
- //SerialMenuListener listener = new SerialMenuListener();
279
- boolean problem = false;
280
-
281
- // if this is failing, it may be because
282
- // lib/javax.comm.properties is missing.
283
- // java is weird about how it searches for java.comm.properties
284
- // so it tends to be very fragile. i.e. quotes in the CLASSPATH
285
- // environment variable will hose things.
286
- try {
287
- //System.out.println("building port list");
288
- Enumeration portList = CommPortIdentifier.getPortIdentifiers();
289
- while (portList.hasMoreElements()) {
290
- CommPortIdentifier portId =
291
- (CommPortIdentifier) portList.nextElement();
292
- //System.out.println(portId);
293
-
294
- if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
295
- //if (portId.getName().equals(port)) {
296
- String name = portId.getName();
297
- //CheckboxMenuItem mi =
298
- //new CheckboxMenuItem(name, name.equals(defaultName));
299
-
300
- //mi.addItemListener(listener);
301
- //serialMenu.add(mi);
302
- list.addElement(name);
303
- }
304
- }
305
- } catch (UnsatisfiedLinkError e) {
306
- e.printStackTrace();
307
- problem = true;
308
-
309
- } catch (Exception e) {
310
- System.out.println("exception building serial menu");
311
- e.printStackTrace();
312
- }
313
-
314
- //if (serialMenu.getItemCount() == 0) {
315
- //System.out.println("dimming serial menu");
316
- //serialMenu.setEnabled(false);
317
- //}
318
-
319
- // only warn them if this is the first time
320
- if (problem && PdeBase.firstTime) {
321
- JOptionPane.showMessageDialog(this, //frame,
322
- "Serial port support not installed.\n" +
323
- "Check the readme for instructions\n" +
324
- "if you need to use the serial port. ",
325
- "Serial Port Warning",
326
- JOptionPane.WARNING_MESSAGE);
327
- }
328
- return list;
329
- }
330
- */
331
-
332
-
333
-
0 commit comments