来自gps的数据有时会更改为不可读的格式

时间:2016-07-14 13:23:58

标签: c# gps hex client-server serversocket

我从avl系统获取数据,你可以看到这种格式:

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

正如您所看到的,我从gps获得的数据是:

enter image description here

正如您在某些方面所看到的那样,数据是不可读的。为什么会出现这个问题?

获取数据的服务器是这样的:

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

当我移动GPS时,它会返回不可读的数据。为什么?

1 个答案:

答案 0 :(得分:0)

这是一个长镜头,但尝试改变

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

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

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