Problem in serial port communication in j2me
I am developing an application for hp ipaq hw6500. A part of it needs to find out the current location. For that initially i opted for location api, but that didnt work out in ipaq. So i thought of using serial port communication. Now I have used the following..
sc= (StreamConnection)Connector.open("comm:7;baudrate=4800;bitsperchar=8;stopbits=1;parity=none");
is =sc.openDataInputStream();
But its not working. Further I need to parse it to get the necessary location coordinates. I dont really have any idea regarding that also...plzzzzzzzz help!
# 1
The entire code goes like this...
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.io.*;
import java.lang.String.*;
public class TestSerial extends MIDlet implements CommandListener
{
Form form1;
TextField tf1;
Display disp1=null;
Command command1;
StreamConnection sc;
DataInputStream is;
int dataFidelity=0;
/****************************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****************/
sc= (StreamConnection)Connector.open("comm:7;baudrate=4800;bitsperchar=8;stopbits=1;parity=none");
is =sc.openDataInputStream();}
catch(Exception e)
{
System.out.println(e.toString());
}
int newData = 0;
boolean flag1=false,flag2=false;
StringBuffer inputBuffer = new StringBuffer();
//PARSE NMEA sentence
boolean done = false;
boolean gotsentence = false;
boolean sentenceactive = false;
boolean eatchecksum = false;
while (!done)
{
try
{
newData = is.read();
switch (newData)
{
case -1:
done = true;
break;
case '$':
sentenceactive = true;
break;
case '*':
eatchecksum = true;
break;
case 0x0d:
break;
case 0x0a:
sentenceactive = false;
gotsentence = true;
done = true;
break;
default:
if (!eatchecksum)
{
inputBuffer.append((char)newData);
}
break;
}
}
catch (IOException ex)
{
System.err.println(ex.getMessage());
}
}
if (gotsentence)
{
String sentence = new String(inputBuffer);
System.out.println("[" + sentence + "]\r\n");
String s1=sentence.substring(1,6);
if(s1.equals("GPGGA"))
{
String s2=sentence.substring(14,16);
String s3=sentence.substring(16,22);
String lat=s2.concat(s3);
flag1=true;
dataFidelity++;
String s4=sentence.substring(25,28);
String s5=sentence.substring(28,34);
String lon=s4.concat(s5);
flag2=true;
form1.append("Latitude: "+lat+"Longitude: "+lon);
}
inputBuffer = new StringBuffer();
if(flag1 && flag2 && dataFidelity == 1)
{
try{
sc.close();}
catch(IOException e)
{
form1.append(""+e.getMessage());
}
}
}
}
}
}
But still its not working...plz let me know the defects..i m waiting eagerly...