Skip to content

Commit

Permalink
Code cleanup, more work on serial interface
Browse files Browse the repository at this point in the history
-moved serial processing back to Coordinator
-added more serial ports to the search list
-cleaned up the code a bit
  • Loading branch information
ciasaboark committed Jan 31, 2014
1 parent 4037129 commit ee180a6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 264 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Expand Up @@ -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<String> remoteModules = new HashSet<String>();
private static HashMap<String, Driver> loadedDrivers = new HashMap<String, Driver>();
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -459,8 +455,17 @@ static synchronized ArrayList<String> 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 {
Expand Down Expand Up @@ -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
*/
}
}
Expand Up @@ -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;

Expand All @@ -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<String> 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;
Expand All @@ -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;

Expand All @@ -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);
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}


}
}
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);
}
}
}

0 comments on commit ee180a6

Please sign in to comment.