1

I am getting data from avl system as you can see with this format :

*HQ,4106016320,V1,090458,A,5257.4318,N,15840.4221,E,000.00,000,101115,FFFFFBFF,250,01,0,0,5#

As you can see the data that I get from the gps is :

enter image description here

As you can see in some lines the data is unreadable. Why is this happening?

My server that gets the data is like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace smartparckingHandlerServer
{
    public partial class Form1 : Form
    {
        public AsyncCallback pfnWorkerCallBack;
        private Socket m_mainSocket;
        private Socket[] m_workerSocket = new Socket[25];
        private int m_clientCount = 0;
        private string ipaddress;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            startfun();
        }
        public void startfun()
        {
            try
            {
                // DrawMapPersian();
                ipaddress = "127.0.0.1";
                // Check the port value

                string portStr = "5000";
                int port = System.Convert.ToInt32(portStr);
                // Create the listening socket...
                m_mainSocket = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Stream,
                                          ProtocolType.Tcp);
                IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
                // Bind to local IP Address...
                m_mainSocket.Bind(ipLocal);
                listBox1.Items.Add("Server Started...");
                // Start listening...
                m_mainSocket.Listen(20);
                listBox1.Items.Add("Server Listening for ...");

                // Create the call back for any client connections...
                m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
            catch (Exception qqq)
            {
                using (StreamWriter writer =
        new StreamWriter(@"f:\a.txt"))
                {
                    writer.Write(qqq.Message);

                }
            }
        }
        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                m_workerSocket[m_clientCount] = m_mainSocket.EndAccept(asyn);
                // Let the worker Socket do the further processing for the 
                // just connected client
                WaitForData(m_workerSocket[m_clientCount]);
                // Now increment the client count
                ++m_clientCount;
                // Display this client connection as a status message on the GUI    
                String str = String.Format("Client # {0} connected", m_clientCount);


                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
                using (StreamWriter writer =
                    new StreamWriter(@"f:\con.txt"))
                {
                    writer.Write("connect to client");
                }
            }
            catch (Exception qqq)
            {
                using (StreamWriter writer =
        new StreamWriter(@"f:\a.txt"))
                {
                    writer.Write(qqq.Message);

                }
            }


        }
        public class SocketPacket
        {
            public System.Net.Sockets.Socket m_currentSocket;
            public byte[] dataBuffer = new byte[200];
        }
        public void WaitForData(System.Net.Sockets.Socket soc)
        {
            try
            {
                if (pfnWorkerCallBack == null)
                {
                    // Specify the call back function which is to be 
                    // invoked when there is any write activity by the 
                    // connected client
                    pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                }
                SocketPacket theSocPkt = new SocketPacket();
                theSocPkt.m_currentSocket = soc;
                // Start receiving any data written by the connected client
                // asynchronously
                soc.BeginReceive(theSocPkt.dataBuffer, 0,
                                   theSocPkt.dataBuffer.Length,
                                   SocketFlags.None,
                                   pfnWorkerCallBack,
                                   theSocPkt);
            }
            catch (Exception qqq)
            {
                using (StreamWriter writer =
        new StreamWriter(@"f:\a.txt"))
                {
                    writer.Write(qqq.Message);

                }
            }

        }
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                int iRx = 0;
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream 
                // by the client
                iRx = socketData.m_currentSocket.EndReceive(asyn);
                string res = GetParameters(socketData.dataBuffer);

                Console.WriteLine(res.ToString());
                //char[] chars = new char[iRx + 1];
                //System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                //int charLen = d.GetChars(socketData.dataBuffer,
                //                         0, iRx, chars, 0);
                //System.String szData = new System.String(chars);

                this.Invoke(new MethodInvoker(delegate ()
                {

                    listBox1.Items.Add("clinet's data:" + res);

                }));

                // Continue the waiting for data on the Socket
                WaitForData(socketData.m_currentSocket);
                //socketData = null;
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
            }
            catch (Exception qqq)
            {
                using (StreamWriter writer =
        new StreamWriter(@"f:\a.txt"))
                {
                    writer.Write(qqq.Message);

                }
            }
        }
        public string GetParameters(byte[] buf)
        {


            string result = System.Text.Encoding.ASCII.GetString(buf);


            return result;
        }
    }
}

When I move the GPS it returns unreadable data. Why?

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.