我正在为C#中的GPS追踪器开发一个基于控制台的聆听应用程序,我的GPS追踪器配置为在服务器上发送具有特定端口的数据包,我的应用程序在该端口运行。现在的事情是根据GPS103的协议文件
它首先发送我已在我的应用程序中收到的##,12345678999121,A
字符串,但收到此字符串后,我必须从应用程序向我的GPS跟踪器发送“LOAD”,这将再次由GPS跟踪器通过发送消息进行响应登录成功。
问题:我必须在收到第一个字符串后发送命令,但我从未收到GPS追踪器的回复。
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();
}
}
}
}
答案 0 :(得分:2)
您正在终止与client.Close();
的连接而未阅读回复。不要关闭,直到它返回空。
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;
6)回复ON
现在它不断重复步骤3到6。
请参阅这些链接以了解正确用法。
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
这些设备的框或手册中没有显示协议格式。找到协议本身很难。