Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoug Schaefer2015-02-16 17:19:18 +0000
committerGerrit Code Review @ Eclipse.org2015-02-16 18:19:54 +0000
commit275feb68745a7eaf27d4010d4875abdd158cbc33 (patch)
tree53a50522fdb491eea480d89650eefcae19f59056 /core/org.eclipse.cdt.core.native/src
parent52c80c124ee2f4edccf292c472886d9cde8d6143 (diff)
downloadorg.eclipse.cdt-275feb68745a7eaf27d4010d4875abdd158cbc33.tar.gz
org.eclipse.cdt-275feb68745a7eaf27d4010d4875abdd158cbc33.tar.xz
org.eclipse.cdt-275feb68745a7eaf27d4010d4875abdd158cbc33.zip
Bug 459971 New SerialPort support. First step is Mac library.
Java classes are defined and a pretty simple native library for Mac is there. Tested with Arduino so I know reading works. This implementation should work for Linux as well. Windows is going to be the hard one. Not checking in the binaries yet until I get more testing with them. Also remove ppc native libraries for Mac since that hasn't been supported in a long time. Change-Id: If4ffbc6e73a7656a47c2f45b875be0842c482b05
Diffstat (limited to 'core/org.eclipse.cdt.core.native/src')
-rw-r--r--core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/BaudRate.java49
-rw-r--r--core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/ByteSize.java29
-rw-r--r--core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/Parity.java19
-rw-r--r--core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/SerialPort.java185
-rw-r--r--core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/StopBits.java18
5 files changed, 300 insertions, 0 deletions
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/BaudRate.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/BaudRate.java
new file mode 100644
index 00000000000..fd2151366e2
--- /dev/null
+++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/BaudRate.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.utils.serial;
+
+public enum BaudRate {
+
+ B0(0),
+ B50(50),
+ B75(75),
+ B110(110),
+ B134(134),
+ B150(150),
+ B200(200),
+ B300(300),
+ B600(600),
+ B1200(1200),
+ B1800(1800),
+ B2400(2400),
+ B4800(4800),
+ B7200(7200),
+ B9600(9600),
+ B14400(14400),
+ B19200(19200),
+ B28800(28800),
+ B38400(38400),
+ B57600(57600),
+ B76800(76800),
+ B115200(115200),
+ B230400(230400);
+
+ private final int rate;
+
+ private BaudRate(int rate) {
+ this.rate = rate;
+ }
+
+ public int getRate() {
+ return rate;
+ }
+
+}
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/ByteSize.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/ByteSize.java
new file mode 100644
index 00000000000..7f0e639f367
--- /dev/null
+++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/ByteSize.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.utils.serial;
+
+public enum ByteSize {
+
+ B5(5),
+ B6(6),
+ B7(7),
+ B8(8);
+
+ private final int size;
+
+ private ByteSize(int size) {
+ this.size = size;
+ }
+
+ public int getSize() {
+ return size;
+ }
+}
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/Parity.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/Parity.java
new file mode 100644
index 00000000000..276081c9e0a
--- /dev/null
+++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/Parity.java
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.utils.serial;
+
+public enum Parity {
+
+ None,
+ Even,
+ Odd
+
+}
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/SerialPort.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/SerialPort.java
new file mode 100644
index 00000000000..e5fc8545751
--- /dev/null
+++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/SerialPort.java
@@ -0,0 +1,185 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.utils.serial;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.runtime.Platform;
+
+public class SerialPort {
+
+ private final String portName;
+ private boolean isOpen;
+ private BaudRate baudRate = BaudRate.B115200;
+ private ByteSize byteSize = ByteSize.B8;
+ private Parity parity = Parity.None;
+ private StopBits stopBits = StopBits.S1;
+ private long handle;
+
+ private static final String PORT_OPEN = "Port is open";
+
+ static {
+ System.loadLibrary("serial"); //$NON-NLS-1$
+ }
+
+ /**
+ * Create a serial port that connect to the given serial device.
+ *
+ * @param portName name for the serial device.
+ */
+ public SerialPort(String portName) {
+ this.portName = portName;
+ }
+
+ /**
+ * List the available serial ports.
+ *
+ * @return serial ports
+ */
+ public static String[] list() {
+ if (Platform.getOS().equals(Platform.OS_MACOSX)) {
+ File dev = new File("/dev"); //$NON-NLS-1$
+ final Pattern pattern = Pattern.compile("tty\\.(usbserial|usbmodem).*"); //$NON-NLS-1$
+ File[] files = dev.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return pattern.matcher(name).matches();
+ }
+ });
+
+ if (files == null) {
+ return new String[0];
+ }
+ String[] names = new String[files.length];
+ for (int i = 0; i < files.length; i++) {
+ names[i] = files[i].getAbsolutePath();
+ }
+ return names;
+ } else {
+ return new String[0];
+ }
+ }
+
+ /**
+ * Return the name for this serial port.
+ *
+ * @return serial port name
+ */
+ public String getPortName() {
+ return portName;
+ }
+
+ public void open() throws IOException {
+ handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
+ isOpen = true;
+ }
+
+ private native long open0(String portName, int baudRate, int byteSize, int parity, int stopBits) throws IOException;
+
+ public void close() throws IOException {
+ close0(handle);
+ isOpen = false;
+ }
+
+ private native void close0(long handle) throws IOException;
+
+ public boolean isOpen() {
+ return isOpen;
+ }
+
+ public void setBaudRate(BaudRate rate) throws IOException {
+ if (isOpen) {
+ throw new IOException(PORT_OPEN);
+ }
+ this.baudRate = rate;
+ }
+
+ public BaudRate getBaudRate() {
+ return baudRate;
+ }
+
+ public void setByteSize(ByteSize size) throws IOException {
+ if (isOpen) {
+ throw new IOException(PORT_OPEN);
+ }
+ this.byteSize = size;
+ }
+
+ public ByteSize getByteSize() {
+ return byteSize;
+ }
+
+ public void setParity(Parity parity) throws IOException {
+ if (isOpen) {
+ throw new IOException(PORT_OPEN);
+ }
+ this.parity = parity;
+ }
+
+ public Parity getParity() {
+ return parity;
+ }
+
+ public void setStopBit(StopBits stopBits) throws IOException {
+ if (isOpen) {
+ throw new IOException(PORT_OPEN);
+ }
+ this.stopBits = stopBits;
+ }
+
+ public StopBits getStopBits() {
+ return stopBits;
+ }
+
+ public InputStream getInputStream() {
+ return new InputStream() {
+ @Override
+ public int read() throws IOException {
+ if (isOpen()) {
+ return read0(handle);
+ } else {
+ return -1;
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ SerialPort.this.close();
+ }
+ };
+ }
+
+ private native int read0(long handle) throws IOException;
+
+ public OutputStream getOutputStream() {
+ return new OutputStream() {
+ @Override
+ public void write(int b) throws IOException {
+ if (isOpen()) {
+ write0(handle, b);
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ SerialPort.this.close();
+ }
+ };
+ }
+
+ private native void write0(long handle, int b) throws IOException;
+
+}
diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/StopBits.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/StopBits.java
new file mode 100644
index 00000000000..1e9a7e07a33
--- /dev/null
+++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/serial/StopBits.java
@@ -0,0 +1,18 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software Systems and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * QNX Software Systems - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.utils.serial;
+
+public enum StopBits {
+
+ S1,
+ S2
+
+}

Back to the top