GPS103跟踪器听力在C#中的应用 [英] GPS103 Tracker Listening Application in C#

查看:81
本文介绍了GPS103跟踪器听力在C#中的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C#中的GPS跟踪器开发一个基于控制台的侦听应用程序,我的GPS跟踪器配置为在具有运行我的应用程序的特定端口的服务器上发送数据包.现在事情是根据GPS103的协议文件 它首先发送在应用程序中已经收到的字符串,例如##,12345678999121,A,但是在收到该字符串之后,我必须从应用程序向GPS跟踪器发送"LOAD",该应用程序将再次通过发送消息Logon Success来响应GPS跟踪器的响应.

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.

问题:我必须在收到第一个字符串后发送命令,但我从未收到GPS跟踪器的回复.

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

推荐答案

您要终止与client.Close();的连接,而无需阅读答复.在它返回空之前,请不要关闭.

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

1)设备发送##,12345678999121,A

2)用LOAD

3)它发回12345678999121;

4)回复**,imei:12345678999121,B;

5)它发送回跟踪数据imei:12345678999121,tracker,161003171049,,F,091045.000,A,1017.6730,N,07845.7982,E,0.00,0;

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)回复ON

现在,它继续不断地重复步骤3到6.

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

代码示例:

  ' 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

这些设备的包装盒或手册中未显示协议格式.很难找到协议本身.

这篇关于GPS103跟踪器听力在C#中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆