public GPSPosition GetPosition() { GPSPosition ret = null; bool posAcquired = false; lock (this) { if (IsOpen()) { int cb = Marshal.SizeOf(typeof(GPSPosition)); IntPtr ptr = LocalAlloc(LPTR, cb); ret = new GPSPosition(); ret.dwVersion = 1; ret.dwSize = Marshal.SizeOf(typeof(GPSPosition)); // Marshal our data to the native pointer we allocated. Marshal.StructureToPtr(ret, ptr, false); Int32 i = GPSGetPosition(hGPSDevice, ptr, mMaximunAge, 0); if (i == 0) { Marshal.PtrToStructure(ptr, ret); posAcquired = true; // COMENTADO. // Ahora la fecha la sacamos del SQL Server. //' '' Seteo la hora actual en el sistema con la que tienen los satélites // ''If ret.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_UTC_TIME) Then // '' If ret.stUTCTime.wYear >= 2007 Then // '' 'Si la diferencia de tiempo es de más de 2 minutos, actualiza la hora // '' If Math.Abs(ret.Time.Subtract(DateTime.UtcNow).Minutes) > 2 Then // '' Dim rc As UInteger // '' rc = SetSystemTime(ret.stUTCTime) // '' End If // '' End If // ''End If } else { MessageBox.Show("Error# " + i.ToString()); } LocalFree(ptr); } } if (posAcquired == false) { if (PositionReceived != null) { PositionReceived(ret); } } return(ret); }
public static bool IsPositionValid(GPSPosition position, bool ValidateFix, float MAXDOP) { bool posOk = false; if ((position != null)) { if (position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_UTC_TIME) & position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_HORIZONTAL_DILUTION_OF_PRECISION) & position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_LATITUDE) & position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_LONGITUDE) & (position.fixType >= GPSFixType.XyD | ValidateFix == false)) { if ((MAXDOP >= position.flHorizontalDilutionOfPrecision) | MAXDOP == -1) { posOk = true; } } } return(posOk); }
private void ticking() { try { if (!glbGPS.IsOpen()) { ST("No esta abierto el GPS!!!"); } GPSPosition Position = glbGPS.GetPosition(); if ((Position != null)) { HDOPLabel.Text = Position.flHorizontalDilutionOfPrecision.ToString("0.00"); //ErrorLabel.Text = (Position.flHorizontalDilutionOfPrecision * 6) + " m"; SatelitesLabel.Text = Position.dwSatelliteCount.ToString(); if (Position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_LONGITUDE)) { LongLabel.Text = Position.dblLongitude.ToString("0.0000000"); } else { LongLabel.Text = ""; } if (Position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_LATITUDE)) { LatLabel.Text = Position.dblLatitude.ToString("0.0000000"); } else { LatLabel.Text = ""; } try { if (Position.isValidField(GPSPosition.GPS_VALID_FIELDS.GPS_VALID_UTC_TIME)) { StampLabel.Text = Position.Time.ToString("yyyy/MM/dd HH:mm"); } else { StampLabel.Text = ""; } } catch (Exception ex) { StampLabel.Text = ex.ToString(); //Position.Time.ToString(); } switch (Position.fixType) { case GPSFixType.Unknown: FixTypeLabel.Text = ""; break; case GPSFixType.XyD: FixTypeLabel.Text = "2D"; break; case GPSFixType.XyzD: FixTypeLabel.Text = "3D"; break; } switch (Position.fixQuality) { case GPSFixQuality.DGps: FixTypeLabel.Text += " DGPS"; break; case GPSFixQuality.Gps: FixTypeLabel.Text += " GPS"; break; } lastPosition = Position; if (glbGPS.IsPositionValid(Position)) { pnlFixed.BackColor = Color.LimeGreen; StatusLabel.BackColor = Color.LimeGreen; ST("Posicion obtenida"); gotFix = true; btAceptar.Enabled = true; Time = DateTime.Now; // timer1.Enabled = false; } else { pnlFixed.BackColor = Color.OrangeRed; StatusLabel.BackColor = Color.OrangeRed; if (!odd) { odd = true; ST("Esperando posición"); } else { odd = false; ST("Buscando satélites"); } gotFix = false; btAceptar.Enabled = false; } } else { //GrabarButton.Enabled = False pnlFixed.BackColor = Color.Red; } } catch (Exception ex) { ST(ex.Message.ToString()); } }