This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

suggestions required for J2ME

 
Salman Faraz
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Salman Faraz
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic