Skip to content

Commit 7d5442b

Browse files
author
Federico Fissore
committed
Serial class clean up
1 parent c4e1458 commit 7d5442b

File tree

1 file changed

+26
-122
lines changed

1 file changed

+26
-122
lines changed

arduino-core/src/processing/app/Serial.java

+26-122
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222

2323
package processing.app;
2424

25-
import static processing.app.I18n._;
25+
import jssc.SerialPort;
26+
import jssc.SerialPortEvent;
27+
import jssc.SerialPortEventListener;
28+
import jssc.SerialPortException;
2629

2730
import java.io.IOException;
2831
import java.util.Arrays;
2932
import java.util.List;
3033

31-
import jssc.SerialPort;
32-
import jssc.SerialPortEvent;
33-
import jssc.SerialPortEventListener;
34-
import jssc.SerialPortException;
34+
import static processing.app.I18n._;
3535

3636

3737
public class Serial implements SerialPortEventListener {
@@ -45,39 +45,34 @@ public class Serial implements SerialPortEventListener {
4545
// for the classloading problem.. because if code ran again,
4646
// the static class would have an object that could be closed
4747

48-
SerialPort port;
49-
50-
int rate;
51-
int parity;
52-
int databits;
53-
int stopbits;
48+
private SerialPort port;
5449

5550
public Serial() throws SerialException {
5651
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")));
6156
}
6257

6358
public Serial(int irate) throws SerialException {
6459
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")));
6863
}
6964

7065
public Serial(String iname, int irate) throws SerialException {
7166
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")));
7469
}
7570

7671
public Serial(String iname) throws SerialException {
7772
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")));
8176
}
8277

8378
public static boolean touchForCDCReset(String iname) throws SerialException {
@@ -101,27 +96,23 @@ public static boolean touchForCDCReset(String iname) throws SerialException {
10196
}
10297
}
10398

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 {
105100
//if (port != null) port.close();
106101
//this.parent = parent;
107102
//parent.attach(this);
108103

109-
this.rate = irate;
110-
111-
parity = SerialPort.PARITY_NONE;
104+
int parity = SerialPort.PARITY_NONE;
112105
if (iparity == 'E') parity = SerialPort.PARITY_EVEN;
113106
if (iparity == 'O') parity = SerialPort.PARITY_ODD;
114107

115-
this.databits = idatabits;
116-
117-
stopbits = SerialPort.STOPBITS_1;
108+
int stopbits = SerialPort.STOPBITS_1;
118109
if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5;
119110
if (istopbits == 2) stopbits = SerialPort.STOPBITS_2;
120111

121112
try {
122113
port = new SerialPort(iname);
123114
port.openPort();
124-
port.setParams(rate, databits, stopbits, parity, true, true);
115+
port.setParams(irate, idatabits, stopbits, parity, true, true);
125116
port.addEventListener(this);
126117
} catch (SerialPortException e) {
127118
if (e.getPortName().startsWith("/dev") && SerialPortException.TYPE_PERMISSION_DENIED.equals(e.getExceptionType())) {
@@ -171,12 +162,9 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
171162
/**
172163
* This method is intented to be extended to receive messages
173164
* coming from serial port.
174-
*
175-
* @param chars
176-
* @param length
177165
*/
178166
protected void message(char[] chars, int length) {
179-
// Empty
167+
// Empty
180168
}
181169

182170

@@ -192,7 +180,7 @@ public void write(int what) { // will also cover char
192180
}
193181

194182

195-
public void write(byte bytes[]) {
183+
private void write(byte bytes[]) {
196184
try {
197185
port.writeBytes(bytes);
198186
} catch (SerialPortException e) {
@@ -208,7 +196,7 @@ public void write(byte bytes[]) {
208196
* (most often the case for networking and serial i/o) and
209197
* will only use the bottom 8 bits of each char in the string.
210198
* (Meaning that internally it uses String.getBytes)
211-
* <p/>
199+
* <p>
212200
* If you want to move Unicode data, you can first convert the
213201
* String to a byte stream in the representation of your choice
214202
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
@@ -242,92 +230,8 @@ static public List<String> list() {
242230
* General error reporting, all corraled here just in case
243231
* I think of something slightly more intelligent to do.
244232
*/
245-
static public void errorMessage(String where, Throwable e) {
233+
private static void errorMessage(String where, Throwable e) {
246234
System.err.println(I18n.format(_("Error inside Serial.{0}()"), where));
247235
e.printStackTrace();
248236
}
249237
}
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

Comments
 (0)