void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            #region LER SIRF BINARY

            //byte[] bData = new byte[1];
            //port.Read(bData, 0, 1);

            //binary = binary + BitConverter.ToString(bData);

            //if (binary.EndsWith("B0B3"))
            //{
            //    //remove start sequence
            //    binary = binary.Substring(4);

            //    //pega o tamanho do payload
            //    string payloadLength = binary.Substring(0, 4);
            //    int length = Convert.ToInt32(payloadLength, 16) * 2;
            //    binary = binary.Substring(4);
            //    //pegar o payload
            //    payloadMessage = binary.Substring(0, length);
            //    binary = binary.Substring(length);
            //    //pega o checksum
            //    string checksum = binary.Substring(0, 4);

            //    if (lstAvisos.InvokeRequired)
            //    {
            //        lstAvisos.Invoke(new Action(() => { lstAvisos.Items.Add(payloadMessage); }));
            //        lstAvisos.Invoke(new Action(() => { lstAvisos.SelectedIndex = lstAvisos.Items.Count - 1; }));
            //    }

            //    binary = "";

            //    //é uma measure navigation data out - message id 2
            //    if (payloadMessage.StartsWith("02"))
            //    {
            //        payloadMessage = payloadMessage.Substring(2);
            //        string xPosition = payloadMessage.Substring(0, 4);
            //        payloadMessage = payloadMessage.Substring(4);
            //        string yPosition = payloadMessage.Substring(0, 4);
            //    }
            //}

            #endregion

            #region LER NMEA

            byte[] bData = new byte[1];
            try
            {
                port.Read(bData, 0, 1);

                nmea = nmea + System.Text.Encoding.UTF8.GetString(bData, 0, 1);

                if (System.Text.Encoding.UTF8.GetString(bData, 0, 1).Equals("\n") && nmea.Length > 1)
                {
                    if (txtLat.InvokeRequired)
                    {
                        if (nmea.Contains("$GPGGA"))
                        {
                            GeoFramework.Gps.Nmea.GpggaSentence gpgga = new GeoFramework.Gps.Nmea.GpggaSentence(nmea);
                            GeoFramework.Position position = gpgga.Position;
                            base.lat = Math.Round(position.Latitude.DecimalDegrees, 6).ToString();
                            base.txtSatelites = gpgga.FixedSatelliteCount.ToString();
                            base.lon = Math.Round(position.Longitude.DecimalDegrees, 6).ToString();
                            accuracy = Convert.ToDouble(gpgga.HorizontalDilutionOfPrecision.EstimatedPrecision.ToMeters().ToString());
                            txtLat.Invoke(new Action(() => { txtLat.Text = base.lat; }));
                            txtLon.Invoke(new Action(() => { txtLon.Text = base.lon; }));
                            txtSatelites.Invoke(new Action(() => { txtSatelites.Text = "" + base.txtSatelites; }));
                            txtAcuracia.Invoke(new Action(() => { txtAcuracia.Text = "" + accuracy; }));

                            if (Math.Round(accuracy, 2) < 20)
                            {
                                this.BeginInvoke(new MethodInvoker(delegate()
                                {
                                    if (!menuItemSalvar.Enabled)
                                    {
                                        menuItemSalvar.Enabled = true;
                                        menuItemSalvar.Text = "Salvar";
                                        MessageBeep();
                                    }
                                }
                                ));

                            }
                            else
                            {
                                this.BeginInvoke(new MethodInvoker(delegate()
                                {
                                    menuItemSalvar.Enabled = false;
                                    menuItemSalvar.Text = "Salvar (Dados incompletos)";
                                }
                                ));
                            }
                        }
                    }
                    nmea = "";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            //if (port.IsOpen)
            //{
            //    System.IO.Ports.SerialPort sp = (System.IO.Ports.SerialPort)sender;
            //    string s = sp.ReadExisting();
            //    if (lstAvisos.InvokeRequired)
            //    {
            //        lstAvisos.Invoke(new Action(() => { lstAvisos.Items.Add(s); }));
            //        lstAvisos.Invoke(new Action(() => { lstAvisos.SelectedIndex = lstAvisos.Items.Count - 1; }));
            //    }
            //}
        }
示例#2
0
        private void timerCoordenada_Tick(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.DoEvents();

            System.Net.Sockets.NetworkStream stream =
                Usuario.btClient.GetStream();

            //primeiro pega a data e hora com GPZDA
            while (!displayString.Contains("$GPRMC"))
            {
                byte[] data = new byte[1];
                int numOfBytesRead = stream.Read(data, 0, data.Length);

                displayString = displayString + Encoding.ASCII.GetString(data, 0, numOfBytesRead);
            }
            displayString = "$GPRMC";
            while (!displayString.Contains("\r"))
            {
                byte[] data = new byte[1];
                int numOfBytesRead = stream.Read(data, 0, data.Length);
                displayString = displayString + Encoding.ASCII.GetString(data, 0, numOfBytesRead);
            }
            gprmc = displayString;
            try
            {
                GeoFramework.Gps.Nmea.GprmcSentence gprmcSetence =
                    new GeoFramework.Gps.Nmea.GprmcSentence(gprmc);

                dataHora = gprmcSetence.UtcDateTime;
            }
            catch (Exception ex)
            {
                Library.log(ex.Message, "gprmc sentença falhou", this.Name);
            }

            //GPGGA
            displayString = "";
            while (!displayString.Contains("$GPGGA"))
            {
                byte[] data = new byte[1];
                int numOfBytesRead = stream.Read(data, 0, data.Length);

                displayString = displayString + Encoding.ASCII.GetString(data, 0, numOfBytesRead);
            }

            displayString = "$GPGGA";

            while (!displayString.Contains("\r"))
            {
                byte[] data = new byte[1];
                int numOfBytesRead = stream.Read(data, 0, data.Length);
                displayString = displayString + Encoding.ASCII.GetString(data, 0, numOfBytesRead);
            }

            gpgga = displayString;
            try
            {
                GeoFramework.Gps.Nmea.GpggaSentence gpggaSetence =
                    new GeoFramework.Gps.Nmea.GpggaSentence(gpgga);

                accuracy = gpggaSetence.HorizontalDilutionOfPrecision.Value;
                lat = Math.Round(gpggaSetence.Position.Latitude.DecimalDegrees, 10).ToString();
                lon = Math.Round(gpggaSetence.Position.Longitude.DecimalDegrees, 10).ToString();

                LatLon latlon = new LatLon();
                latlon.Lat = lat;
                latlon.Lon = lon;

                filaLatLon.Enqueue(latlon);

                if (filaLatLon.Count == 4)
                {
                    if ((filaLatLon.All(l => l.Lat == latlon.Lat)) &&
                        (filaLatLon.All(l => l.Lon == latlon.Lon)))
                    {
                        salvar = true;
                    }
                    else
                    {
                        salvar = false;
                    }

                    filaLatLon.Dequeue();
                }

                //quantSatelites = nmea[7];
                quantSatelites = gpggaSetence.FixedSatelliteCount.ToString();
                if (Int32.Parse(quantSatelites) < 4)
                {
                    txtSatelites = quantSatelites + " (Ruim)";
                }
                else
                {
                    txtSatelites = quantSatelites + " (Bom)";
                }

                displayString = string.Empty;

                atualizaInfoGPS();
            }
            catch (Exception ex)
            {
                Library.log(ex.Message, "gpgga sentença falhou", this.Name);
            }
        }