0

Actually I'm trying to open serial port to send logging data in j2me.
When I connect my phone via usb cable to pc my code works correctly. But when I start app without usb cable connecting to phone it is hanging up.

private static void sendToSerial(String msg) {
        try {
            if (msg == null) return;
            msg += getEol(); // append End Of Line symbol "\r\n"
            CommConnection comm = (CommConnection)Connector.open("comm:" + getComPort());
            OutputStream os = comm.openOutputStream();
            os.write(msg.getBytes());
            os.close();
            comm.close();
        } catch (Exception error) {
            error.printStackTrace();
        }
    }

in above code getComPort() returns "com0" string.

Is there any way to use Connector.open() with timeout?
I already tried this too:

comm = (CommConnection)Connector.open("comm:" + getComPort(), Connector.WRITE, true);

Or is there any way to detect serial port is available or not?
I've already analyze microedition.commports system property too. Following method gives me "COM0" value.

public static String getComPort() {
        if (comPort != null && comPort.trim().length() > 0) {
            return comPort;
        }
        String comPorts = System.getProperty("microedition.commports");
        if (comPorts != null && comPorts.toLowerCase().indexOf("com") != -1) {
            int idxStart = comPorts.toLowerCase().indexOf("com");
            comPort = comPorts.substring(idxStart, idxStart + 4);
        }
        //midlet.showAlert("com port = " + comPort);
        return comPort;
    }
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by _neo_
0

I've also tried achieve this code snippet, which uses two TimerTask: first one for connecting to com port, second one starts a bit later to canceling first timer task if it hasn't finished work.

private static CommConnection comm = null;
    private static Timer timer1 = null;
    private static Timer timer2 = null;
    private static ComConnectTask connectTask = null;
    private static ComCancelTask cancelTask = null;
    private static boolean isComAvailable = false;
     ...
    /**
     * Tries to connect to com port.
     * @return true, if connection success. Otherwise returns false.
     */
    private static boolean connect() {
        isComAvailable = false;
        timer1 = new Timer();
        timer2 = new Timer();
        connectTask = new ComConnectTask();
        cancelTask = new ComCancelTask();
        timer1.schedule(connectTask, new Date());
        timer2.schedule(cancelTask, 5000);
        return isComAvailable;
    }
    
    /**
     * Task for connecting to com port.
     */
    private static class ComConnectTask extends TimerTask {
        public void run() {
            try {
                comm = (CommConnection)Connector.open("comm:" + getComPort(), Connector.WRITE, true);
                isComAvailable = true;
            } catch (Exception error) {
                error.printStackTrace();
            }
        }
    }
    
    /**
     * Task for canceling connection task to com port.
     */
    private static class ComCancelTask extends TimerTask {
        public void run() {
            if (!isComAvailable) {
                connectTask.cancel();
            }
        }
    }

it throws IOException: COM busy.
Please, help me.

Edited by _neo_: correcting sentence (my bad english)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.