我正在实施 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;
}
上面是我当前正在使用的代码,但它不适合我。每次我收到登录数据包而不是位置数据时。请指导我需要更正代码的地方。
谢谢, 海伦小伙子。
您应该在serialNo之后添加您一开始收到的错误检查。
示例:“78780501”+序列号+错误检查+“0D0A”;
校验和或错误字节未修复,它是通过使用长度+协议+序列号生成的,示例收到的十六进制代码:78781101035517210119626770110001001b951a0d0a
用于创建它的响应:78780501001b66070d0a
0501001b 错误字节为 6607
在此生成 https://crccalc.com/[在此处输入图像描述]