diff --git a/GPSDOTimeSync/FormMain.Designer.cs b/GPSDOTimeSync/FormMain.Designer.cs index b2b416a..32dc3e0 100644 --- a/GPSDOTimeSync/FormMain.Designer.cs +++ b/GPSDOTimeSync/FormMain.Designer.cs @@ -23,27 +23,25 @@ /// the contents of this method with the code editor. /// private void InitializeComponent() { - this.labelTimestamps = new System.Windows.Forms.Label(); this.statusStrip = new System.Windows.Forms.StatusStrip(); this.latestLogMessage = new System.Windows.Forms.ToolStripStatusLabel(); + this.serialPortNames = new System.Windows.Forms.ComboBox(); + this.serialPortNamesLabel = new System.Windows.Forms.Label(); + this.start = new System.Windows.Forms.Button(); + this.stop = new System.Windows.Forms.Button(); + this.deviceLabel = new System.Windows.Forms.Label(); + this.deviceNames = new System.Windows.Forms.ComboBox(); this.statusStrip.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; - // // statusStrip // this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.latestLogMessage}); - this.statusStrip.Location = new System.Drawing.Point(0, 336); + this.statusStrip.Location = new System.Drawing.Point(0, 99); this.statusStrip.Name = "statusStrip"; - this.statusStrip.Size = new System.Drawing.Size(486, 22); + this.statusStrip.Size = new System.Drawing.Size(224, 22); + this.statusStrip.SizingGrip = false; this.statusStrip.TabIndex = 1; this.statusStrip.Text = "statusStrip1"; // @@ -51,16 +49,79 @@ // this.latestLogMessage.ForeColor = System.Drawing.Color.Black; this.latestLogMessage.Name = "latestLogMessage"; - this.latestLogMessage.Size = new System.Drawing.Size(66, 17); + this.latestLogMessage.Size = new System.Drawing.Size(178, 17); + this.latestLogMessage.Spring = true; this.latestLogMessage.Text = "{RUNTIME}"; // + // serialPortNames + // + this.serialPortNames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.serialPortNames.FormattingEnabled = true; + this.serialPortNames.Location = new System.Drawing.Point(76, 39); + this.serialPortNames.Name = "serialPortNames"; + this.serialPortNames.Size = new System.Drawing.Size(121, 21); + this.serialPortNames.TabIndex = 2; + // + // serialPortNamesLabel + // + this.serialPortNamesLabel.AutoSize = true; + this.serialPortNamesLabel.Location = new System.Drawing.Point(12, 42); + this.serialPortNamesLabel.Name = "serialPortNamesLabel"; + this.serialPortNamesLabel.Size = new System.Drawing.Size(55, 13); + this.serialPortNamesLabel.TabIndex = 3; + this.serialPortNamesLabel.Text = "Serial Port"; + // + // start + // + this.start.Location = new System.Drawing.Point(12, 66); + this.start.Name = "start"; + this.start.Size = new System.Drawing.Size(75, 23); + this.start.TabIndex = 4; + this.start.Text = "Start"; + this.start.UseVisualStyleBackColor = true; + this.start.Click += new System.EventHandler(this.start_Click); + // + // stop + // + this.stop.Enabled = false; + this.stop.Location = new System.Drawing.Point(93, 66); + this.stop.Name = "stop"; + this.stop.Size = new System.Drawing.Size(75, 23); + this.stop.TabIndex = 5; + this.stop.Text = "Stop"; + this.stop.UseVisualStyleBackColor = true; + this.stop.Click += new System.EventHandler(this.stop_Click); + // + // deviceLabel + // + this.deviceLabel.AutoSize = true; + this.deviceLabel.Location = new System.Drawing.Point(12, 15); + this.deviceLabel.Name = "deviceLabel"; + this.deviceLabel.Size = new System.Drawing.Size(41, 13); + this.deviceLabel.TabIndex = 6; + this.deviceLabel.Text = "Device"; + // + // deviceNames + // + this.deviceNames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.deviceNames.FormattingEnabled = true; + this.deviceNames.Location = new System.Drawing.Point(76, 12); + this.deviceNames.Name = "deviceNames"; + this.deviceNames.Size = new System.Drawing.Size(121, 21); + this.deviceNames.TabIndex = 7; + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(486, 358); + this.ClientSize = new System.Drawing.Size(224, 121); + this.Controls.Add(this.deviceNames); + this.Controls.Add(this.deviceLabel); + this.Controls.Add(this.stop); + this.Controls.Add(this.start); + this.Controls.Add(this.serialPortNamesLabel); + this.Controls.Add(this.serialPortNames); this.Controls.Add(this.statusStrip); - this.Controls.Add(this.labelTimestamps); this.Name = "FormMain"; this.Text = "GPSDO Time Sync"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormMain_FormClosing); @@ -72,10 +133,14 @@ } #endregion - - private System.Windows.Forms.Label labelTimestamps; private System.Windows.Forms.StatusStrip statusStrip; private System.Windows.Forms.ToolStripStatusLabel latestLogMessage; + private System.Windows.Forms.ComboBox serialPortNames; + private System.Windows.Forms.Label serialPortNamesLabel; + private System.Windows.Forms.Button start; + private System.Windows.Forms.Button stop; + private System.Windows.Forms.Label deviceLabel; + private System.Windows.Forms.ComboBox deviceNames; } } diff --git a/GPSDOTimeSync/FormMain.cs b/GPSDOTimeSync/FormMain.cs index b6fef4a..6206f7a 100644 --- a/GPSDOTimeSync/FormMain.cs +++ b/GPSDOTimeSync/FormMain.cs @@ -15,6 +15,18 @@ namespace GPSDOTimeSync { { LogLevel.Error, Color.Red } }; + private static readonly Dictionary> TIME_PROVIDER_CONSTRUCTORS = new Dictionary>() { + { + "Trimble Thunderbolt", + new Func((serialPort) => { + ThunderboltSerialPort thunderboltSerialPort = new ThunderboltSerialPort(serialPort); + ITimeProvider timeProvider = new ThunderboltTimeProvider(thunderboltSerialPort); + + return timeProvider; + }) + } + }; + private ITimeProvider timeProvider; public FormMain() { @@ -30,15 +42,50 @@ namespace GPSDOTimeSync { // Quit InitializeComponent(); + PopulateDropDowns(); + statusStrip.Renderer = new TruncatedTextEllipsisRenderer(); latestLogMessage.Text = ""; + } + + private void PopulateDropDowns() { + foreach (string portName in SerialPort.GetPortNames()) { + serialPortNames.Items.Add(portName); + } + + if (serialPortNames.Items.Count > 0) { + serialPortNames.SelectedIndex = 0; + } + + foreach (string deviceName in TIME_PROVIDER_CONSTRUCTORS.Keys) { + deviceNames.Items.Add(deviceName); + } + + if (deviceNames.Items.Count > 0) { + deviceNames.SelectedIndex = 0; + } + } + + private void AddMessageToLog(string message, LogLevel logLevel) { + latestLogMessage.Text = string.Format("{0} ({1})", message, DateTime.Now.ToString("G")); + latestLogMessage.ForeColor = LOG_LEVEL_TO_COLOR[logLevel]; + } - ThunderboltSerialPort thunderboltSerialPort = new ThunderboltSerialPort(new SerialPort("COM3")); - timeProvider = new ThunderboltTimeProvider(thunderboltSerialPort); + private void start_Click(object sender, EventArgs e) { + string serialPortName = (string) serialPortNames.SelectedItem; + string deviceName = (string) deviceNames.SelectedItem; + + SerialPort serialPort = new SerialPort(serialPortName); + timeProvider = TIME_PROVIDER_CONSTRUCTORS[deviceName](serialPort); timeProvider.TimeAvailable += (DateTime dateTime) => { Invoke(new Action(() => { - labelTimestamps.Text += string.Format("{0} {1}\n", dateTime.ToLongDateString(), dateTime.ToLongTimeString()); + AddMessageToLog( + string.Format( + "Time is {0} {1}", + dateTime.ToLongDateString(), dateTime.ToLongTimeString() + ), LogLevel.Info + ); })); }; @@ -49,15 +96,34 @@ namespace GPSDOTimeSync { }; timeProvider.Start(); + + AddMessageToLog("Time sync started.", LogLevel.Info); + + start.Enabled = false; + stop.Enabled = true; } - private void AddMessageToLog(string message, LogLevel logLevel) { - latestLogMessage.Text = string.Format("{0} ({1})", message, DateTime.Now.ToString("G")); - latestLogMessage.ForeColor = LOG_LEVEL_TO_COLOR[logLevel]; + private void stop_Click(object sender, EventArgs e) { + timeProvider.Stop(); + + AddMessageToLog("Time sync stopped.", LogLevel.Info); + + stop.Enabled = false; + start.Enabled = true; } private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { - timeProvider.Stop(); + timeProvider?.Stop(); + } + } + + public class TruncatedTextEllipsisRenderer : ToolStripProfessionalRenderer { + protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) { + if (e.Item is ToolStripStatusLabel) { + TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont, e.TextRectangle, e.TextColor, Color.Transparent, e.TextFormat | TextFormatFlags.EndEllipsis); + } else { + base.OnRenderItemText(e); + } } } } diff --git a/GPSDOTimeSync/GPSDOTimeSync.csproj b/GPSDOTimeSync/GPSDOTimeSync.csproj index f0691eb..b773ba5 100644 --- a/GPSDOTimeSync/GPSDOTimeSync.csproj +++ b/GPSDOTimeSync/GPSDOTimeSync.csproj @@ -91,4 +91,4 @@ --> - + \ No newline at end of file diff --git a/GPSDOTimeSync/TimeProviders/Thunderbolt/ThunderboltSerialPort.cs b/GPSDOTimeSync/TimeProviders/Thunderbolt/ThunderboltSerialPort.cs index f9e1511..2143ab4 100644 --- a/GPSDOTimeSync/TimeProviders/Thunderbolt/ThunderboltSerialPort.cs +++ b/GPSDOTimeSync/TimeProviders/Thunderbolt/ThunderboltSerialPort.cs @@ -73,6 +73,7 @@ namespace GPSDOTimeSync.Devices.Thunderbolt { this.serialPort = serialPort; readThread = new Thread(ReadSerialPort); + readThread.Name = "ThunderboltSerialPort Read"; } /// @@ -201,12 +202,14 @@ namespace GPSDOTimeSync.Devices.Thunderbolt { private void ReadSerialPort() { while (running) { - int possibleCurrentByte = serialPort.ReadByte(); + if (serialPort.BytesToRead > 0) { + int possibleCurrentByte = serialPort.ReadByte(); - if (possibleCurrentByte != -1) { - // Once we're sure the byte that was read wasn't -1 (which signifies the end of the read), we're safe to cast to a byte - ProcessByte((byte) possibleCurrentByte); - } + if (possibleCurrentByte != -1) { + // Once we're sure the byte that was read wasn't -1 (which signifies the end of the read), we're safe to cast to a byte + ProcessByte((byte) possibleCurrentByte); + } + } } } }