GPS103 Tracker Listening Application in C#
Asked Answered
C

1

0

I am developing a console based listening app for my GPS tracker in C#, my GPS tracker is configured to send packets on the server with specific port, where my application runs. Now the thing is according to the protocol document of GPS103 It very first sends the string like ##,12345678999121,A which I have already received in my application, but after receiving this string I have to send "LOAD" to my GPS tracker from app which will again Responded by GPS tracker by sending message Logon Success.

Issue: I have to send the command after receiving first string, but I never get a reply from the GPS Tracker.

namespace PakTrackingListenerApp_tk103_
{
class Program
{
    static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(1000);
        System.Net.ServicePointManager.Expect100Continue = false;
        listener.Start();

        while (true)
        {
            Console.WriteLine("Waiting for a connection");
            TcpClient client = listener.AcceptTcpClient();
            StreamReader sr = new StreamReader(client.GetStream());
            StreamWriter sw = new StreamWriter(client.GetStream());

            try
            {
                string request = sr.ReadLine();
                Console.WriteLine("GPS Command" + request);
                string[] tokens = request.Split(',');

                string response = "LOAD";
                if (tokens[0] == "##")
                {
                    response = "LOAD";
                    Console.WriteLine("Token" + tokens[0]);
                }

                sw.WriteLine(response);
                sw.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception :" +e.Message);
            }
            client.Close();
        }
    }
}
}
Clio answered 17/8, 2016 at 8:19 Comment(4)
Usually for each client that establishes a connection, you move it off into it's own thread.... because that is the connection you will communicate on for the rest of the clients lifetimeStature
but is there any way so that i can make sure that the response i am sending to my device is actually going to it in Response.Clio
When a client establishes a connection through TCP, then it is a direct line between the two devices.Stature
Actually for making connection to device ... its follows a protocol first device sends ##,12345678999121,A this string on server which is recived by my application Works ok. Now in Response i have to send "LOAD" command which to device and if its is interpreted by Device then it send back "Success log on" ... I have tried to send LOAD Command in this code but never recive success response i always receive ##,12345678999121,A string after 1 minute or 2 minute interval.Clio
S
3

You are terminating the connection with client.Close(); without reading the reply. Do not close till it returns empty.

1) Device sends ##,12345678999121,A

2) Reply with LOAD

3) It sends back 12345678999121;

4) Reply with **,imei:12345678999121,B;

5) It sends back the tracking data imei:12345678999121,tracker,161003171049,,F,091045.000,A,1017.6730,N,07845.7982,E,0.00,0;

6) Reply with ON

Now it keeps on repeating the steps 3 to 6 continuously.

Refer these links for proper usage.

https://github.com/tananaev/traccar/blob/master/src/org/traccar/protocol/Gps103ProtocolDecoder.java

http://www.gpspassion.com/forumsen/topic.asp?TOPIC_ID=108384&whichpage=80

Code sample:

  ' Enter the listening loop. 
        While True
            Console.Write(listeningPort & " Waiting for a connection... " & vbLf)

            ' Perform a blocking call to accept requests. 
            ' You could also user server.AcceptSocket() here. 
            Dim client As TcpClient = server.AcceptTcpClient()
            Console.WriteLine(listeningPort & " Connected!")

            data = Nothing

            ' Get a stream object for reading and writing 
            Dim stream As NetworkStream = client.GetStream()
            stream.ReadTimeout = 180000

            Dim i As Int32

            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
                ' Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                Console.WriteLine(listeningPort & " Received: {0}", data)

                If Trim(data) <> "" Then
                    If Trim(data).StartsWith("##,") Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("LOAD")
                        stream.Write(byte1, 0, byte1.Length)
                    ElseIf Trim(data).EndsWith(";") And Trim(data).Contains("imei:") = False Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("**,imei:" & data.Replace(";", ",B;"))
                        stream.Write(byte1, 0, byte1.Length)
                    Else

           'TODO: PROCESS THE DATA HERE

                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("ON")
                        stream.Write(byte1, 0, byte1.Length)
                    End If
                End If

                Try
                    i = stream.Read(bytes, 0, bytes.Length)
                Catch ex As IOException     'Added For the Timeout, End Connection and Start Again
                    If ex.Message.Contains("did not properly respond after a period of time") Then
                        'WriteLog("Port: " & listeningPort & " IOException: " & ex.Message)
                        client.Close()
                        server.Server.Close()
                        server.Stop()
                    Else
                        Try
                            client.Close()
                            server.Server.Close()
                            server.Stop()
                        Catch e As Exception
                            'WriteLog("Port: " & listeningPort & " Error in Closing Port. : " & e.Message)
                        End Try
                    End If
                    GoTo finalblock
                End Try
            End While
            ' Shutdown and end connection
            client.Close()
        End While

These devices do not have the protocol format displayed in their boxes or in the manual. Finding the protocol itself is hard.

Sniffle answered 3/10, 2016 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.