1

I bought a gps, I tested this with sms message and with its own software to retrieve the lat / long and it works fine , see the screenshot : enter image description here

Now I'm sure that the communication with gprs->internet works so I want to create my own software who can do a lot of other functionality that does not exist with the software manufacturer.. In my first reflection, I tried to listen the NMEA messages with socket tcp/ip (java) on the port 7070 , here is my source code:

 import java.io.*;
 import java.net.*;


 public class Serveur {
 static final int port = 7070;
  public static void main(String[] args) throws Exception {              

    ServerSocket s = new ServerSocket(port);
    System.out.println("waiting for connexion from GPS 7070");
    Socket socClient = s.accept();
    System.out.println("Connexion GPS ok");

           BufferedReader in = new BufferedReader(
                           new InputStreamReader(socClient.getInputStream())
                          ); 
            PrintWriter out = new PrintWriter(
                         new BufferedWriter(
                            new OutputStreamWriter(socClient.getOutputStream())), 
                         true);   
    String str = in.readLine(); 

    System.out.println("the GPS message  is : " + str);         

    in.close();
    out.close();

    socClient.close();
  } 
  }

I tried to send a message through a java client on the local network, and it works..

But the problem that I do not understand is : when I receive a message from the GPS, i get this error :

waiting for connexion from GPS 7070
Connexion GPS ok
Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at serveur.Serveur.main(Serveur.java:27)

at serveur.Serveur.main(Serveur.java:27)

the error comes from the line String str = in.readLine();

if its software was able to intercept the message NMEA why not my application ??

UPDATE:

I sniffed the network ,here is the result :

[the protocol used by my GPS][4]

the first packet received from the GPS (the hidden ip adress is my fixed public address ): enter image description here

the first response from the software(1/2) (its an empty message):

enter image description here

from the software(2/2): enter image description here

from the GPS:

enter image description here

from the soft: enter image description here

from the GPS(1/2)(its an empty message): enter image description here

and finally the long/lat/speed... from the GPS(2/2): enter image description here

as I have already written, there are empty messages sent by the GPS...So here is all details.

@Peter Lawrey:I tried to read the message byte by byte but I got an error from netbeans,,,can you give me a piece of code? thanks Hope this help me.

Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80
  • Are you sure the GPS is a Socket client? It would make sense to think the device is the server. – Mister Smith Mar 01 '12 at 12:55
  • I'm not sure ,but normally this is a socket client... – Marwen Trabelsi Mar 06 '12 at 09:41
  • Do you have the device and the server in the same machine? – Mister Smith Mar 06 '12 at 09:50
  • No, the device is in the car,and the message sent by the GPS through the GPRS networks, normally there is a chance of working it, as you already saw there is a communication with the GPS... – Marwen Trabelsi Mar 06 '12 at 10:55
  • Then try Peter's answer and use `socClient.getInputStream()` directly instead of wrapping it in a BufferedReader. By calling `InputStream.read()` which returns a byte (in the LSB of an int), you can check if you are receiving data or there is a problem with line terminator. – Mister Smith Mar 06 '12 at 11:18

1 Answers1

2

Are you sure the GPS is sending a line (ended with a new line)?

If not I would use read() repeatedly to get all the data it sends (until an EOF is reached)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130