Skip to content

Commit

Permalink
Added CAN logging in the UI and generic bgworker.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasclaesson committed Feb 1, 2016
1 parent f712853 commit bf9e911
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
14 changes: 14 additions & 0 deletions TrionicCANFlasher/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 79 additions & 3 deletions TrionicCANFlasher/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}
}
}
29 changes: 29 additions & 0 deletions TrionicCANLib/ITrionic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Runtime.InteropServices;
using TrionicCANLib.CAN;
using NLog;
using System.ComponentModel;

namespace TrionicCANLib.API
{
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;
Expand Down Expand Up @@ -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;
}
}
}
}
}

0 comments on commit bf9e911

Please sign in to comment.