diff --git a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/4b/e4/ed/history.index b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/4b/e4/ed/history.index index a7d049e..8d7f012 100644 Binary files a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/4b/e4/ed/history.index and b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/4b/e4/ed/history.index differ diff --git a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/e4/e4/ed/history.index b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/e4/e4/ed/history.index index a691f0e..2b3e021 100644 Binary files a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/e4/e4/ed/history.index and b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Coordinator/.indexes/e4/e4/ed/history.index differ diff --git a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources index c2e936e..d2bd079 100644 Binary files a/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources and b/coordinator_eclipse_workspace/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources differ diff --git a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/Coordinator.java b/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/Coordinator.java index 501f5f5..6548ddb 100644 --- a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/Coordinator.java +++ b/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/Coordinator.java @@ -23,18 +23,16 @@ * */ public class Coordinator { - - //TODO check that 1000 bytes enough + private static final String TAG = "Coordinator"; private static ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); private static HashSet remoteModules = new HashSet(); private static HashMap loadedDrivers = new HashMap(); private static int portNum; private static final int DEFAULT_PORT = 2024; private static String serialPortName = null; - private static final String TAG = "Coordinator"; - private static boolean ioReady = false; - - //bitmask flags for the transmission start byte + /* + * Bit-mask flags for the transmission start byte + */ private static final byte TEXT_TRANSMISSION = (byte)0b0000_0000; private static final byte BIN_TRANSMISSION = (byte)0b1000_0000; //the safety bit is reserved and always 1. This is to make sure that the @@ -144,7 +142,6 @@ private static synchronized void sendCommandV1(String moduleName, String command return; } - ioReady = true; //the arduino expects chars as 1 byte instead of two, convert command //+ to ascii byte array, then tack on the header, name, and footer byte startByte = (byte)(TEXT_TRANSMISSION | SAFETY_BIT | protocolVersion); @@ -321,7 +318,6 @@ static synchronized void sendBinary(String moduleName, byte[] data) { return; } - ioReady = true; //the arduino expects chars as 1 byte instead of two, convert command //+ to ascii byte array, then tack on the header, name, and footer byte startByte = (byte)(BIN_TRANSMISSION | SAFETY_BIT | protocolVersion); @@ -459,8 +455,17 @@ static synchronized ArrayList getLoadedDrivers() { } - static synchronized void setIoReady(boolean state) { - ioReady = state; +// static synchronized void setIoReady(boolean state) { +// ioReady = state; +// } + + static void incomingSerial(byte b) { + //Log.d(TAG, "incoming byte " + (char)b); + if (b == 0x0A) { + Coordinator.processMessage(byteBuffer.toByteArray()); + } else { + byteBuffer.write(b); + } } public static void main(String argv[]) throws InterruptedException { @@ -616,20 +621,8 @@ public static void main(String argv[]) throws InterruptedException { } } - ioReady = false; Thread.yield(); Thread.sleep(100); } - - - /* - * TODO: - * -wait for the local arduino to respond with "READY" on the serial line - * -broadcast a message to every remote module asking for their name - * -store those names - * -load all drivers - * -start the web server to listen for connections from a frontend - * -enter infinite loop checking for input from the local arduino - */ } } diff --git a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnection.java b/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnection.java index 670052d..4897eb7 100644 --- a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnection.java +++ b/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnection.java @@ -10,7 +10,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.Enumeration; import java.util.LinkedHashSet; @@ -19,15 +19,7 @@ public class SerialConnection implements SerialPortEventListener { private static final String TAG = "SerialTest"; SerialPort serialPort; - /** The port we're normally going to use. */ private static LinkedHashSet portNames; - - /** - * A BufferedReader which will be fed by a InputStreamReader - * converting the bytes into characters - * making the displayed results codepage independent - */ - private BufferedReader bReader; private InputStream input; /** The output stream to the port */ private OutputStream output; @@ -36,8 +28,6 @@ public class SerialConnection implements SerialPortEventListener { /** Default bits per second for COM port. */ // private static final int DATA_RATE = 9600; private static final int DATA_RATE = 115200; - private static String preferredConnection; - private static ByteArrayOutputStream buffer = new ByteArrayOutputStream(); private boolean connected = false; @@ -53,7 +43,11 @@ public SerialConnection(String preferredConnection) { portNames.add("/dev/tty.usbmodemfa131"); //MacOS portNames.add("/dev/tty.usbmodemfd121"); //MacOS portNames.add("/dev/ttyUSB0"); //Linux + portNames.add("/dev/ttyUSB1"); //Linux + portNames.add("/dev/ttyUSB2"); //Linux portNames.add("COM3"); //Windows + portNames.add("COM2"); //Windows + portNames.add("COM1"); //Windows this.initialize(preferredConnection); } @@ -62,6 +56,7 @@ private void initialize(String preferredConnection) { connected = false; CommPortIdentifier portId = null; + @SuppressWarnings("rawtypes") Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); //First, Find an instance of serial port as set in PORT_NAMES. @@ -97,7 +92,6 @@ private void initialize(String preferredConnection) { // open the streams input = serialPort.getInputStream(); - bReader = new BufferedReader(new InputStreamReader(input)); output = serialPort.getOutputStream(); // add event listeners @@ -125,25 +119,23 @@ synchronized void close() { } /** - * Handle an event on the serial port. Read the data and print it. + * reads a single byte of data from the serial connection + * Since SerialConnection does not understand the protocol + * formats it passes the byte back to Coordinator for processing */ public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { int readInt = input.read(); if (readInt != -1) { - if ((byte)readInt == 0x0A) { - Coordinator.processMessage(buffer.toByteArray()); - } else { - buffer.write((byte)readInt); - } + Coordinator.incomingSerial((byte)readInt); } } catch (Exception e) { System.err.println(e.toString()); } } } - + synchronized boolean isDataAvailable() { boolean available = false; try { @@ -163,15 +155,13 @@ synchronized int readInputByte() throws IOException { synchronized void writeData(byte[] data) { - try { -// Log.d(TAG, "writing byte[] data to output"); - output.write(data); - output.flush(); - } catch (IOException e) { - Log.e(TAG, "error writing byte[] data to output, trying to re-connect:"); - initialize(null); - } - - - } -} \ No newline at end of file + try { +// Log.d(TAG, "writing byte[] data to output"); + output.write(data); + output.flush(); + } catch (IOException e) { + Log.e(TAG, "error writing byte[] data to output, trying to re-connect:"); + initialize(null); + } + } +} diff --git a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnectionOld.java b/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnectionOld.java deleted file mode 100644 index ada9c2c..0000000 --- a/coordinator_eclipse_workspace/Coordinator/src/org/apparatus_templi/SerialConnectionOld.java +++ /dev/null @@ -1,211 +0,0 @@ -package org.apparatus_templi; - -import gnu.io.CommPortIdentifier; -import gnu.io.PortInUseException; -import gnu.io.SerialPort; -import gnu.io.SerialPortEvent; -import gnu.io.SerialPortEventListener; -import gnu.io.UnsupportedCommOperationException; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.TooManyListenersException; - -import javax.xml.bind.DatatypeConverter; - - -public class SerialConnectionOld implements SerialPortEventListener { - - private final String TAG = "SerialConnection"; - private final byte[] testBytes = {(byte)0x00, (byte)0xFF}; - - - public SerialConnectionOld() { - super(); - } - - - /* - * BEGIN IMPORTED CODE - */ - private SerialPort serialPort = null; - - //input and output streams for sending and receiving data - private InputStream input = null; - private OutputStream output = null; - - //the timeout value for connecting with the port - final int TIMEOUT = 2000; - - //some ascii values for for certain things - final int SPACE_ASCII = 32; - final int DASH_ASCII = 45; - final int NEW_LINE_ASCII = 10; - - //a string for recording what goes on in the program - //this string is written to the GUI - String logText = ""; - - //search for all the serial ports - //pre style="font-size: 11px;": none - //post: adds all the found ports to a combo box on the GUI -// public void searchForPorts() -// { -// ports = CommPortIdentifier.getPortIdentifiers(); -// -// while (ports.hasMoreElements()) -// { -// CommPortIdentifier curPort = (CommPortIdentifier)ports.nextElement(); -// -// //get only serial ports -// if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL) -// { -// //window.cboxPorts.addItem(curPort.getName()); -// portMap.put(curPort.getName(), curPort); -// } -// } -// } - - //connect to the selected port in the combo box - //pre style="font-size: 11px;": ports are already found by using the searchForPorts - //method - //post: the connected comm port is stored in commPort, otherwise, - //an exception is generated - public boolean connect(String selectedPort) - { - boolean connected = false; - try { - CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(selectedPort); - serialPort = (SerialPort) portId.open(TAG, 5000); - Log.d(TAG, selectedPort + " opened successfully."); - connected = true; - - //set the port parameters - int baudRate = 57600; // 57600bps - try { - // Set serial port to 57600bps-8N1..my favourite - serialPort.setSerialPortParams( - baudRate, - SerialPort.DATABITS_8, - SerialPort.STOPBITS_1, - SerialPort.PARITY_NONE); - } catch (UnsupportedCommOperationException ex) { - System.err.println(ex.getMessage()); - } - - try { - serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); - // OR - // If CTS/RTS is needed - //serialPort.setFlowControlMode( - // SerialPort.FLOWCONTROL_RTSCTS_IN | - // SerialPort.FLOWCONTROL_RTSCTS_OUT); - } catch (UnsupportedCommOperationException ex) { - System.err.println(ex.getMessage()); - } - - } catch (PortInUseException e) { - Log.e(TAG, "port " + selectedPort + " is in use. (" + e.toString() + ")"); - } catch (Exception e) { - Log.e(TAG, "Failed to open " + selectedPort + "(" + e.toString() + ")"); - } - - return connected; - } - - - - public boolean initIOStream() { - boolean successful = false; - - try { - input = serialPort.getInputStream(); - output = serialPort.getOutputStream(); - writeData(testBytes); - - successful = true; - return successful; - } catch (IOException e) { - Log.e(TAG, "I/O Streams failed to open. (" + e.toString() + ")"); - return successful; - } - } - - - //starts the event listener that knows whenever data is available to be read - //pre style="font-size: 11px;": an open serial port - //post: an event listener for the serial port that knows when data is received - public void initListener() - { - try - { - serialPort.addEventListener(this); - serialPort.notifyOnDataAvailable(true); - } - catch (TooManyListenersException e) - { - logText = "Too many listeners. (" + e.toString() + ")"; -// window.txtLog.setForeground(Color.red); -// window.txtLog.append(logText + "\n"); - } - } - - - //disconnect the serial port - //pre style="font-size: 11px;": an open serial port - //post: closed serial port - public void disconnect() - { - //close the serial port - try - { - //writeData(new byte[] {0b00}); - - serialPort.removeEventListener(); - serialPort.close(); - input.close(); - output.close(); -// setConnected(false); -// window.keybindingController.toggleControls(); - - logText = "Disconnected."; - - } catch (Exception e) { - Log.e(TAG, "Failed to close " + serialPort.getName() + "(" + e.toString() + ")"); - } - } - - - public void serialEvent(SerialPortEvent evt) { - if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) { -// Log.d(TAG, "serial data available for reading"); - Coordinator.setIoReady(true); - try { - Byte dataByte = (byte)input.read(); - //char c = (char)singleData; - Log.d(TAG, "Read hex value: " + DatatypeConverter.printHexBinary(new byte[] {dataByte})); - Log.d(TAG, "Read char value: " + new String(new byte[] {dataByte.byteValue()})); - } catch (IOException e) { - Log.e(TAG, "Failed to read data. (" + e.toString() + ")"); - } - } - } - - - public void writeData(byte[] data) { - try { - Log.d(TAG, "writing byte[] data to output"); - output.write(data); - output.flush(); - } catch (IOException e) { - Log.e(TAG, "error writing byte[] data to output"); - } - - - } - - /* - * END IMPORTED CODE - */ -}