Thursday, July 17, 2008

Retrieve GPS data from the receiver

jGPS is a JAVA-API that makes the handling of GPS devices a lot easier. Most of the GPS devices use their own protocol along with the NMEA standard.NMEA has been standardized for communications from GPS device to host.jGPS is a new product. It is in a quite early stage, so it does by far not yet include all of the manufacturer-specific protocols.

I tested this in windows operating system. What we have to do is,
1. download jgps.jar and jGPS.dll.

2.copy the native libs to the system32 directory and integrate the jar with the projects jars.

Now we are ready to write our application,
public class GPSReader implements GPSListener {
static int count = 0;
NMEAHandler nh = null;
String lon = "Not yet read";
String lat = "Not yet Read";

public String getLat() {
return lat;
}

public String getLon() {
return lon;
}

public GPSReader() {
try {
nh = new NMEAHandler(1, "myApp", "COM4", 9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
nh.command(GPSConstants.CMD_INIT, 0);
}
catch (ConnectException x) {
System.out.println(" Something is going wrong");
JOptionPane.showMessageDialog(null, x.getMessage(),"Error", JOptionPane.INFORMATION_MESSAGE);
}
nh.registerListener(this);
}

public void actionPerformed(GPSEvent ev) {
Position p = null;
if (ev.getTransType() == GPSEvent.POS_TRANS) {
p = (Position) ev.getData();
Descriptor wa = p.getProperty("LONGITUDE");
Descriptor wb = p.getProperty("LATITUDE");
lon = wa == null ? "" : (String) wa.getValue().toString();
lat = wb == null ? "" : (String) wb.getValue().toString();
System.out.println("Latitude/Longitude = [" + lat + "][" + lon + "]");
}
}

public void portClose() {
try {
nh.close();
} catch (Exception e) {
System.out.println("While closing: " + e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.INFORMATION_MESSAGE);
}
}
}

No comments:

search engine

Custom Search