diff --git a/TrionicCANFlasher/frmMain.Designer.cs b/TrionicCANFlasher/frmMain.Designer.cs index 961bedbe..74a42116 100644 --- a/TrionicCANFlasher/frmMain.Designer.cs +++ b/TrionicCANFlasher/frmMain.Designer.cs @@ -55,6 +55,7 @@ private void InitializeComponent() this.cbAdapter = new System.Windows.Forms.ComboBox(); this.cbUseFlasherOnDevice = new System.Windows.Forms.CheckBox(); this.linkLabelLogging = new System.Windows.Forms.LinkLabel(); + this.btnLogData = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnReadECU @@ -365,11 +366,23 @@ private void InitializeComponent() this.linkLabelLogging.Text = "Open logging directory"; this.linkLabelLogging.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelLogging_LinkClicked); // + // btnLogData + // + this.btnLogData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.btnLogData.Location = new System.Drawing.Point(675, 180); + this.btnLogData.Name = "btnLogData"; + this.btnLogData.Size = new System.Drawing.Size(107, 50); + this.btnLogData.TabIndex = 68; + this.btnLogData.Text = "Log data"; + this.btnLogData.UseVisualStyleBackColor = true; + this.btnLogData.Click += new System.EventHandler(this.btnLogData_Click); + // // frmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1023, 392); + this.Controls.Add(this.btnLogData); this.Controls.Add(this.linkLabelLogging); this.Controls.Add(this.cbUseFlasherOnDevice); this.Controls.Add(this.cbAdapter); @@ -436,6 +449,7 @@ private void InitializeComponent() private System.Windows.Forms.ComboBox cbAdapter; private System.Windows.Forms.CheckBox cbUseFlasherOnDevice; private System.Windows.Forms.LinkLabel linkLabelLogging; + private System.Windows.Forms.Button btnLogData; } } diff --git a/TrionicCANFlasher/frmMain.cs b/TrionicCANFlasher/frmMain.cs index 0af7f90f..4715981b 100644 --- a/TrionicCANFlasher/frmMain.cs +++ b/TrionicCANFlasher/frmMain.cs @@ -30,6 +30,7 @@ public partial class frmMain : Form public DelegateProgressStatus m_DelegateProgressStatus; private Logger logger = LogManager.GetCurrentClassLogger(); msiupdater m_msiUpdater; + BackgroundWorker bgworkerLogCanData; public frmMain() { @@ -202,7 +203,11 @@ private void btnFlashEcu_Click(object sender, EventArgs e) void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { - if (e.Result != null && (bool)e.Result) + if (e.Cancelled) + { + AddLogItem("Stopped"); + } + else if (e.Result != null && (bool)e.Result) { AddLogItem("Operation done"); } @@ -213,7 +218,16 @@ void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) TimeSpan ts = DateTime.Now - dtstart; AddLogItem("Total duration: " + ts.Minutes + " minutes " + ts.Seconds + " seconds"); - trionic8.Cleanup(); + + if (cbxEcuType.SelectedIndex == (int)ECU.TRIONIC7) + { + trionic7.Cleanup(); + } + else if (cbxEcuType.SelectedIndex == (int)ECU.TRIONIC8 || + cbxEcuType.SelectedIndex == (int)ECU.MOTRONIC96) + { + trionic8.Cleanup(); + } EnableUserInput(true); AddLogItem("Connection terminated"); } @@ -1025,7 +1039,6 @@ private void btnReadDTC_Click(object sender, EventArgs e) if (cbxEcuType.SelectedIndex == (int)ECU.TRIONIC7) { SetGenericOptions(trionic7); - //trionic7.ELM327Kline = cbELM327Kline.Checked; trionic7.UseFlasherOnDevice = false; EnableUserInput(false); @@ -1381,6 +1394,11 @@ private void btnReadECUcalibration_Click(object sender, EventArgs e) } private void cbEnableLogging_CheckedChanged(object sender, EventArgs e) + { + UpdateLogManager(); + } + + private void UpdateLogManager() { if (cbEnableLogging.Checked) { @@ -1453,5 +1471,63 @@ private void btnRestoreT8_Click(object sender, EventArgs e) LogManager.Flush(); } + private void btnLogData_Click(object sender, EventArgs e) + { + if (btnLogData.Text != "Stop") + { + // Force logging on + LogManager.EnableLogging(); + dtstart = DateTime.Now; + + if (cbxEcuType.SelectedIndex == (int)ECU.TRIONIC7) + { + SetGenericOptions(trionic7); + trionic7.UseFlasherOnDevice = false; + + EnableUserInput(false); + AddLogItem("Opening connection"); + if (trionic7.openDevice()) + { + StartBGWorkerLog(trionic7); + } + } + else if (cbxEcuType.SelectedIndex == (int)ECU.TRIONIC8 || + cbxEcuType.SelectedIndex == (int)ECU.MOTRONIC96) + { + SetGenericOptions(trionic8); + + EnableUserInput(false); + AddLogItem("Opening connection"); + trionic8.SecurityLevel = AccessLevel.AccessLevel01; + if (trionic8.openDevice(false)) + { + StartBGWorkerLog(trionic8); + } + } + btnLogData.Text = "Stop"; + } + else + { + bgworkerLogCanData.CancelAsync(); + // Reset logging to setting + UpdateLogManager(); + btnLogData.Text = "Log Data"; + EnableUserInput(true); + } + } + + private void StartBGWorkerLog(ITrionic trionic) + { + AddLogItem("Logging in progress"); + bgworkerLogCanData = new BackgroundWorker + { + WorkerReportsProgress = true, + WorkerSupportsCancellation = true + }; + bgworkerLogCanData.DoWork += new DoWorkEventHandler(trionic.LogCANData); + bgworkerLogCanData.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted); + bgworkerLogCanData.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged); + bgworkerLogCanData.RunWorkerAsync(); + } } } diff --git a/TrionicCANLib/ITrionic.cs b/TrionicCANLib/ITrionic.cs index 48e0ac37..4126c4b9 100644 --- a/TrionicCANLib/ITrionic.cs +++ b/TrionicCANLib/ITrionic.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices; using TrionicCANLib.CAN; using NLog; +using System.ComponentModel; namespace TrionicCANLib.API { @@ -11,6 +12,7 @@ abstract public class ITrionic { private static Logger logger = LogManager.GetCurrentClassLogger(); protected ICANDevice canUsbDevice; + private CANListener m_canLogListener; public delegate void WriteProgress(object sender, WriteProgressEventArgs e); public event ITrionic.WriteProgress onWriteProgress; @@ -261,5 +263,32 @@ public ReadProgressEventArgs(int percentage) _percentage = percentage; } } + + public void LogCANData(object sender, DoWorkEventArgs workEvent) + { + BackgroundWorker bw = sender as BackgroundWorker; + + if (!canUsbDevice.isOpen()) return; + + if (m_canLogListener == null) + { + m_canLogListener = new CANListener(); + } + canUsbDevice.AcceptOnlyMessageIds = null; + canUsbDevice.addListener(m_canLogListener); + + while (true) + { + m_canLogListener.waitMessage(1000); + + if (bw.CancellationPending) + { + canUsbDevice.removeListener(m_canLogListener); + m_canLogListener = null; + workEvent.Cancel = true; + return; + } + } + } } }