Browse Source

Calculate UTC time and display

master
The6P4C 7 years ago
parent
commit
2be1bb23ac
2 changed files with 30 additions and 6 deletions
  1. +13
    -1
      ThunderboltTimeSync/FormMain.Designer.cs
  2. +17
    -5
      ThunderboltTimeSync/FormMain.cs

+ 13
- 1
ThunderboltTimeSync/FormMain.Designer.cs View File

@@ -23,13 +23,23 @@
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.labelTimestamps = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelTimestamps
//
this.labelTimestamps.Dock = System.Windows.Forms.DockStyle.Fill;
this.labelTimestamps.Location = new System.Drawing.Point(0, 0);
this.labelTimestamps.Name = "labelTimestamps";
this.labelTimestamps.Size = new System.Drawing.Size(486, 358);
this.labelTimestamps.TabIndex = 0;
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(312, 316);
this.ClientSize = new System.Drawing.Size(486, 358);
this.Controls.Add(this.labelTimestamps);
this.Name = "FormMain";
this.Text = "Thunderbolt Time Sync";
this.ResumeLayout(false);
@@ -37,6 +47,8 @@
}

#endregion

private System.Windows.Forms.Label labelTimestamps;
}
}


+ 17
- 5
ThunderboltTimeSync/FormMain.cs View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Windows.Forms;

namespace ThunderboltTimeSync {
@@ -20,13 +19,26 @@ namespace ThunderboltTimeSync {

InitializeComponent();

ThunderboltSerialPort tbsp = new ThunderboltSerialPort(new SerialPort("COM3"));
ThunderboltSerialPort tbsp = new ThunderboltSerialPort(new SerialPort("COM8"));

tbsp.PacketReceived += (ThunderboltPacket packet) => {
if (packet.IsPacketValid) {
if (packet.ID == 0x8F && packet.Data.Count == 17 && packet.Data[0] == 0xAB) {
ushort year = (ushort) (packet.Data[15] << 8 | packet.Data[16]);
Debug.WriteLine(year);
int timeOfWeek = packet.Data[1] << 24 | packet.Data[2] << 16 | packet.Data[3] << 8 | packet.Data[4];
ushort weekNumber = (ushort) (packet.Data[5] << 8 | packet.Data[6]);
short utcOffset = (short) (packet.Data[7] << 8 | packet.Data[8]);

// Current epoch for GPS week numbers is the morning of 22/8/1999
DateTime dateTime = new DateTime(1999, 8, 22, 0, 0, 0);

dateTime = dateTime.AddDays(7 * weekNumber);
dateTime = dateTime.AddSeconds(timeOfWeek);

dateTime = dateTime.AddSeconds(-utcOffset);

labelTimestamps.Invoke(new Action(() => {
labelTimestamps.Text += string.Format("{0} {1}\n", dateTime.ToLongDateString(), dateTime.ToLongTimeString());
}));
}
}
};


Loading…
Cancel
Save