1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

Merge branch 'ide-1.5.x-jssc2' into ide-1.5.x

This commit is contained in:
Federico Fissore
2013-12-03 18:15:52 +01:00
19 changed files with 1047 additions and 350 deletions

View File

@ -29,17 +29,15 @@
package cc.arduino.packages.discoverers;
import gnu.io.CommPortIdentifier;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import jssc.SerialPortList;
import processing.app.Base;
import processing.app.Platform;
import processing.app.helpers.PreferencesMap;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import java.util.ArrayList;
import java.util.List;
public class SerialDiscovery implements Discovery {
@ -49,27 +47,21 @@ public class SerialDiscovery implements Discovery {
String devicesListOutput = os.preListAllCandidateDevices();
List<BoardPort> res = new ArrayList<BoardPort>();
@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> ports = CommPortIdentifier
.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier commPort = ports.nextElement();
if (commPort.getPortType() != CommPortIdentifier.PORT_SERIAL)
continue;
String address = commPort.getName();
String boardName = os.resolveDeviceAttachedTo(address, Base.packages,
devicesListOutput);
String label = address;
String[] ports = SerialPortList.getPortNames();
for (String port : ports) {
String boardName = os.resolveDeviceAttachedTo(port, Base.packages, devicesListOutput);
String label = port;
if (boardName != null)
label += " (" + boardName + ")";
BoardPort port = new BoardPort();
port.setAddress(address);
port.setProtocol("serial");
port.setBoardName(boardName);
port.setLabel(label);
res.add(port);
BoardPort boardPort = new BoardPort();
boardPort.setAddress(port);
boardPort.setProtocol("serial");
boardPort.setBoardName(boardName);
boardPort.setLabel(label);
res.add(boardPort);
}
return res;
}

View File

@ -89,15 +89,13 @@ public class SerialUploader extends Uploader {
// otherwise assert DTR, which would cancel the WDT reset if
// it happened within 250 ms. So we wait until the reset should
// have already occured before we start scanning.
if (!Base.isMacOS())
Thread.sleep(300);
Thread.sleep(300);
uploadPort = waitForUploadPort(uploadPort, before);
} else {
Thread.sleep(400);
}
} catch (SerialException e) {
throw new RunnerException(e.getMessage());
throw new RunnerException(e);
} catch (InterruptedException e) {
throw new RunnerException(e.getMessage());
}

View File

@ -23,16 +23,16 @@
*/
package processing.app;
//import processing.core.*;
import jssc.*;
import processing.app.debug.MessageConsumer;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static processing.app.I18n._;
import gnu.io.*;
import java.io.*;
import java.util.*;
public class Serial implements SerialPortEventListener {
@ -53,84 +53,70 @@ public class Serial implements SerialPortEventListener {
int stopbits;
boolean monitor = false;
// read buffer and streams
InputStream input;
OutputStream output;
byte buffer[] = new byte[32768];
int bufferIndex;
int bufferLast;
MessageConsumer consumer;
public Serial(boolean monitor) throws SerialException {
this(Preferences.get("serial.port"),
Preferences.getInteger("serial.debug_rate"),
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
Preferences.getInteger("serial.debug_rate"),
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
this.monitor = monitor;
}
public Serial() throws SerialException {
this(Preferences.get("serial.port"),
Preferences.getInteger("serial.debug_rate"),
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
Preferences.getInteger("serial.debug_rate"),
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
}
public Serial(int irate) throws SerialException {
this(Preferences.get("serial.port"), irate,
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
}
public Serial(String iname, int irate) throws SerialException {
this(iname, irate, Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
}
public Serial(String iname) throws SerialException {
this(iname, Preferences.getInteger("serial.debug_rate"),
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
Preferences.get("serial.parity").charAt(0),
Preferences.getInteger("serial.databits"),
new Float(Preferences.get("serial.stopbits")).floatValue());
}
public static boolean touchPort(String iname, int irate) throws SerialException {
SerialPort port;
boolean result = false;
SerialPort serialPort = new SerialPort(iname);
try {
@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = portList.nextElement();
if ((CommPortIdentifier.PORT_SERIAL == portId.getPortType()) && (portId.getName().equals(iname))) {
port = (SerialPort) portId.open("tap", 2000);
port.setSerialPortParams(irate, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.close();
result = true;
serialPort.openPort();
serialPort.setParams(irate, 8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.closePort();
return true;
} catch (SerialPortException e) {
throw new SerialException(I18n.format(_("Error touching serial port ''{0}''."), iname), e);
} finally {
if (serialPort.isOpened()) {
try {
serialPort.closePort();
} catch (SerialPortException e) {
// noop
}
}
} catch (PortInUseException e) {
throw new SerialException(
I18n.format(_("Serial port ''{0}'' already in use. Try quitting any programs that may be using it."), iname)
);
} catch (Exception e) {
throw new SerialException(
I18n.format(_("Error touching serial port ''{0}''."), iname), e
);
}
return result;
}
public Serial(String iname, int irate,
char iparity, int idatabits, float istopbits)
throws SerialException {
public Serial(String iname, int irate, char iparity, int idatabits, float istopbits) throws SerialException {
//if (port != null) port.close();
//this.parent = parent;
//parent.attach(this);
@ -148,142 +134,62 @@ public class Serial implements SerialPortEventListener {
if (istopbits == 2) stopbits = SerialPort.STOPBITS_2;
try {
port = null;
@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//System.out.println("found " + portId.getName());
if (portId.getName().equals(iname)) {
//System.out.println("looking for "+iname);
port = (SerialPort)portId.open("serial madness", 2000);
port.setDTR(true);
port.setRTS(true);
input = port.getInputStream();
output = port.getOutputStream();
port.setSerialPortParams(rate, databits, stopbits, parity);
port.addEventListener(this);
port.notifyOnDataAvailable(true);
//System.out.println("opening, ready to roll");
}
}
}
} catch (PortInUseException e) {
throw new SerialException(
I18n.format(
_("Serial port ''{0}'' already in use. Try quiting any programs that may be using it."),
iname
)
);
port = new SerialPort(iname);
port.openPort();
port.setParams(rate, databits, stopbits, parity, true, true);
port.addEventListener(this);
} catch (Exception e) {
throw new SerialException(
I18n.format(
_("Error opening serial port ''{0}''."),
iname
),
e
);
// //errorMessage("<init>", e);
// //exception = e;
// //e.printStackTrace();
throw new SerialException(I18n.format(_("Error opening serial port ''{0}''."), iname), e);
}
if (port == null) {
throw new SerialNotFoundException(
I18n.format(
_("Serial port ''{0}'' not found. Did you select the right one from the Tools > Serial Port menu?"),
iname
)
);
throw new SerialNotFoundException(I18n.format(_("Serial port ''{0}'' not found. Did you select the right one from the Tools > Serial Port menu?"), iname));
}
}
public void setup() {
//parent.registerCall(this, DISPOSE);
}
//public void size(int w, int h) { }
//public void pre() { }
//public void draw() { }
//public void post() { }
//public void mouse(java.awt.event.MouseEvent event) { }
//public void key(java.awt.event.KeyEvent e) { }
public void dispose() throws IOException {
// do io streams need to be closed first?
if (input != null) input.close();
if (output != null) output.close();
input = null;
output = null;
if (port != null) port.close(); // close the port
port = null;
if (port != null) {
try {
if (port.isOpened()) {
port.closePort(); // close the port
}
} catch (SerialPortException e) {
throw new IOException(e);
} finally {
port = null;
}
}
}
public void addListener(MessageConsumer consumer) {
this.consumer = consumer;
}
synchronized public void serialEvent(SerialPortEvent serialEvent) {
//System.out.println("serial port event"); // " + serialEvent);
//System.out.flush();
//System.out.println("into");
//System.out.flush();
//System.err.println("type " + serialEvent.getEventType());
//System.err.println("ahoooyey");
//System.err.println("ahoooyeysdfsdfsdf");
if (serialEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
//System.out.println("data available");
//System.err.flush();
public synchronized void serialEvent(SerialPortEvent serialEvent) {
if (serialEvent.isRXCHAR()) {
try {
while (input.available() > 0) {
//if (input.available() > 0) {
//serial = input.read();
//serialEvent();
//buffer[bufferCount++] = (byte) serial;
synchronized (buffer) {
if (bufferLast == buffer.length) {
byte temp[] = new byte[bufferLast << 1];
System.arraycopy(buffer, 0, temp, 0, bufferLast);
buffer = temp;
}
//buffer[bufferLast++] = (byte) input.read();
if(monitor == true)
System.out.print((char) input.read());
if (this.consumer != null)
this.consumer.message("" + (char) input.read());
/*
System.err.println(input.available() + " " +
((char) buffer[bufferLast-1]));
*/ //}
byte[] buf = port.readBytes();
if (buf.length > 0) {
if (bufferLast == buffer.length) {
byte temp[] = new byte[bufferLast << 1];
System.arraycopy(buffer, 0, temp, 0, bufferLast);
buffer = temp;
}
if (monitor) {
System.out.print(new String(buf));
}
if (this.consumer != null) {
this.consumer.message(new String(buf));
}
}
//System.out.println("no more");
} catch (IOException e) {
} catch (SerialPortException e) {
errorMessage("serialEvent", e);
//e.printStackTrace();
//System.out.println("angry");
}
catch (Exception e) {
}
}
//System.out.println("out of");
//System.err.println("out of event " + serialEvent.getEventType());
}
@ -291,7 +197,7 @@ public class Serial implements SerialPortEventListener {
* Returns the number of bytes that have been read from serial
* and are waiting to be dealt with by the user.
*/
public int available() {
public synchronized int available() {
return (bufferLast - bufferIndex);
}
@ -299,29 +205,27 @@ public class Serial implements SerialPortEventListener {
/**
* Ignore all the bytes read so far and empty the buffer.
*/
public void clear() {
public synchronized void clear() {
bufferLast = 0;
bufferIndex = 0;
}
/**
* Returns a number between 0 and 255 for the next byte that's
* waiting in the buffer.
* 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
* first check available() to see if things are ready to avoid this)
*/
public int read() {
public synchronized int read() {
if (bufferIndex == bufferLast) return -1;
synchronized (buffer) {
int outgoing = buffer[bufferIndex++] & 0xff;
if (bufferIndex == bufferLast) { // rewind
bufferIndex = 0;
bufferLast = 0;
}
return outgoing;
int outgoing = buffer[bufferIndex++] & 0xff;
if (bufferIndex == bufferLast) { // rewind
bufferIndex = 0;
bufferLast = 0;
}
return outgoing;
}
@ -329,8 +233,8 @@ public class Serial implements SerialPortEventListener {
* Returns the next byte in the buffer as a char.
* Returns -1, or 0xffff, if nothing is there.
*/
public char readChar() {
if (bufferIndex == bufferLast) return (char)(-1);
public synchronized char readChar() {
if (bufferIndex == bufferLast) return (char) (-1);
return (char) read();
}
@ -338,154 +242,146 @@ public class Serial implements SerialPortEventListener {
/**
* 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 read, but it's easier to use than
* a byte array on each read, but it's easier to use than
* readBytes(byte b[]) (see below).
*/
public byte[] readBytes() {
public synchronized byte[] readBytes() {
if (bufferIndex == bufferLast) return null;
synchronized (buffer) {
int length = bufferLast - bufferIndex;
byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
int length = bufferLast - bufferIndex;
byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex = 0; // rewind
bufferLast = 0;
return outgoing;
}
bufferIndex = 0; // rewind
bufferLast = 0;
return outgoing;
}
/**
* 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 readBytes() returning a byte[] array.
*
* 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 readBytes() returning a byte[] array.
* <p/>
* 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.
*/
public int readBytes(byte outgoing[]) {
public synchronized int readBytes(byte outgoing[]) {
if (bufferIndex == bufferLast) return 0;
synchronized (buffer) {
int length = bufferLast - bufferIndex;
if (length > outgoing.length) length = outgoing.length;
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
int length = bufferLast - bufferIndex;
if (length > outgoing.length) length = outgoing.length;
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex += length;
if (bufferIndex == bufferLast) {
bufferIndex = 0; // rewind
bufferLast = 0;
}
return length;
}
bufferIndex += length;
if (bufferIndex == bufferLast) {
bufferIndex = 0; // rewind
bufferLast = 0;
}
return length;
}
/**
* Reads from the serial port into a buffer of bytes up to and
* including a particular character. If the character isn't in
* including a particular character. If the character isn't in
* the serial buffer, then 'null' is returned.
*/
public byte[] readBytesUntil(int interesting) {
public synchronized byte[] readBytesUntil(int interesting) {
if (bufferIndex == bufferLast) return null;
byte what = (byte)interesting;
byte what = (byte) interesting;
synchronized (buffer) {
int found = -1;
for (int k = bufferIndex; k < bufferLast; k++) {
if (buffer[k] == what) {
found = k;
break;
}
int found = -1;
for (int k = bufferIndex; k < bufferLast; k++) {
if (buffer[k] == what) {
found = k;
break;
}
if (found == -1) return null;
int length = found - bufferIndex + 1;
byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex = 0; // rewind
bufferLast = 0;
return outgoing;
}
if (found == -1) return null;
int length = found - bufferIndex + 1;
byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex = 0; // rewind
bufferLast = 0;
return outgoing;
}
/**
* Reads from the serial port into a buffer of bytes until a
* 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 returned.
*
* If outgoing[] is not big enough, then -1 is returned,
* and an error message is printed on the console.
* <p/>
* 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 returned.
* If 'interesting' byte is not in the buffer, then 0 is returned.
*/
public int readBytesUntil(int interesting, byte outgoing[]) {
public synchronized int readBytesUntil(int interesting, byte outgoing[]) {
if (bufferIndex == bufferLast) return 0;
byte what = (byte)interesting;
byte what = (byte) interesting;
synchronized (buffer) {
int found = -1;
for (int k = bufferIndex; k < bufferLast; k++) {
if (buffer[k] == what) {
found = k;
break;
}
int found = -1;
for (int k = bufferIndex; k < bufferLast; k++) {
if (buffer[k] == what) {
found = k;
break;
}
if (found == -1) return 0;
int length = found - bufferIndex + 1;
if (length > outgoing.length) {
System.err.println(
I18n.format(
_("readBytesUntil() byte buffer is too small for the {0}" +
" bytes up to and including char {1}"),
length,
interesting
)
);
return -1;
}
//byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex += length;
if (bufferIndex == bufferLast) {
bufferIndex = 0; // rewind
bufferLast = 0;
}
return length;
}
if (found == -1) return 0;
int length = found - bufferIndex + 1;
if (length > outgoing.length) {
System.err.println(
I18n.format(
_("readBytesUntil() byte buffer is too small for the {0}" +
" bytes up to and including char {1}"),
length,
interesting
)
);
return -1;
}
//byte outgoing[] = new byte[length];
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
bufferIndex += length;
if (bufferIndex == bufferLast) {
bufferIndex = 0; // rewind
bufferLast = 0;
}
return length;
}
/**
* Return whatever has been read from the serial port so far
* as a String. It assumes that the incoming characters are ASCII.
*
* as a String. It assumes that the incoming characters are ASCII.
* <p/>
* 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 Unicode data), and send it as a byte array.
*/
public String readString() {
public synchronized String readString() {
if (bufferIndex == bufferLast) return null;
return new String(readBytes());
}
/**
* Combination of readBytesUntil and readString. See caveats in
* Combination of readBytesUntil and readString. See caveats in
* each function. Returns null if it still hasn't found what
* you're looking for.
*
* <p/>
* 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 Unicode data), and send it as a byte array.
*/
public String readStringUntil(int interesting) {
public synchronized String readStringUntil(int interesting) {
byte b[] = readBytesUntil(interesting);
if (b == null) return null;
return new String(b);
@ -497,10 +393,8 @@ public class Serial implements SerialPortEventListener {
*/
public void write(int what) { // will also cover char
try {
output.write(what & 0xff); // for good measure do the &
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
port.writeInt(what & 0xff);
} catch (SerialPortException e) {
errorMessage("write", e);
}
}
@ -508,24 +402,21 @@ public class Serial implements SerialPortEventListener {
public void write(byte bytes[]) {
try {
output.write(bytes);
output.flush(); // hmm, not sure if a good idea
} catch (Exception e) { // null pointer or serial port dead
//errorMessage("write", e);
e.printStackTrace();
port.writeBytes(bytes);
} catch (SerialPortException e) {
errorMessage("write", e);
}
}
/**
* 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 that you mean to send a byte buffer
* for Unicode (two bytes per char), nor will it send UTF8
* characters.. It assumes 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 string.
* (Meaning that internally it uses String.getBytes)
*
* <p/>
* 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 Unicode data), and send it as a byte array.
@ -535,45 +426,23 @@ public class Serial implements SerialPortEventListener {
}
public void setDTR(boolean state) {
port.setDTR(state);
try {
port.setDTR(state);
} catch (SerialPortException e) {
errorMessage("setDTR", e);
}
}
public void setRTS(boolean state) {
port.setRTS(state);
try {
port.setRTS(state);
} catch (SerialPortException e) {
errorMessage("setRTS", e);
}
}
/**
* 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, who knows.
*/
static public List<String> list() {
List<String> list = new ArrayList<String>();
try {
//System.err.println("trying");
@SuppressWarnings("unchecked")
Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
//System.err.println("got port list");
while (portList.hasMoreElements()) {
CommPortIdentifier portId = portList.nextElement();
//System.out.println(portId);
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
String name = portId.getName();
list.add(name);
}
}
} catch (UnsatisfiedLinkError e) {
//System.err.println("1");
errorMessage("ports", e);
} catch (Exception e) {
//System.err.println("2");
errorMessage("ports", e);
}
//System.err.println("move out");
return list;
return Arrays.asList(SerialPortList.getPortNames());
}