mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-15 12:29:26 +01:00
Serial class clean up
This commit is contained in:
parent
c4e1458b1d
commit
7d5442b059
@ -22,16 +22,16 @@
|
|||||||
|
|
||||||
package processing.app;
|
package processing.app;
|
||||||
|
|
||||||
import static processing.app.I18n._;
|
import jssc.SerialPort;
|
||||||
|
import jssc.SerialPortEvent;
|
||||||
|
import jssc.SerialPortEventListener;
|
||||||
|
import jssc.SerialPortException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import jssc.SerialPort;
|
import static processing.app.I18n._;
|
||||||
import jssc.SerialPortEvent;
|
|
||||||
import jssc.SerialPortEventListener;
|
|
||||||
import jssc.SerialPortException;
|
|
||||||
|
|
||||||
|
|
||||||
public class Serial implements SerialPortEventListener {
|
public class Serial implements SerialPortEventListener {
|
||||||
@ -45,39 +45,34 @@ public class Serial implements SerialPortEventListener {
|
|||||||
// for the classloading problem.. because if code ran again,
|
// for the classloading problem.. because if code ran again,
|
||||||
// the static class would have an object that could be closed
|
// the static class would have an object that could be closed
|
||||||
|
|
||||||
SerialPort port;
|
private SerialPort port;
|
||||||
|
|
||||||
int rate;
|
|
||||||
int parity;
|
|
||||||
int databits;
|
|
||||||
int stopbits;
|
|
||||||
|
|
||||||
public Serial() throws SerialException {
|
public Serial() throws SerialException {
|
||||||
this(PreferencesData.get("serial.port"),
|
this(PreferencesData.get("serial.port"),
|
||||||
PreferencesData.getInteger("serial.debug_rate"),
|
PreferencesData.getInteger("serial.debug_rate"),
|
||||||
PreferencesData.get("serial.parity").charAt(0),
|
PreferencesData.get("serial.parity").charAt(0),
|
||||||
PreferencesData.getInteger("serial.databits"),
|
PreferencesData.getInteger("serial.databits"),
|
||||||
new Float(PreferencesData.get("serial.stopbits")).floatValue());
|
Float.parseFloat(PreferencesData.get("serial.stopbits")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Serial(int irate) throws SerialException {
|
public Serial(int irate) throws SerialException {
|
||||||
this(PreferencesData.get("serial.port"), irate,
|
this(PreferencesData.get("serial.port"), irate,
|
||||||
PreferencesData.get("serial.parity").charAt(0),
|
PreferencesData.get("serial.parity").charAt(0),
|
||||||
PreferencesData.getInteger("serial.databits"),
|
PreferencesData.getInteger("serial.databits"),
|
||||||
new Float(PreferencesData.get("serial.stopbits")).floatValue());
|
Float.parseFloat(PreferencesData.get("serial.stopbits")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Serial(String iname, int irate) throws SerialException {
|
public Serial(String iname, int irate) throws SerialException {
|
||||||
this(iname, irate, PreferencesData.get("serial.parity").charAt(0),
|
this(iname, irate, PreferencesData.get("serial.parity").charAt(0),
|
||||||
PreferencesData.getInteger("serial.databits"),
|
PreferencesData.getInteger("serial.databits"),
|
||||||
new Float(PreferencesData.get("serial.stopbits")).floatValue());
|
Float.parseFloat(PreferencesData.get("serial.stopbits")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Serial(String iname) throws SerialException {
|
public Serial(String iname) throws SerialException {
|
||||||
this(iname, PreferencesData.getInteger("serial.debug_rate"),
|
this(iname, PreferencesData.getInteger("serial.debug_rate"),
|
||||||
PreferencesData.get("serial.parity").charAt(0),
|
PreferencesData.get("serial.parity").charAt(0),
|
||||||
PreferencesData.getInteger("serial.databits"),
|
PreferencesData.getInteger("serial.databits"),
|
||||||
new Float(PreferencesData.get("serial.stopbits")).floatValue());
|
Float.parseFloat(PreferencesData.get("serial.stopbits")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean touchForCDCReset(String iname) throws SerialException {
|
public static boolean touchForCDCReset(String iname) throws SerialException {
|
||||||
@ -101,27 +96,23 @@ public class Serial implements SerialPortEventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Serial(String iname, int irate, char iparity, int idatabits, float istopbits) throws SerialException {
|
private Serial(String iname, int irate, char iparity, int idatabits, float istopbits) throws SerialException {
|
||||||
//if (port != null) port.close();
|
//if (port != null) port.close();
|
||||||
//this.parent = parent;
|
//this.parent = parent;
|
||||||
//parent.attach(this);
|
//parent.attach(this);
|
||||||
|
|
||||||
this.rate = irate;
|
int parity = SerialPort.PARITY_NONE;
|
||||||
|
|
||||||
parity = SerialPort.PARITY_NONE;
|
|
||||||
if (iparity == 'E') parity = SerialPort.PARITY_EVEN;
|
if (iparity == 'E') parity = SerialPort.PARITY_EVEN;
|
||||||
if (iparity == 'O') parity = SerialPort.PARITY_ODD;
|
if (iparity == 'O') parity = SerialPort.PARITY_ODD;
|
||||||
|
|
||||||
this.databits = idatabits;
|
int stopbits = SerialPort.STOPBITS_1;
|
||||||
|
|
||||||
stopbits = SerialPort.STOPBITS_1;
|
|
||||||
if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5;
|
if (istopbits == 1.5f) stopbits = SerialPort.STOPBITS_1_5;
|
||||||
if (istopbits == 2) stopbits = SerialPort.STOPBITS_2;
|
if (istopbits == 2) stopbits = SerialPort.STOPBITS_2;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
port = new SerialPort(iname);
|
port = new SerialPort(iname);
|
||||||
port.openPort();
|
port.openPort();
|
||||||
port.setParams(rate, databits, stopbits, parity, true, true);
|
port.setParams(irate, idatabits, stopbits, parity, true, true);
|
||||||
port.addEventListener(this);
|
port.addEventListener(this);
|
||||||
} catch (SerialPortException e) {
|
} catch (SerialPortException e) {
|
||||||
if (e.getPortName().startsWith("/dev") && SerialPortException.TYPE_PERMISSION_DENIED.equals(e.getExceptionType())) {
|
if (e.getPortName().startsWith("/dev") && SerialPortException.TYPE_PERMISSION_DENIED.equals(e.getExceptionType())) {
|
||||||
@ -171,12 +162,9 @@ public class Serial implements SerialPortEventListener {
|
|||||||
/**
|
/**
|
||||||
* This method is intented to be extended to receive messages
|
* This method is intented to be extended to receive messages
|
||||||
* coming from serial port.
|
* coming from serial port.
|
||||||
*
|
|
||||||
* @param chars
|
|
||||||
* @param length
|
|
||||||
*/
|
*/
|
||||||
protected void message(char[] chars, int length) {
|
protected void message(char[] chars, int length) {
|
||||||
// Empty
|
// Empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -192,7 +180,7 @@ public class Serial implements SerialPortEventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void write(byte bytes[]) {
|
private void write(byte bytes[]) {
|
||||||
try {
|
try {
|
||||||
port.writeBytes(bytes);
|
port.writeBytes(bytes);
|
||||||
} catch (SerialPortException e) {
|
} catch (SerialPortException e) {
|
||||||
@ -208,7 +196,7 @@ public class Serial implements SerialPortEventListener {
|
|||||||
* (most often the case for networking and serial i/o) and
|
* (most often the case for networking and serial i/o) and
|
||||||
* will only use the bottom 8 bits of each char in the string.
|
* will only use the bottom 8 bits of each char in the string.
|
||||||
* (Meaning that internally it uses String.getBytes)
|
* (Meaning that internally it uses String.getBytes)
|
||||||
* <p/>
|
* <p>
|
||||||
* If you want to move Unicode data, you can first convert the
|
* If you want to move Unicode data, you can first convert the
|
||||||
* String to a byte stream in the representation of your choice
|
* String to a byte stream in the representation of your choice
|
||||||
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
|
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
|
||||||
@ -242,92 +230,8 @@ public class Serial implements SerialPortEventListener {
|
|||||||
* General error reporting, all corraled here just in case
|
* General error reporting, all corraled here just in case
|
||||||
* I think of something slightly more intelligent to do.
|
* I think of something slightly more intelligent to do.
|
||||||
*/
|
*/
|
||||||
static public void errorMessage(String where, Throwable e) {
|
private static void errorMessage(String where, Throwable e) {
|
||||||
System.err.println(I18n.format(_("Error inside Serial.{0}()"), where));
|
System.err.println(I18n.format(_("Error inside Serial.{0}()"), where));
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
class SerialMenuListener implements ItemListener {
|
|
||||||
//public SerialMenuListener() { }
|
|
||||||
|
|
||||||
public void itemStateChanged(ItemEvent e) {
|
|
||||||
int count = serialMenu.getItemCount();
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
((CheckboxMenuItem)serialMenu.getItem(i)).setState(false);
|
|
||||||
}
|
|
||||||
CheckboxMenuItem item = (CheckboxMenuItem)e.getSource();
|
|
||||||
item.setState(true);
|
|
||||||
String name = item.getLabel();
|
|
||||||
//System.out.println(item.getLabel());
|
|
||||||
PdeBase.properties.put("serial.port", name);
|
|
||||||
//System.out.println("set to " + get("serial.port"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
protected Vector buildPortList() {
|
|
||||||
// get list of names for serial ports
|
|
||||||
// have the default port checked (if present)
|
|
||||||
Vector list = new Vector();
|
|
||||||
|
|
||||||
//SerialMenuListener listener = new SerialMenuListener();
|
|
||||||
boolean problem = false;
|
|
||||||
|
|
||||||
// if this is failing, it may be because
|
|
||||||
// lib/javax.comm.properties is missing.
|
|
||||||
// java is weird about how it searches for java.comm.properties
|
|
||||||
// so it tends to be very fragile. i.e. quotes in the CLASSPATH
|
|
||||||
// environment variable will hose things.
|
|
||||||
try {
|
|
||||||
//System.out.println("building port list");
|
|
||||||
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
|
|
||||||
while (portList.hasMoreElements()) {
|
|
||||||
CommPortIdentifier portId =
|
|
||||||
(CommPortIdentifier) portList.nextElement();
|
|
||||||
//System.out.println(portId);
|
|
||||||
|
|
||||||
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
|
|
||||||
//if (portId.getName().equals(port)) {
|
|
||||||
String name = portId.getName();
|
|
||||||
//CheckboxMenuItem mi =
|
|
||||||
//new CheckboxMenuItem(name, name.equals(defaultName));
|
|
||||||
|
|
||||||
//mi.addItemListener(listener);
|
|
||||||
//serialMenu.add(mi);
|
|
||||||
list.addElement(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (UnsatisfiedLinkError e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
problem = true;
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("exception building serial menu");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//if (serialMenu.getItemCount() == 0) {
|
|
||||||
//System.out.println("dimming serial menu");
|
|
||||||
//serialMenu.setEnabled(false);
|
|
||||||
//}
|
|
||||||
|
|
||||||
// only warn them if this is the first time
|
|
||||||
if (problem && PdeBase.firstTime) {
|
|
||||||
JOptionPane.showMessageDialog(this, //frame,
|
|
||||||
"Serial port support not installed.\n" +
|
|
||||||
"Check the readme for instructions\n" +
|
|
||||||
"if you need to use the serial port. ",
|
|
||||||
"Serial Port Warning",
|
|
||||||
JOptionPane.WARNING_MESSAGE);
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user