Alex Rivera | Logout

How to transfer data to a serial port?

Asked 2011-11-15T10:58:04.397
9

I know that in J2ME CommConnection is the connection to use when working with serial port. I know there are the openInputStream and openOutputStream methods , but in fact I don't know how to transfer data from my MIDLet to the COM port ( the USB port into which the phone's cable is inserted , the phone is Alcatel OT-806D ). For example I want to send the text "Hello world". How to achieve that ?

Here are codes :

J2ME :

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.CommConnection;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

public class SerialPortMidlet extends MIDlet implements CommandListener, Runnable {
    private Command upload = new Command("upload", Command.SCREEN, 0);
    private Command exit = new Command("exit", Command.SCREEN, 1);
    private Form f = new Form("test serial port");
    private Thread uploadThread;
    private CommConnection com;
    private OutputStream os;
    public SerialPortMidlet()
    {
        f.addCommand(upload);
        f.addCommand(exit);
        f.setCommandListener(this);
        uploadThread = new Thread(this);
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(f);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    public void commandAction(Command c, Displayable d) {
        if (c == upload)
        {
            uploadThread.start();
            f.removeCommand(upload);
        }
        else if (c == exit)
        {
            if (uploadThread.isAlive())
            {
                uploadThread.interrupt();
                try {
   
Edit
Report

1 Answer

1

Your phone seems to be well detected by the computer as connected on the virtual COM port 4. However, this is not clear to me that you should use the COM port protocol on the phone side to communicate with the computer. It is perfectly possible that there is simply a buffer on the phone, that once filled will be delivered on the usb port.

I don't know your phone but I have already programmed a microcontroller. There I was never using the COM port protocol and managed to communicate with a computer with virtual com port driver.

To understand better my point, you can probably refer to documentation of the microcontroller present on your phone.

answered 2011-11-15T20:20:38.087

Your Answer