Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial')
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java92
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java68
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java55
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java228
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java52
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java31
-rw-r--r--native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties11
7 files changed, 537 insertions, 0 deletions
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java
new file mode 100644
index 00000000000..70965fdeed4
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/BaudRate.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * 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.serial;
+
+/**
+ * @since 1.0
+ */
+public enum BaudRate {
+
+ B110(110),
+ B300(300),
+ B600(600),
+ B1200(1200),
+ B2400(2400),
+ B4800(4800),
+ B9600(9600),
+ B14400(14400),
+ B19200(19200),
+ B38400(38400),
+ B57600(57600),
+ B115200(115200);
+
+ private final int rate;
+
+ private BaudRate(int rate) {
+ this.rate = rate;
+ }
+
+ public int getRate() {
+ return rate;
+ }
+
+ private static final String[] strings = {
+ "110", //$NON-NLS-1$
+ "300", //$NON-NLS-1$
+ "600", //$NON-NLS-1$
+ "1200", //$NON-NLS-1$
+ "2400", //$NON-NLS-1$
+ "4800", //$NON-NLS-1$
+ "9600", //$NON-NLS-1$
+ "14400", //$NON-NLS-1$
+ "19200", //$NON-NLS-1$
+ "38400", //$NON-NLS-1$
+ "57600", //$NON-NLS-1$
+ "115200" //$NON-NLS-1$
+ };
+
+ public static String[] getStrings() {
+ return strings;
+ }
+
+ private static final BaudRate[] rates = {
+ B110,
+ B300,
+ B600,
+ B1200,
+ B2400,
+ B4800,
+ B9600,
+ B14400,
+ B19200,
+ B38400,
+ B57600,
+ B115200
+ };
+
+ public static BaudRate fromStringIndex(int rate) {
+ return rates[rate];
+ }
+
+ public static int getStringIndex(BaudRate rate) {
+ for (int i = 0; i < rates.length; ++i) {
+ if (rate.equals(rates[i])) {
+ return i;
+ }
+ }
+ return getStringIndex(getDefault());
+ }
+
+ public static BaudRate getDefault() {
+ return B9600;
+ }
+
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java
new file mode 100644
index 00000000000..ad37c63afc6
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/ByteSize.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * 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.serial;
+
+/**
+ * @since 5.8
+ */
+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;
+ }
+
+ private static final String[] strings = {
+ "5", //$NON-NLS-1$
+ "6", //$NON-NLS-1$
+ "7", //$NON-NLS-1$
+ "8" //$NON-NLS-1$
+ };
+
+ public static String[] getStrings() {
+ return strings;
+ }
+
+ private static final ByteSize[] sizes = {
+ B5,
+ B6,
+ B7,
+ B8
+ };
+
+ public static ByteSize fromStringIndex(int size) {
+ return sizes[size];
+ }
+
+ public static int getStringIndex(ByteSize size) {
+ for (int i = 0; i < sizes.length; ++i) {
+ if (size.equals(sizes[i])) {
+ return i;
+ }
+ }
+ return getStringIndex(getDefault());
+ }
+
+ public static ByteSize getDefault() {
+ return B8;
+ }
+
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java
new file mode 100644
index 00000000000..c93fa815d16
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/Parity.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * 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.serial;
+
+/**
+ * @since 5.8
+ */
+public enum Parity {
+
+ None,
+ Even,
+ Odd;
+
+ private static final String[] strings = {
+ "None", //$NON-NLS-1$
+ "Even", //$NON-NLS-1$
+ "Odd" //$NON-NLS-1$
+ };
+
+ public static String[] getStrings() {
+ return strings;
+ }
+
+ private static final Parity[] parities = {
+ None,
+ Even,
+ Odd
+ };
+
+ public static Parity fromStringIndex(int index) {
+ return parities[index];
+ }
+
+ public static int getStringIndex(Parity parity) {
+ for (int i = 0; i < parities.length; ++i) {
+ if (parity.equals(parities[i])) {
+ return i;
+ }
+ }
+ return getStringIndex(getDefault());
+ }
+
+ public static Parity getDefault() {
+ return None;
+ }
+
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
new file mode 100644
index 00000000000..62c7a9bdabb
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
@@ -0,0 +1,228 @@
+/*******************************************************************************
+ * 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.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.cdt.serial.internal.Messages;
+
+/**
+ * @since 5.8
+ */
+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 SERIAL_KEY = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; //$NON-NLS-1$
+ private static final String PORT_OPEN = Messages.getString("SerialPort.PortIsOpen"); //$NON-NLS-1$
+
+ static {
+ try {
+ System.loadLibrary("serial"); //$NON-NLS-1$
+ } catch (UnsatisfiedLinkError e) {
+ e.printStackTrace();
+ }
+ }
+
+ private InputStream inputStream = new InputStream() {
+ @Override
+ public int read() throws IOException {
+ if (isOpen()) {
+ return read0(handle);
+ } else {
+ return -1;
+ }
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (isOpen()) {
+ return read1(handle, b, off, len);
+ } else {
+ return -1;
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ SerialPort.this.close();
+ }
+ };
+
+ private OutputStream outputStream = new OutputStream() {
+ @Override
+ public void write(int b) throws IOException {
+ if (isOpen()) {
+ write0(handle, b);
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ SerialPort.this.close();
+ }
+ };
+
+ /**
+ * 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 (System.getProperty("os.name").equals("Mac OS X")) { //$NON-NLS-1$//$NON-NLS-2$
+ 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 if (System.getProperty("os.name").equals("Windows NT")) { //$NON-NLS-1$//$NON-NLS-2$
+// WindowsRegistry reg = WindowsRegistry.getRegistry();
+// if (reg != null) {
+// List<String> ports = new ArrayList<>();
+// int i = 0;
+// String name = reg.getLocalMachineValueName(SERIAL_KEY, i);
+// while (name != null) {
+// String value = reg.getLocalMachineValue(SERIAL_KEY, name);
+// ports.add(value);
+// i++;
+// name = reg.getLocalMachineValueName(SERIAL_KEY, i);
+// }
+// return ports.toArray(new String[ports.size()]);
+// } else {
+// return new String[0];
+// }
+ return new String[0];
+ } else {
+ return new String[0];
+ }
+ }
+
+ private native long open0(String portName, int baudRate, int byteSize, int parity, int stopBits) throws IOException;
+
+ private native void close0(long handle) throws IOException;
+
+ private native int read0(long handle) throws IOException;
+
+ private native int read1(long handle, byte[] b, int off, int len);
+
+ private native void write0(long handle, int b) throws IOException;
+
+ /**
+ * Return the name for this serial port.
+ *
+ * @return serial port name
+ */
+ public String getPortName() {
+ return portName;
+ }
+
+ public InputStream getInputStream() {
+ return inputStream;
+ }
+
+ public OutputStream getOutputStream() {
+ return outputStream;
+ }
+
+ public void open() throws IOException {
+ handle = open0(portName, baudRate.getRate(), byteSize.getSize(), parity.ordinal(), stopBits.ordinal());
+ isOpen = true;
+ }
+
+ public synchronized void close() throws IOException {
+ if (isOpen) {
+ close0(handle);
+ isOpen = false;
+ handle = 0;
+ }
+ }
+
+ 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 setStopBits(StopBits stopBits) throws IOException {
+ if (isOpen) {
+ throw new IOException(PORT_OPEN);
+ }
+ this.stopBits = stopBits;
+ }
+
+ public StopBits getStopBits() {
+ return stopBits;
+ }
+
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java
new file mode 100644
index 00000000000..92752ce8e19
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/StopBits.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * 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.serial;
+
+/**
+ * @since 5.8
+ */
+public enum StopBits {
+
+ S1,
+ S2;
+
+ private static final String[] strings = {
+ "1", //$NON-NLS-1$
+ "2" //$NON-NLS-1$
+ };
+
+ public static String[] getStrings() {
+ return strings;
+ }
+
+ private static final StopBits[] stopBits = {
+ S1,
+ S2
+ };
+
+ public static StopBits fromStringIndex(int index) {
+ return stopBits[index];
+ }
+
+ public static int getStringIndex(StopBits sb) {
+ for (int i = 0; i < stopBits.length; ++i) {
+ if (sb.equals(stopBits[i])) {
+ return i;
+ }
+ }
+ return getStringIndex(getDefault());
+ }
+
+ public static StopBits getDefault() {
+ return S1;
+ }
+
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java
new file mode 100644
index 00000000000..45a3d53817a
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/Messages.java
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * 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.serial.internal;
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+public class Messages {
+ private static final String BUNDLE_NAME = "org.eclipse.cdt.serial.internal.messages"; //$NON-NLS-1$
+
+ private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
+
+ private Messages() {
+ }
+
+ public static String getString(String key) {
+ try {
+ return RESOURCE_BUNDLE.getString(key);
+ } catch (MissingResourceException e) {
+ return '!' + key + '!';
+ }
+ }
+}
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties
new file mode 100644
index 00000000000..9a39f7958ce
--- /dev/null
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/internal/messages.properties
@@ -0,0 +1,11 @@
+##################################################################################
+# 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
+##################################################################################
+SerialPort.PortIsOpen=Port is open

Back to the top