GPS103 Tracker Listening Application in C#

无人久伴 提交于 2019-12-23 04:43:42

问题


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();
        }
    }
}
}

回答1:


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.



来源:https://stackoverflow.com/questions/38991490/gps103-tracker-listening-application-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!