Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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[^]

C#
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

        {

            // Set the TCP IP and port

            int port = 8888;

            IPAddress localAddr = IPAddress.Any;

            // TcpListener server

            server = new TcpListener(localAddr, port);

            // Start listening for client requests

            server.Start();

            // Enter the listening loop

            while (true)

            {

                Console.WriteLine("Waiting for a connection... and Listening through Port: " + port.ToString());

                // Perform a blocking call to accept requests

                TcpClient client = server.AcceptTcpClient();

                Console.WriteLine("Connected! + : " + DateTime.Now.ToString());

                // Handle the client request asynchronously

                HandleClient(client);

            }

        }

        catch (SocketException e)

        {

            Console.WriteLine($"SocketException: {e}");

        }

        finally

        {

            // Stop listening for new clients

            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}");

                    // Parse and process sensor data

                      await ProcessSensorReceivedData(data, stream);

                    // Clear the buffer after processing

                    Array.Clear(buffer, 0, buffer.Length);

                }

                // After handling the client request, wait for a new connection

                Console.WriteLine($"[{DateTime.Now}] Waiting for a connection... and Listening through Port: 8888");

            }

        }

        catch (SocketException ex)

        {

            // Handle socket exceptions

            Console.WriteLine($"[{DateTime.Now}] SocketException: {ex}");

        }

        catch (Exception ex)

        {

            // Log any other exceptions that occur during handling

            Console.WriteLine($"[{DateTime.Now}] Exception: {ex}");

        }

        finally

        {

            // Close the client connection

            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;

            // Process CONFIG command

            //return $"[3G*{deviceID}*0008*CONFIG,1]";

        }

        else if (data.Contains("ICCID"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process ICCID command

            // string imei = data.Split(',')[2];

            // return $"[3G*{deviceID}*0016*RYIMEI,{imei}]";

        }

        else if (data.Contains("CENTER"))

        {

            // Process CENTER command

            return $"[3G*{deviceID}*0006*CENTER]";

        } 

        else if (data.Contains("SOS"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process SOS command

            //return $"[3G*{deviceID}*0003*SOS]";

        } 

        else if (data.Contains("SMSONOFF"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process SMSONOFF command

            // return $"[3G*{deviceID}*0008*SMSONOFF]";

        }

        else if (data.Contains("REMOVESMS"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process REMOVESMS command

            // return $"[3G*{deviceID}*0009*REMOVESMS]";

        }

        else if (data.Contains("LSN"))

        {

            // Process LSN command

            //return $"[3G*{deviceID}*0003*LSN]";

            string response = await SendCommand(stream, data);

            return response;

        }

        else if (data.Contains("LED"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process LED command

            //return $"[3G*{deviceID}*0003*LED]";

        }

        else if (data.Contains("VON"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process VON command

            // return $"[3G*{deviceID}*0003*VON]";

        }

        else if (data.Contains("HON"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process HON command

            //return $"[3G*{deviceID}*0003*HON]";

        }

        else if (data.Contains("00B7* AL"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process HON command

            //  return $"[3G*{deviceID}*0002*AL]";

        }

        else if (data.Contains("HON"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process HON command

            //return $"[3G*{deviceID}*0003*HON]";

        }

        else if (data.Contains("MOD"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process MOD command

            // return $"[3G*{deviceID}*0003*MOD]";

        }

        else if (data.Contains("DND"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process DND command

            //return $"[3G*{deviceID}*0003*DND]";

        }

        else if (data.Contains("BON"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process BON command

            //return $"[3G*{deviceID}*0003*BON]";

        }

        else if (data.Contains("RESET"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process RESET command

            //return $"[3G*{deviceID}*0005*RESET]";

        }

        else if (data.Contains("POWEROFF"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process POWEROFF command

            // return $"[3G*{deviceID}*0008*POWEROFF]";

        }

       

        else if (data.Contains("WALKTIME"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process WALKTIME command

            // return $"[3G*{deviceID}*0008*WALKTIME]";

        }

        else if (data.Contains("PEDO"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process PEDO command

            //return $"[3G*{deviceID}*0004*PEDO]";

        }

        else if (data.Contains("CR"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process CR command 

            //return $"[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]";

        }

        else if (data.Contains("UPLOAD"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process UPLOAD command

            //return $"[3G*{deviceID}*0006*UPLOAD]";

        }

        else if (data.Contains("00BF*AL_LTE,040522,025405"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process AL_LTE command (Alarm data)

            // return "0000 0000 0001 0000 0000 0000 0000 0000";

        }

        else if (data.Contains("00BF*AL_LTE,040522,025606"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process AL_LTE command (Alarm data)

            // return "0000 0000 0000 0000 1000 0000 0000 0000";

        }

        else if (data.Contains("00BF* AL_LTE,040522,025733,A,22.653534"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process AL_LTE command (Alarm data)

            // return "0000 0000 0000 0001 0000 0000 0000 0000";

        }

        else if (data.Contains("LK"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process LK command

            // return $"[3G*{deviceID}*0002*LK]";

        }

        else if (data.Contains("FIND"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process FIND command

            // Activate ring tone to find the device

            //return $"[3G*{deviceID}*0004*FIND]";

        }

        else if (data.Contains("TKQ"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process TKQ command

            // return $"[3G*{deviceID}*0003*TKQ]";

        }

        else if (data.Contains("TKQ2"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process TKQ2 command

            //return $"[3G*{deviceID}*0004*TKQ2]";

        }

        else if (data.Contains("UD_LTE"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process UD_LTE command

            //return $"[3G*{deviceID}*0005*UD_LTE]";   // no Server response on the document 

        }

        else if (data.Contains("UD"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process UD comma

            // Process UD command

            // return $"[3G*{deviceID}*0002*UD]"; // no Server response on the document 

        }

        else if (data.Contains("SG") && data.Contains("LK"))

        {

            string response = await SendCommand(stream, data);

            // Process UD command

            return response;

            //$"[SG*{deviceID}*0002*LK]"; // no Server response on the document 

        }

      

        else if (data.Contains("UD") && data.Contains(",A,") && data.Contains(",N,") && data.Contains(",E,"))

        {

            string response = await SendCommand(stream, data);

            return response;

            // Process UD command

            //return $"[]"; // no Server response on the document 

        }

        else

        {

            // Unknown command

            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,"))

            {

                //  No Response 

            }

            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...");

            // Convert the command string to bytes

            byte[] commandBytes = Encoding.ASCII.GetBytes(command);

            Console.WriteLine($"[{DateTime.Now}]  Convert the command string to bytes Step 3 ");

            // Send the command to the device asynchronously

            await stream.WriteAsync(commandBytes, 0, commandBytes.Length);

            Console.WriteLine($"[{DateTime.Now}]  Send the command to the device asynchronously Step 4 ");

            // Read the response from the device asynchronously

            byte[] responseBuffer = new byte[1024]; // Adjust buffer size as per your requirement

            int bytesRead = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);

            Console.WriteLine($"[{DateTime.Now}]   Read the response from the device asynchronously Step 5 ");

            // Convert the response bytes to string

            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...");

            // Return the response received from the device

             

            //////////////

        }

        catch (Exception e) 

        {

            Console.WriteLine($"Error Sending to Device Step 7" + e.Message);

        }

        return response;

    }

    static string ExtractDeviceID(string data)

    {

        // Extract device ID from the data string

        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
Posted
Updated 5-Apr-24 13:49pm
v2

1 solution

Go back to the manufacturers and ask for tech support - you would need the device available to you and running to stand any chance of working out what is wrong and we have neither any access to it nor any idea what it expects!

It's possible the tech support people can provide sample code that talks to the device that you can build upon to get your system working. But we can't!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900