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;
}