This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java Micro Edition and the fly likes suggestions required for J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "suggestions required for J2ME" Watch "suggestions required for J2ME" New topic
Author

suggestions required for J2ME

Salman Faraz
Greenhorn

Joined: Feb 14, 2005
Posts: 28
Respected Friends,
I wanted to establish a connection with serial port through my J2ME application.I am writting the following code it compiles(build) successfully but it is throwing exception which means that the Sun's J2ME wireless toolkit does'nt supports serial port connection,can any one please tell me the reason why the toolkit is not supporting serial port and if there is any mistake in the code what is it,the code is as follows:

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

public class TestSerial extends MIDlet implements CommandListener
{
Form form1;
TextField tf1;
Display disp1=null;
Command command1;
StreamConnection sc;
OutputConnection oc;
Connection connection1;

/****************************DefiningConstructor*****************************/

public TestSerial()
{
disp1=Display.getDisplay(this);
command1=new Command("Start",Command.OK,1);
form1=new Form("Serial Port Connection");
form1.addCommand(command1);
form1.setCommandListener(this);
}
/********************************************************************************/
public void startApp()
{
disp1.setCurrent(form1);
}

public void pauseApp()
{
}

public void destroyApp(boolean b)
{
}

public void commandAction(Command c,Displayable d)
{
if(c==command1)
{
try
{
/************Establishing serial port connection****************/
connection1= Connector.open("comm:0;baudrate=9000");
oc.close();
}

catch(Exception e)
{
System.out.println(e.toString());
}
}
}
}

Thanking You,
Salman Faraz.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi Faraz,

The Wireless toolkit(WTK) does support the serial port communications. You can get the available list of ports for the WTK with the method, System.getProperty("microedition.commports") method, here you will get a comma seperated list.

I think if you have "com0" instead of "0", your program may work.

Hope this will solve your problem, if the problem still persist, please do post the exception you get so we can pin point the problem more accurately.


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Salman Faraz
Greenhorn

Joined: Feb 14, 2005
Posts: 28
Respected Mr Ransika deSilva ,
Thanks for your previous reply.According to your suggestion I tryed to use the System.getProperty("microedition.commports") method but it continiously returned me null does this means that no ports are supported.The code after making changes is as follows:

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

public class TestSerial extends MIDlet implements CommandListener
{
Form form1;
TextField tf1;
Display disp1=null;
Command command1;
StreamConnection sc;
OutputConnection oc;
Connection connection1;

/****************************Defining Constructor*****************************/

public TestSerial()
{
disp1=Display.getDisplay(this);
command1=new Command("Start",Command.OK,1);
form1=new Form("Serial Port Connection");
form1.addCommand(command1);
form1.setCommandListener(this);
}
/********************************************************************************/
public void startApp()
{
disp1.setCurrent(form1);
}

public void pauseApp()
{
}

public void destroyApp(boolean b)
{
}

public void commandAction(Command c,Displayable d)
{
if(c==command1)
{
try
{
/************Establishing serial port connection****************/
/*connection1= Connector.open("comm:0;baudrate=9000");
connection1.close();*/
String string1=System.getProperty("microedition.commports");
System.out.println(string1);
}

catch(Exception e)
{
System.out.println(e.toString());
}
}
}
}

If you run the above code you will get null which means that no ports are present,how is this possible,since my PC has all three parallel,serial and USB ports.Can you please mail the correct code so that I can successfully establish connection with the serial ports.Please mail it as soon as possible since it is very important for me.

Thanking You,
Salman Faraz.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi,
I am so sorry, I couldn't reply you soon, well the getProperty() returns a COMMA seperated list of all the the available ports. You have put the entire String to get the port, but what happens is the String looks like this, "COM1,COM2" so on. So you have to get one out of all these ports, get ONE out of the list. This should work fine. Regards.
Salman Faraz
Greenhorn

Joined: Feb 14, 2005
Posts: 28
Respected Mr Ransika deSilva,
I am sorry but Sir I was unable to understand your last reply(since I am not very genius or intellegent).If convinient for you than can you please make changes(which you are suggesting) in the code which I posted.I know that it will take a lot of your precious time but I would be thankful to you from the depth of my heart.

Thanking You,
Salman Faraz.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi,
Sorry about my previous reply if you couldn't understand it. What I meant was that the getProperty() method signature is . Since you have many ports available, what you get as the return value is a "Comman String of ports". For example the returned String would look like: COM1,COM2,COM3

So you have to get one out of all the ports. You can do a substring like this.

String ports = getProperty("MicroEdtion.ports");
String myPort = ports.substring(0,ports.indexOf(','));

Now you can use the myPort String in your Connection.
Hope this is much clearer.
Regards
Salman Faraz
Greenhorn

Joined: Feb 14, 2005
Posts: 28
Respected Sir,
I did the same way as yu suggested but I am getting "null" and nothing else.I am using J2ME wireless toolkit version1.0,on what version are you working.

Thanking You,
Salman Faraz.
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi,
Well I am using the WTK 2.1 version. I am not sure whether this is going to work with WTK 1.0 version. If possible try out it with 2.1 version. Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: suggestions required for J2ME