Communicating a GPS/GPRS/GSM tracker with the server

gprs

I am developing a vehicle tracking system which uses GPS/GPRS/GSM. The tracking device I am currently using is GV100, a GPS/GPRS/GSM tracker from Quectel ([www.quectel.com][1].)
I am not able to establish connection between the device and the backend server. My question particularly is:

  1. How do I send commands (AT Commands) to the device from the server?
  2. How do I accept the response (reports and acknowledgement messages) from the device to save it in the database?

I sent command to the device with the MGV100 Manage Tool (Software provided by Quectel) via serial port. And I got acknowledgement SMS message on GSM enabled mobile telephone. Now, I want to send message from the server and accept reply on the server (not by SMS). I don’t know how to send command and receive the reply. I have no previous experience in developing such systems.

It would be great if I can get a sample code and setup procedures if it requires.
Where can I get a relevant tutorial for the case I mentioned?


Thanks jhonkola


To understand how server receives and send data to the device, I decided to first implement the communication between the client (currently my PC) and server. Though my ultimate goal is communicating with the device, currently I am trying to establish connection from my PC to the server. If I succeed in this, I will strive to communicate to server from the device which needs IP address and port number of server to send and receive data.
This is my assumption how to do it:

  • I can open a port on the server from .cs code so as to communicate
    using TCP/UDP.
  • Client then can send and receive data via this
    port.
  • I can save the data sent from the client on server's file
    system and review it any time. (Am not storing the data in relational database because I don't want to bother about database issues now.)
    This is how I tried to implement:
    Server a C# Web Application:
    When a button is clicked it opens a port and listens to client

    protected void btnConnect_Click(object sender, EventArgs e)
    {
        try
        {
            continueListening = true;
            while (continueListening)
            {
                int port=Int32.Parse(txtPort.Text);
                lblOutput.Text = "Port is now " + port +". Waiting for connection";
                TcpListener myList = new TcpListener(IPAddress.Parse(txtIpAddress.Text), port);
                myList.Start();
                Socket s = myList.AcceptSocket();
                lblOutput.Text="Connection accepted from " + s.RemoteEndPoint;
    
                byte[] b = new byte[100];
                int k = s.Receive(b);
                lblOutput.Text = ("Recieved...");
                String obtainedText = "";
                for (int i = 0; i < k; i++)
                {
                    obtainedText = obtainedText + "  " + (Convert.ToChar(b[i]));
                }
                writeToTextFile("C:/Users/MekAtIbex/Desktop/TESTED/RECIEVED.txt", obtainedText);
                lblOutput.Text = obtainedText;
                ASCIIEncoding asen = new ASCIIEncoding();
                lblOutput.Text = lblOutput.Text +"  "+ ("The string was recieved by the server.");
                lblOutput.Text = lblOutput.Text +"  "+ ("\r\nSent Acknowledgement");
            } 
    

Client: C# Windows application

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                TcpClient tcpClient = new TcpClient();
                int port=Int32.Parse(txtPort.Text.Trim());

                tcpClient.Connect(txtIpAddress.Text, port);
                lblStatus.Text = ("Connected");
                Stream stm = tcpClient.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] bytesToSend = asen.GetBytes(txtData.Text);
                lblStatus.Text = ("Transmitting.....");

                stm.Write(bytesToSend, 0, bytesToSend.Length);

                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);

                for (int i = 0; i < k; i++)
                {
                    txtaResponse.Text = txtaResponse.Text + "\n" + "Res... " + new DateTime() + " " + Convert.ToChar(bb[i]);
                    Console.Write(Convert.ToChar(bb[i]));
                }
                tcpClient.Close();
            }

            catch (Exception ex)
            {
                lblStatus.Text = ("Connected");
                txtaRequest.Text = txtaRequest.Text + "\n" + "Err... " + new DateTime() + " " + ex.StackTrace;
            }
        }

My current questions are:

  1. Is my assumption correct? If not how should I do it?
  2. I have tried to save it using the above code but I didn't got the file.
  3. What is the advantage and disadvantage of using UDP in comparison TCP for tracking applications?
    I have browsed well, but I couldn't find a place for a good start. And, as I have no experience in such applications, I couldn't debug my application.

Best Answer

My current questions are:

Is my assumption correct? If not how should I do it?

Yes, your basic assumptions are correct. The server would open a listening port and then the client could connect to this port and drop off data as needed. You can have the server log this to a file for later review too.

I have tried to save it using the above code but I didn't got the file.

Is the file already created? The method you have will fail if the file is not existent on the system.

What is the advantage and disadvantage of using UDP in comparison TCP for tracking applications?

UPD is less expensive in terms of network setup. It is the "fast and dirty" method of communication. The downside is that you may not get every message properly delivered. In some applications, this just doesn't matter and the benefits are worth this cost.

Now a few things I'd change:

Change IPAddress.Parse(txtIpAddress.Text) to IPAddress.Any

This will allow your listener the broadest ability to catch incoming messages and will most likely not effect other systems (since this is essentially your first networking program).

You'll also want to make your listener spawn a thread to handle the file writing and then go back to listening. This is a very standard practice and allows for servers to handle multiple connections.

Related Topic