GPS Tracker GT06使用C#将登录数据包发送到终端

时间:2018-09-26 19:31:32

标签: c# .net gps

我正在实施GT06 GPS跟踪协议。在这种情况下,我不断从终端获取登录数据包,但是在成功将登录数据包响应发送到终端后,我无法从终端接收位置数据。下面是详细信息。

从终端到服务器的接收字符串:

  

78-78-0D-01-03-58-51-10-22-16-34-42-00-03-1A-8E-0D-0A

从服务器到终端的发送登录数据包响应的代码:

    string sendData = "78780501" + serialNo + "D9DC0D0A";
    Send(handler, sendData);

    private static void Send(Socket handler, String data)
    {

        byte[] byteData = StringToByteArray(data);
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.  
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.  
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }

以上是我当前正在使用的代码,但对我不起作用。每次我收到登录数据包而不是位置数据时。请指导我在哪里我需要正确的代码。

谢谢, 聘请小伙子。

1 个答案:

答案 0 :(得分:0)

您应该在serialNo之后添加开始时收到的错误检查。

例如:“ 78780501” +序列号+错误检查+“ 0D0A”;