来自gps的数据有时会更改为不可读的格式 [英] data from gps sometimes changed to unreadable format

查看:231
本文介绍了来自gps的数据有时会更改为不可读的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从avl系统获取数据,如下所示:

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#

正如你可以看到,我从gps获得的数据是:

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

正如你可以看到在某些行数据是不可读的。这个问题发生了什么?

As you can see in some lines the data is unreadable .why this problem happened?

我的服务器数据如下:

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

返回不可读的数据。

推荐答案

这是一个很长的时间,但尝试更改

It's a longshot, but try changing

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

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

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

这篇关于来自gps的数据有时会更改为不可读的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆