package serial;

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

/**
 * (c) 2002 - All rights reserved.
 * Read serial device connected to a Java enabled phone 
 * (Original phone used i85s, but any Java enabled phone would work). 
 * Once data has been read, MIDlet can connect to a Web server via POST request
 * and run a Servlet and store information into a DB.
 * @author Jose Sandoval (jose@josesandoval.com) - May 12, 2002, 4:42 PM
 * @version 1.0
 */
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();
   
	/**
	 * Start application.
	 */
    public void startApp() {
        if (display == null) {
            initMIDlet();
        }
    }
   
	/**
	 * Init MIDlet.
	 */
    private void initMIDlet() {
        display = Display.getDisplay(this);
        displayIntro();
    }
    
   
	/**
	 * Display introduction.
	 */
    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);
    }
   
	/**
	 * Exit MIDlet.
	 */
    private void exitMIDlet() {
        notifyDestroyed();
    }
   
	/**
	 * Pause MIDlet.
	 */
    public void pauseApp() {
    }
   
	/**
	 * Destroy MIDlet.
	 * @param unconditional
	 */
    public void destroyApp(boolean unconditional) {
    }
   
	/**
	 * Action listener.
	 * @param c Command
	 * @param s Displayable
	 */
    public void commandAction(Command c, Displayable s) {
        if (c == readCommand) {
            doReadCOMPort();
        } else if (c == sendCommand) {
            doHTTPSend();
        } else if (c == againCommand) {
            displayIntro();
        }else {
            exitMIDlet();
        }
    }
   
	/**
	 * Connect to HTTP server using POST method.
	 */
    public void doHTTPSend() {
        try {
            HttpConnection conn = (HttpConnection) Connector.open(url);
            conn.setRequestMethod(HttpConnection.POST);
            /*
            conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
            conn.setRequestProperty("Content-Language", "en-US");
            conn.setRequestProperty("Accept", "application/octet-stream");
            conn.setRequestProperty("Connection", "close");
            conn.setRequestProperty("Content-Length", Integer.toString(data.length));
             */
            
            // Prepare data to send...
            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();
            
            // Send
            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);
        }
    }
   
	/**
	 * Read COM port.
	 */
    public void doReadCOMPort() {
        // String parameter = "comm:0;baudrate=9600;parity=n;databits=8;stopbits=1;flowcontrol=n/n";
        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);
            }
            
            // Clean up
            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);
            // e.printStackTrace();
        }
        
        TextBox tb = new TextBox("Reading Port:", "", 32, 0);
        tb.setString(readValue.toString());        
        tb.addCommand(exitCommand);
        tb.addCommand(sendCommand);
        tb.setCommandListener(this);
        display.setCurrent(tb);
    }
}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Wednesday, February 12 2003 at 23:17