コード例 #1
0
ファイル: SerialInterface.cs プロジェクト: vdevan/PiNavigator
        public async Task <string> ReadAsync(uint bufferLength)
        {
            String   str    = ""; // new StringBuilder();
            GPSData  gps    = new GPSData();
            GPSSpeed gs     = new GPSSpeed();
            string   retVal = "GPS not initialised";

            try
            {
                // Once we have written the contents successfully we load the stream. By getting the
                //right bufferlength, we can get the read more accurate without over or under buffer read
                bufferLength = await dr.LoadAsync(bufferLength);

                while (dr.UnconsumedBufferLength > 0)
                {
                    str += dr.ReadString(bufferLength) + "\n";
                    //if (str.Contains("GGA"))
                    //break;
                }

                if (str == "")
                {
                    return("GPS Data not available");
                }

                var dataGPS   = str.Substring(str.IndexOf("GGA")).Split('\n');
                var dataSpeed = str.Substring(str.IndexOf("VTG")).Split('\n');


                if (dataGPS.Count() > 0 && gps.SetData(dataGPS[0]))
                {
                    retVal = string.Format("Time: {0}; Latitude: {1}; Longitude: {2}; Elevation: {3}; Satellites: {4}; ",
                                           gps.gpsTime, gps.latitude, gps.longitude, gps.elevation, gps.satellites);
                    latLong = string.Format("{0}; {1}", gps.latitude, gps.longitude);
                }

                if (dataSpeed.Count() > 0 && gs.SetData(dataSpeed[0]))
                {
                    retVal += string.Format("Speed in Km: {0}", gs.SpeedKM);
                }

                Speed = gs.SpeedKM;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Reading serial data failed!" + ex.Message);
                return("");
            }

            return(retVal);
        }