Selaa lähdekoodia

Calculate UTC time and display

master
The6P4C 7 vuotta sitten
vanhempi
commit
2be1bb23ac
2 muutettua tiedostoa jossa 30 lisäystä ja 6 poistoa
  1. +13
    -1
      ThunderboltTimeSync/FormMain.Designer.cs
  2. +17
    -5
      ThunderboltTimeSync/FormMain.cs

+ 13
- 1
ThunderboltTimeSync/FormMain.Designer.cs Näytä tiedosto

@@ -23,13 +23,23 @@
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() { private void InitializeComponent() {
this.labelTimestamps = new System.Windows.Forms.Label();
this.SuspendLayout(); 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 // FormMain
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.Name = "FormMain";
this.Text = "Thunderbolt Time Sync"; this.Text = "Thunderbolt Time Sync";
this.ResumeLayout(false); this.ResumeLayout(false);
@@ -37,6 +47,8 @@
} }


#endregion #endregion

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



+ 17
- 5
ThunderboltTimeSync/FormMain.cs Näytä tiedosto

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


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


InitializeComponent(); InitializeComponent();


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


tbsp.PacketReceived += (ThunderboltPacket packet) => { tbsp.PacketReceived += (ThunderboltPacket packet) => {
if (packet.IsPacketValid) { if (packet.IsPacketValid) {
if (packet.ID == 0x8F && packet.Data.Count == 17 && packet.Data[0] == 0xAB) { 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());
}));
} }
} }
}; };


Ladataan…
Peruuta
Tallenna