How do I continuously read and parse high speed SerialPort data

与世无争的帅哥 提交于 2019-12-10 16:56:51

问题


I am trying to write C# code that takes serialport data from a high speed GPS device and parse the data to grab coordinates.

The problem with this compared to other serial GPS devices is that it spits out 5-6 lines of data every 100 milliseconds instead of the same every second. So basically, every 0.1 seconds, I get 5 or 6 lines of data burst, which will last continuously as long as the device is turned on.

When I tried it on a simulated standard-speed GPS device, it works just fine.

This is me getting a 5 line burst once every second. But when it speeds up 10 fold, it stops working. Basically, I get the effect of SerialDataReceivedEventHandler not triggering at all.

So what is the best way to get a continuous read on what is essentially a superfast serialport dump of data?

My code is below. For you GPS guys, this particular receiver has a baudrate of a non-standard 115200 rather than the standard 4800. I verified that it works with PuTTY as well as off-the-shelf GPS software for Windows.

    public List<double[]> coords = new List<double[]>();
    SerialPort com;
        ...
        try
        {
            com = new SerialPort(COMPORT);
            com.BaudRate = 115200;
            com.DataBits = 8;
            com.StopBits = StopBits.One;
            com.Handshake = Handshake.None;
            com.Open();

            com.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
        }
    }

    public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort port = (SerialPort)sender;
        double longitude = -1000;
        double latitude = -1000;
        double altitude = -1000;
        string sentence = port.ReadLine();
        if (sentence.StartsWith("$GPGGA"))
        {
            string[] values = sentence.Split(',');

            if (values[2].Length > 1 && values.Count() > 10)
            {
                latitude = double.Parse(values[2].Substring(0, values[2].IndexOf('.') - 2));
                latitude += double.Parse(values[2].Substring(values[2].IndexOf('.') - 2)) / 60;
                if (values[3] == "S")
                    latitude = 0 - latitude;
                longitude = double.Parse(values[4].Substring(0, values[4].IndexOf('.') - 2));
                longitude += double.Parse(values[4].Substring(values[4].IndexOf('.') - 2)) / 60;
                if (values[5] == "W")
                    longitude = 0 - longitude;
                altitude = double.Parse(values[9]);

                coords.RemoveAt(0);
                coords.Add(new double[] { longitude, latitude, altitude });
            }

            else
            {
                coords.RemoveAt(0);
                coords.Add(coords.Last());
            }
        }
    }

来源:https://stackoverflow.com/questions/32021784/how-do-i-continuously-read-and-parse-high-speed-serialport-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!