Good Day All
Its been years since I posted something, am a bit stuck on something. I bought a GPS device from China and was given a Protocol document and I have written a TCP listening and hosted it on the server. I can use TCP test tools like "Hercules" and I was also given a Trace tool so that I can trace if any commands are sent on the tracker. They also have an app called "Any tracking" that can be downloaded in Playstore. I did install it and tested the device and I see its working on functions like flash light, sound(horn) . Here is my TCP listener code and when I send a commands from my TCP tool , I get a response in my TCP listener but the device does not respond to the commands and I have attached the Protocol document
https://www.vbforums.com/attachment.php?attachmentid=191053&d=1712353605[
^]
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class TCPServer
{
static void Main(string[] args)
{
TcpListener server = null;
try
{
int port = 8888;
IPAddress localAddr = IPAddress.Any;
server = new TcpListener(localAddr, port);
server.Start();
while (true)
{
Console.WriteLine("Waiting for a connection... and Listening through Port: " + port.ToString());
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected! + : " + DateTime.Now.ToString());
HandleClient(client);
}
}
catch (SocketException e)
{
Console.WriteLine($"SocketException: {e}");
}
finally
{
server?.Stop();
}
Console.WriteLine("\nServer stopped.");
}
static async void HandleClient(TcpClient client)
{
try
{
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine($"[{DateTime.Now}] Received sensor data: {data}");
await ProcessSensorReceivedData(data, stream);
Array.Clear(buffer, 0, buffer.Length);
}
Console.WriteLine($"[{DateTime.Now}] Waiting for a connection... and Listening through Port: 8888");
}
}
catch (SocketException ex)
{
Console.WriteLine($"[{DateTime.Now}] SocketException: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"[{DateTime.Now}] Exception: {ex}");
}
finally
{
client.Close();
}
}
static async Task<string> ProcessReceivedData(string data, NetworkStream stream)
{
string deviceID = ExtractDeviceID(data);
if (data.Contains("CONFIG") && data.Contains("TY"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("ICCID"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("CENTER"))
{
return $"[3G*{deviceID}*0006*CENTER]";
}
else if (data.Contains("SOS"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("SMSONOFF"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("REMOVESMS"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("LSN"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("LED"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("VON"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("HON"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("00B7* AL"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("HON"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("MOD"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("DND"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("BON"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("RESET"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("POWEROFF"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("WALKTIME"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("PEDO"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("CR"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("UPLOAD"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("00BF*AL_LTE,040522,025405"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("00BF*AL_LTE,040522,025606"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("00BF* AL_LTE,040522,025733,A,22.653534"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("LK"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("FIND"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("TKQ"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("TKQ2"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("UD_LTE"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("UD"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("SG") && data.Contains("LK"))
{
string response = await SendCommand(stream, data);
return response;
}
else if (data.Contains("UD") && data.Contains(",A,") && data.Contains(",N,") && data.Contains(",E,"))
{
string response = await SendCommand(stream, data);
return response;
}
else
{
return $"Unknown command: {data}";
}
}
static async Task ProcessSensorReceivedData(string data, NetworkStream stream)
{
try
{
Console.WriteLine($"[{DateTime.Now}] Sensor Data Received: {data}");
string deviceID = ExtractDeviceID(data);
if (data.Contains("CONFIG") && data.Contains("TY"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0008*CONFIG,1]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("ICCID"))
{
string imei = data.Split(',')[2];
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0016*RYIMEI,{imei}]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("CENTER"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0006*CENTER]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("SOS"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*SOS]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("SMSONOFF"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0008*SMSONOFF]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("REMOVESMS"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0009*REMOVESMS]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("LSN"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*LSN,3]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("LED"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*LED]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("VON"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*VON]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("HON"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*HON]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("00B7* AL"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0002*AL]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("HON"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*HON]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("MOD"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*MOD]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("DND"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*DND]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("BON"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*BON]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("RESET"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0005*RESET]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("POWEROFF"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0008*POWEROFF]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("WALKTIME"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0008*WALKTIME]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("PEDO"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0004*PEDO]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("CR"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*00E1*UD_LTE,100222,221554,V,00.000000,,00.0000000,,0.00,0.0,0.0,0,80,40,0,0,00000000,2,0,460,0,10142,225274433,4,10142,54313355,-4,4,,8c:14:b4:5e:4b:a8,-80,,d0:c7:c0:57:af:d2,-94,,60:3a:7c:34:05:c4,-104,,80:8f:1d:86:54:b5,-106,0.0]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("UPLOAD"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0006*UPLOAD]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("00BF*AL_LTE,040522,025405"))
{
var commandresponse = await SendCommand(stream, "0000 0000 0001 0000 0000 0000 0000 0000");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("00BF*AL_LTE,040522,025606"))
{
var commandresponse = await SendCommand(stream, "0000 0000 0000 0000 1000 0000 0000 0000");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("00BF* AL_LTE,040522,025733,A,22.653534"))
{
var commandresponse = await SendCommand(stream, "0000 0000 0000 0001 0000 0000 0000 0000");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("LK"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0002*LK]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("FIND"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0004*FIND]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("TKQ"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0003*TKQ]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("TKQ2"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0004*TKQ2]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("UD_LTE"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0005*UD_LTE]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("UD"))
{
var commandresponse = await SendCommand(stream, $"[3G*{deviceID}*0002*UD]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: " + commandresponse);
}
}
else if (data.Contains("SG") && data.Contains("LK"))
{
var commandresponse = await SendCommand(stream, $"[SG*{deviceID}*0002*LK]");
if (commandresponse != null)
{
Console.WriteLine($"[{DateTime.Now}] Command Sensor response: "+ commandresponse);
}
}
else if (data.Contains("UD") && data.Contains(",A,") && data.Contains(",N,") && data.Contains(",E,"))
{
}
Console.WriteLine($"[{DateTime.Now}] Finished Processing Sensor Data");
}
catch (Exception e)
{
Console.WriteLine($"[{DateTime.Now}] Error in Processing Sensor Data:" + e.Message);
}
}
static async Task<string> SendCommand(NetworkStream stream, string command)
{
Console.WriteLine($"[{DateTime.Now}] Insde Send Command Step 1 ");
string response =string.Empty;
try
{
Console.WriteLine($"[{DateTime.Now}] " + command + " Sending to Device Step 2...");
byte[] commandBytes = Encoding.ASCII.GetBytes(command);
Console.WriteLine($"[{DateTime.Now}] Convert the command string to bytes Step 3 ");
await stream.WriteAsync(commandBytes, 0, commandBytes.Length);
Console.WriteLine($"[{DateTime.Now}] Send the command to the device asynchronously Step 4 ");
byte[] responseBuffer = new byte[1024];
int bytesRead = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);
Console.WriteLine($"[{DateTime.Now}] Read the response from the device asynchronously Step 5 ");
response = Encoding.ASCII.GetString(responseBuffer, 0, bytesRead);
Console.WriteLine($"[{DateTime.Now}] Convert the response bytes to string Step 6 ");
Console.WriteLine($"[{DateTime.Now}] " + "Successfully sent to the Device...");
}
catch (Exception e)
{
Console.WriteLine($"Error Sending to Device Step 7" + e.Message);
}
return response;
}
static string ExtractDeviceID(string data)
{
int startIndex = data.IndexOf('*') + 1;
int endIndex = data.IndexOf('*', startIndex);
return data.Substring(startIndex, endIndex - startIndex);
}
}
What I have tried:
i tried a lot of things .
1) I tried to check the commands that are issued via tracing tool if they re the same as the one i am sending, but they are the same
2) `Put in a lot of logs to trace issues .
The Device only respond to anytracking. To make the device to only listen to my TCP Listener I am to change it via sms.