No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

50 líneas
1.6 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO.Ports;
  4. using System.Windows.Forms;
  5. namespace ThunderboltTimeSync {
  6. public partial class FormMain : Form {
  7. public FormMain() {
  8. // Check for admin rights
  9. // If running as admin:
  10. // Ask for COM port with dialog
  11. // Connect to COM port
  12. // When time message received:
  13. // If (time in UTC) AND (last time change was more than $MIN_UPDATE_INTERVAL ago) AND (error is less than $ERROR_THRESHOLD)
  14. // Change system time to GPS time
  15. // Else:
  16. // Display message to tell user to run as admin
  17. // Quit
  18. InitializeComponent();
  19. ThunderboltSerialPort tbsp = new ThunderboltSerialPort(new SerialPort("COM8"));
  20. tbsp.PacketReceived += (ThunderboltPacket packet) => {
  21. if (packet.IsPacketValid) {
  22. if (packet.ID == 0x8F && packet.Data.Count == 17 && packet.Data[0] == 0xAB) {
  23. int timeOfWeek = packet.Data[1] << 24 | packet.Data[2] << 16 | packet.Data[3] << 8 | packet.Data[4];
  24. ushort weekNumber = (ushort) (packet.Data[5] << 8 | packet.Data[6]);
  25. short utcOffset = (short) (packet.Data[7] << 8 | packet.Data[8]);
  26. // Current epoch for GPS week numbers is the morning of 22/8/1999
  27. DateTime dateTime = new DateTime(1999, 8, 22, 0, 0, 0);
  28. dateTime = dateTime.AddDays(7 * weekNumber);
  29. dateTime = dateTime.AddSeconds(timeOfWeek);
  30. dateTime = dateTime.AddSeconds(-utcOffset);
  31. labelTimestamps.Invoke(new Action(() => {
  32. labelTimestamps.Text += string.Format("{0} {1}\n", dateTime.ToLongDateString(), dateTime.ToLongTimeString());
  33. }));
  34. }
  35. }
  36. };
  37. tbsp.Open();
  38. }
  39. }
  40. }