Skip to content

Commit

Permalink
More work on serial interface
Browse files Browse the repository at this point in the history
LedFlash.java:
-extra debugging statements added

Coordinator.java:
-application layer protocol changed:
  -added one byte to the front of the header to hold transmission type and protocol version
  -transmission type is bit 7, 0 is text transfer, 1 is binary
  -bit 6 reserved
  -bit 5 is always 1 (this will keep the header from ever having the same value as the terminating LF byte)
  -bits 0-4 are protocol version (0 0000) for this initial version
-sendCommand() now implimented (not tested). Creates a byte[] of the message to transmmit
-uses new SerialConnection interface

SerialConnection.java:
-new code based off the stock arduino example
-attempts to auto connect, does not use serial port given in command line (yet)
-added write(byte[]) to send data on the serial line (not tested).
  • Loading branch information
ciasaboark committed Jan 27, 2014
1 parent d9c6298 commit 26ed40e
Show file tree
Hide file tree
Showing 4 changed files with 438 additions and 237 deletions.
5 changes: 4 additions & 1 deletion Coordinator/drivers/org/apparatus_templi/LedFlash.java
Expand Up @@ -138,6 +138,7 @@ public void tellController(String controllerName, String command) {
*/
@Override
public void run() {
Log.d(moduleName, "starting");
if (Coordinator.isModulePresent(moduleName)) {
while (running) {
/*
Expand All @@ -149,13 +150,15 @@ public void run() {
Log.d(moduleName, "flashing LED on pin " + i);
Coordinator.sendCommand(moduleName, String.valueOf(i));
try {
Thread.sleep(15000);
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} else {
Log.w(moduleName, "remote module not present, shutting down");
}
}

Expand Down
137 changes: 114 additions & 23 deletions Coordinator/src/org/apparatus_templi/Coordinator.java
@@ -1,9 +1,11 @@
package org.apparatus_templi;
import gnu.io.SerialPort;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
Expand All @@ -27,9 +29,22 @@ public class Coordinator {
private static final int defaultPort = 2024;
private static String serialPortName = "";
private static final String TAG = "Coordinator";
private static SerialConnection serialConnection = null;
private static SerialConnectionOld serialConnectionOld = null;
private static boolean ioReady = false;

//bitmask flags for the transmission header
private static byte textTransmission = (byte)0b0010_0000;
private static byte binTransmission = (byte)0b1010_0000;
private static byte protocolVersion = (byte)0b0000_0000; //current protocol is version 0

private static String headerSeperator = ":";
private static String footer = "\n";

private static SerialConnection serialConnection;




private static HashMap<String, Driver> runningDrivers = new HashMap<String, Driver>();


Expand All @@ -39,7 +54,7 @@ public class Coordinator {
* @param message the message to pass
*/
private void passMessage(String driverName, String message) {
//TODO
//
}

/**
Expand All @@ -62,6 +77,70 @@ private boolean serialDataAvailable() {
}


/**
* Sends the given command to a specific remote module
* @param name the unique name of the remote module
* @param command the command to send to the remote module
*/
public static synchronized void sendCommand(String name, String command) {
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 headerByte = (byte)(textTransmission | protocolVersion);
byte[] destinationBytes = {0b0};
byte[] headerSeperatorByte = {0b0};
byte[] commandBytes = {0b0};
byte[] footerByte = {0b0};

boolean sendMessage = true;

//convert the destination address to ascii byte array
try {
destinationBytes = name.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "error converting remote module name '" + name + "' to US-ASCII encoding.");
sendMessage = false;
}

//convert the destination separator to ascii byte array
try {
headerSeperatorByte = headerSeperator.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "error converting header seperator to ascii byte array.");
sendMessage = false;
}

//convert the command to ascii byte array
try {
commandBytes = command.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "error converting command '" + command +"' to US-ASCII encoding.");
sendMessage = false;
}

//convert the footer to ascii byte
try {
footerByte = footer.getBytes("US-ASCII");
} catch (UnsupportedEncodingException e) {
Log.w(TAG, "error converting message footer to US-ASCII encoding");
sendMessage = false;
}

if (sendMessage) {
ByteBuffer bBuffer = ByteBuffer.allocate(destinationBytes.length + headerSeperatorByte.length + commandBytes.length + footerByte.length);
// bBuffer.put(headerByte);
bBuffer.put(destinationBytes);
bBuffer.put(headerSeperatorByte);
bBuffer.put(commandBytes);
bBuffer.put(footerByte);
Log.d(TAG, "sending bytes 0x" + DatatypeConverter.printHexBinary(bBuffer.array()));
Log.d(TAG, "sending ascii string '" + new String(bBuffer.array()) + "'");
serialConnection.writeData(bBuffer.array());
} else {
Log.w(TAG, "error converting message to ascii byte[]. Message not sent");
}
}

/*
* Protected methods. The drivers should make use of these
*/
Expand All @@ -81,18 +160,17 @@ public static synchronized String sendCommandAndWait(String name, String command
return null;
}

public static synchronized void setIoReady(boolean state) {
ioReady = state;
}

/**
* Sends the given command to a specific remote module
* @param name the unique name of the remote module
* @param command the command to send to the remote module
* Sends binary data over the serial connection
* @param moduleName the unique name of the remote module
* @param data command the binary data to send
*/
public static synchronized void sendCommand(String name, String command) {
ioReady = true;
serialConnection.writeData(name + ":" + command + "\n");
public static synchronized void sendBinary(String moduleName, byte[] data) {

}

public static synchronized void setIoReady(boolean state) {
ioReady = state;
}

/**
Expand Down Expand Up @@ -190,7 +268,7 @@ public static void main(String argv[]) {

//try to guess a good serial port if we weren't given one
if (cmd.hasOption("serial")) {
serialPortName = cmd.getOptionValue("serialName");
serialPortName = cmd.getOptionValue("serial");
} else {
String osName = System.getProperty("os.name","").toLowerCase();
if ( osName.startsWith("windows") ) {
Expand All @@ -210,25 +288,38 @@ public static void main(String argv[]) {
} catch (ParseException e) {
System.out.println("Error processing options: " + e.getMessage());
new HelpFormatter().printHelp("Diff", options);
Log.e(TAG, "Error parsing options");
Log.e(TAG, "Error parsing command line options, exiting");
System.exit(1);
}

//setup the serial connection
// //setup the serial connection
// serialConnection = new SerialConnection();
// if (serialConnection.connect(serialPortName)) {
// if (serialConnection.initIOStream() == true) {
// serialConnection.initListener();
// }
// } else {
// //writing to both STDERR and the log file
// String errMsg = "Unable to open serial port " + serialPortName + ", exiting";
// System.err.println(errMsg);
// Log.e(TAG, errMsg);
// System.exit(1);
// }

serialConnection = new SerialConnection();
if (serialConnection.connect("/dev/tty.usbmodemfa131")) {
if (serialConnection.initIOStream() == true) {
serialConnection.initListener();
}
}
if (!serialConnection.initialize()) {
Log.e(TAG, "could not connect to serial port, exiting");
System.err.println("could not connect to serial port, exiting");
System.exit(1);
}


//start the drivers
LedFlash driver1 = new LedFlash();
(new Thread(driver1)).start();
runningDrivers.put(driver1.getModuleName(), (Driver)driver1);
//right now just add the driver name to the remote module list
remoteModules.add(driver1.getModuleName());
(new Thread(driver1)).start();

//enter main loop
while (true) {
Expand Down

0 comments on commit 26ed40e

Please sign in to comment.