GPS Tracker GT06 在C#中向终端发送登录数据包
GPS Tracker GT06 Send login packet to terminal in C#
我正在实施 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;
}
以上是我目前正在使用的代码,但它对我不起作用。每次我收到登录数据包而不是位置数据。请指导我在哪里需要更正代码。
谢谢,
Hiren Lad.
您应该在 serialNo 之后添加您在开始时收到的错误检查。
示例:"78780501" + serialNo + ErrorCheck + "0D0A";
我正在实施 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;
}
以上是我目前正在使用的代码,但它对我不起作用。每次我收到登录数据包而不是位置数据。请指导我在哪里需要更正代码。
谢谢, Hiren Lad.
您应该在 serialNo 之后添加您在开始时收到的错误检查。
示例:"78780501" + serialNo + ErrorCheck + "0D0A";