2016-08-29 1 views
1

CE OS 5.2를 실행하는 Motorola MC65 장치가 있습니다. 나는 장치를위한 유효한 시간을 얻고 싶다; 나는이 데이터를 위성으로부터 수신했기 때문에 위치에 대한 정확한 시간을 알 수 있었다고 느꼈다. ...GPS가 Windows Mobile CE 5.2에서 잘못된 타임 스탬프를 반환합니다.

나는 정확한 날짜를 받고 있지만 시간은 3 시간 씩 떨어져있다. 나는 동부 표준시 시간대이며, 장치의 시간대가 올바로 설정되어 있고 장치의 시간이 오전 11시 56 분 (알 수 없음)이며 GPS는 오후 4:15을 반환합니다 ...

여기 내 현재입니다 코드, 나는 Microsoft에서 제공하는 GPS 중급 드라이버 (https://msdn.microsoft.com/en-us/library/bb202128.aspx)를 사용하게 GPS를 통해 시간을 얻을 : 또한

public DateTime GetGPSTime() 
    { 
     Boolean satsInView = false; 
     Gps g = new Gps(); 
     g.DeviceStateChanged += new DeviceStateChangedEventHandler(g_DeviceStateChanged); 
     g.Open(); 
     Thread.Sleep(4000); 

     if (g.Opened) 
     { 
      if (deviceState.ServiceState == GpsServiceState.On) 
      { 
       GpsPosition pos = g.GetPosition(TimeSpan.Zero); //No Delay in time 
       if (pos != null) 
       { 
        //First check that we have sats visible for good GPS signal. 
        if (pos.SatellitesInViewCountValid) 
        { 
         if (pos.SatellitesInViewCount >= 1) 
          satsInView = true; 
         else 
          satsInView = false; 
        } 

        if (pos.TimeValid && satsInView) 
        { 
         g.Close(); 
         g.DeviceStateChanged -= g_DeviceStateChanged; 
         return pos.Time; //Returned time obtained from GPS Obj 
        } 
       } 
       g.Close(); 
       g.DeviceStateChanged -= g_DeviceStateChanged; 
       return Helper.GetSystemTimeToNow(); //If GPS obj is null, return current system time. 
      } 
     } 

     g.Close(); 
     g.DeviceStateChanged -= g_DeviceStateChanged; 
     return Helper.GetSystemTimeToNow(); //If GPS obj is null, return current system time. 
    } 

을, 여기에 마이크로 소프트의 공식 Interme의 방법입니다 diate GPS 코드 샘플. GPSposition.validField은 시간이 유효 함을 말한다면이 방법은, GpsPosition pos = g.GetPosition(TimeSpan.Zero);

/// <summary> 
     /// Get the position reported by the GPS receiver that is no older than 
     /// the maxAge passed in 
     /// </summary> 
     /// <param name="maxAge">Max age of the gps position data that you want back. 
     /// If there is no data within the required age, null is returned. 
     /// if maxAge == TimeSpan.Zero, then the age of the data is ignored</param> 
     /// <returns>GpsPosition class with all the position details</returns> 
     public GpsPosition GetPosition(TimeSpan maxAge) 
     { 
      GpsPosition gpsPosition = null; 
      if (Opened) 
      { 
       // allocate the necessary memory on the native side. We have a class (GpsPosition) that 
       // has the same memory layout as its native counterpart 
       IntPtr ptr = Utils.LocalAlloc(Marshal.SizeOf(typeof(GpsPosition))); 

       // fill in the required fields 
       gpsPosition = new GpsPosition(); 
       gpsPosition.dwVersion = 1; 
       gpsPosition.dwSize = Marshal.SizeOf(typeof(GpsPosition)); 

       // Marshal our data to the native pointer we allocated. 
       Marshal.StructureToPtr(gpsPosition, ptr, false); 

       // call native method passing in our native buffer 
       int result = GPSGetPosition(gpsHandle, ptr, 500000, 0); 
       if (result == 0) 
       { 
        // native call succeeded, marshal native data to our managed data 
        gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition)); 

        if (maxAge != TimeSpan.Zero) 
        { 
         // check to see if the data is recent enough. 
         if (!gpsPosition.TimeValid || DateTime.Now - maxAge > gpsPosition.Time) 
         { 
          gpsPosition = null; 
         } 
        } 
       } 
       else if (result == 87) // ERROR_INVALID_PARAMETER) 
       { 
        // 
        // TEMPORARY HACK 
        // 
        // http://blogs.msdn.com/cenet/archive/2007/12/06/gpsid-problem-workaround-on-recent-wm6-release.aspx 
        // 

        Utils.LocalFree(ptr); 

        // allocate the necessary memory on the native side. We have a class (GpsPosition) that 
        // has the same memory layout as its native counterpart 
        ptr = Utils.LocalAlloc(376); 

        // fill in the required fields 
        gpsPosition = new GpsPosition(); 
        gpsPosition.dwVersion = 1; 
        gpsPosition.dwSize = 376; 

        // Marshal our data to the native pointer we allocated. 
        Marshal.StructureToPtr(gpsPosition, ptr, false); 

        // call native method passing in our native buffer 
        result = GPSGetPosition(gpsHandle, ptr, 500000, 0); 

        if (result == 0) 
        { 
         // native call succeeded, marshal native data to our managed data 
         gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition)); 

         if (maxAge != TimeSpan.Zero) 
         { 
          // check to see if the data is recent enough. 
          if (!gpsPosition.TimeValid || DateTime.Now - maxAge > gpsPosition.Time) 
          { 
           gpsPosition = null; 
          } 
         } 
        } 
       } 

       // free our native memory 
       Utils.LocalFree(ptr); 
      } 

      return gpsPosition;   
     } 

답변

1

는 GPS 시간은 잘한다, 위의 내 코드에 의해 호출됩니다.

GPS 시간은 항상 UTC입니다. 시스템 시간을 설정하는 유일한 방법이며, 시스템 시간도 항상 UTC입니다. DST 시간대에서 외부 DST 시간대로 또는 그 반대로 시스템 시간을 설정하는 경우가 있으므로 시스템 시간은 항상 두 번 설정해야합니다! 시간으로 테스트 할 때, 당신은 GPS (UTC) 어떤 장치가 말한다에게서 얻는 시간을 유의하시기 바랍니다 https://github.com/hjgode/win-mobile-code/blob/master/gps8/Gps8/GPS_Sample8/GPSForm1.cs

에 대한 자세한 내용은 내 블로그 ...

private void SetTimeToGPS(DateTime UTCtime) 
    { 
     if (m_SetTime) 
     { 
      // Get the local time zone and a base Coordinated Universal 
      // Time (UTC). 
      TimeZone localZone = TimeZone.CurrentTimeZone; 
      DateTime baseUTC = UTCtime; // new DateTime(2000, 1, 1); 

      System.Diagnostics.Debug.WriteLine("\nLocal time: {0}\n", 
       localZone.StandardName); 

      // Calculate the local time and UTC offset. 
      DateTime localTime = localZone.ToLocalTime(baseUTC); 
      TimeSpan localOffset = 
       localZone.GetUtcOffset(localTime); 

      System.Diagnostics.Debug.WriteLine(string.Format("{0,-20:yyyy-MM-dd HH:mm}" + 
       "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
       baseUTC, localTime, localOffset, 
       localZone.IsDaylightSavingTime(localTime))); 
      //adjust the clock 
      //localTime += localOffset; 
      PInvokeLibrary.SystemTimeLib.SetTime(localTime); 
      m_SetTime = false; 
     } 
    } 

코드 내 GpsSample8 데모입니다 참조 GPS 시간에 SystemTime을 두 번 설정 한 후 SystemTime과 LocalTime.

BTW : EST은 UTC보다 5 시간 늦지 만 3이 아닙니다. 오프셋 적용 후 DST를 적용할지 여부를 결정합니다 (일년 중 날짜에 따라 다름).

+0

대단히 감사합니다. – Dayan

관련 문제