package serial;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class ReadCOMPort extends javax.microedition.midlet.MIDlet implements CommandListener {
private static String url = "http://localhost:8081/servlet/com.josesandoval.midlet.web.servlet.DBStore";
private String timeStamp;
private String buttonID = "User: Jose Sandoval";
byte[] data;
Display display = null;
Command readCommand = new Command("Read", Command.OK, 1);
Command exitCommand = new Command("Exit", Command.EXIT, 1);
Command sendCommand = new Command("Send", Command.SCREEN, 1);
Command againCommand = new Command("Begin", Command.BACK, 1);
StringBuffer readValue = new StringBuffer();
public void startApp() {
if (display == null) {
initMIDlet();
}
}
private void initMIDlet() {
display = Display.getDisplay(this);
displayIntro();
}
private void displayIntro() {
TextBox tb = new TextBox("Read COM Port", "", 256, 0);
tb.setString("(c) 2002 Jose Sandoval. All Rights Reserved.\n\nPress <Read> to read the COM port.");
tb.addCommand(readCommand);
tb.addCommand(exitCommand);
tb.setCommandListener(this);
display.setCurrent(tb);
}
private void exitMIDlet() {
notifyDestroyed();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == readCommand) {
doReadCOMPort();
} else if (c == sendCommand) {
doHTTPSend();
} else if (c == againCommand) {
displayIntro();
}else {
exitMIDlet();
}
}
public void doHTTPSend() {
try {
HttpConnection conn = (HttpConnection) Connector.open(url);
conn.setRequestMethod(HttpConnection.POST);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(buttonID + " (Data read: " + readValue + ")");
dout.writeUTF(java.util.Calendar.getInstance().toString());
data = bout.toByteArray();
bout.close();
dout.close();
OutputStream os = conn.openOutputStream();
os.write(data);
os.close();
int rc = conn.getResponseCode();
if (rc == conn.HTTP_OK) {
conn.close();
TextBox tb = new TextBox("Everything Saved", "", 128, 0);
tb.setString("Your time stamp and serial port read has been saved.");
tb.addCommand(exitCommand);
tb.addCommand(againCommand);
tb.setCommandListener(this);
display.setCurrent(tb);
}
} catch (IOException e) {
TextBox tbException = new TextBox("Exception", "", 32, 0);
tbException.setString(e.toString());
tbException.addCommand(exitCommand);
tbException.addCommand(againCommand);
tbException.setCommandListener(this);
display.setCurrent(tbException);
}
}
public void doReadCOMPort() {
String parameter = "comm:0;baudrate=9600";
StreamConnection sc = null;
InputStream is = null;
try{
sc = (StreamConnection) Connector.open(parameter, Connector.READ, true);
is = sc.openInputStream();
int ch = 0;
while ((ch = is.read() ) != -1) {
readValue.append((char) ch);
}
if ( sc != null ) {
sc.close();
}
if ( is != null ) {
is.close();
}
} catch (Exception e) {
TextBox tbException = new TextBox("Exception", "", 32, 0);
tbException.setString(e.toString());
tbException.addCommand(exitCommand);
tbException.addCommand(againCommand);
tbException.setCommandListener(this);
display.setCurrent(tbException);
}
TextBox tb = new TextBox("Reading Port:", "", 32, 0);
tb.setString(readValue.toString());
tb.addCommand(exitCommand);
tb.addCommand(sendCommand);
tb.setCommandListener(this);
display.setCurrent(tb);
}
}