Skip to content

Commit dbfe1d8

Browse files
authored
Merge pull request #8 from kctowers/main
Fixed problem when PCAN dongle not plugged in
2 parents 12a0e7a + df77f71 commit dbfe1d8

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</Menu.ItemContainerStyle>
2525
<MenuItem Header="File" x:Name="FileMenu">
2626
<MenuItem Header="Open" Click="OpenMenuItem_ClickAsync" />
27-
<MenuItem x:Name="RecordMenuItem" Header="Record" Click="RecordMenuItem_ClickAsync" />
27+
<MenuItem x:Name="RecordMenuItem" Header="PCAN Capture" Click="RecordMenuItem_ClickAsync" />
2828
<Separator x:Name="RecentFilesSeparator" Visibility="Collapsed" />
2929
</MenuItem>
3030
<MenuItem Header="Statistics">
@@ -166,7 +166,7 @@
166166
</DataGrid.Columns>
167167
</DataGrid>
168168
<GridSplitter Grid.Column="1"
169-
Width="3"
169+
Width="Auto"
170170
HorizontalAlignment="Stretch"
171171
VerticalAlignment="Stretch"
172172
Background="LightGray"

MainWindow.xaml.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public partial class MainWindow : Window
1717
private List<Nmea2000Record>? _Data;
1818
private List<Nmea2000Record>? _assembledData;
1919
private List<Nmea2000Record>? _filteredData;
20-
private bool _isRecording = false;
21-
20+
2221
public MainWindow()
2322
{
2423
InitializeComponent();
@@ -211,18 +210,18 @@ private async void RecentFileMenuItem_Click(object sender, RoutedEventArgs e)
211210

212211
private async void RecordMenuItem_ClickAsync(object sender, RoutedEventArgs e)
213212
{
214-
if (!_isRecording)
215-
{
216-
PCAN.InitCan(true);
217-
RecordMenuItem.Header = "Stop recording";
218-
_isRecording = true;
219-
}
220-
else
213+
if (PCAN.StartCapture())
221214
{
222-
PCAN.InitCan(false);
223-
RecordMenuItem.Header = "Record";
224-
_isRecording = false;
215+
// Pause here to allow packet to capture.
216+
System.Windows.MessageBox.Show(
217+
"Capture Started. Press OK to stop",
218+
"Capturing Data...",
219+
MessageBoxButton.OK,
220+
MessageBoxImage.Information
221+
);
225222

223+
PCAN.StopCapture();
224+
226225
_Data = PCAN.LoadCapture();
227226
UpdateTimestampRange();
228227

@@ -233,7 +232,15 @@ private async void RecordMenuItem_ClickAsync(object sender, RoutedEventArgs e)
233232
UpdateSrcDevices(_assembledData);
234233

235234
}
236-
235+
else
236+
{
237+
System.Windows.MessageBox.Show(
238+
"PCAN dongle not plugged in or driver not installed",
239+
"Error:",
240+
MessageBoxButton.OK,
241+
MessageBoxImage.Error
242+
);
243+
}
237244

238245
}
239246

PCAN.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,37 @@ class PCAN
1717
private static readonly Worker _worker = new Worker(PcanChannel.Usb01, Bitrate.Pcan250);
1818
private static List<Nmea2000Record>? capture;
1919

20-
public static void InitCan(Boolean start)
20+
public static bool StartCapture()
2121
{
22-
if ((start))
22+
try
2323
{
2424
if (capture == null)
25-
{
25+
{
2626
capture = new List<Nmea2000Record>();
2727
}
2828
_worker.MessageAvailable += OnMessageAvailable;
2929
_worker.Start();
3030
Debug.WriteLine("Worker started successfully.");
31+
return (true);
32+
}
33+
catch (PcanBasicException)
34+
{
35+
return (false);
3136
}
32-
else
37+
catch (DllNotFoundException)
38+
{
39+
return (false);
40+
}
41+
}
42+
public static void StopCapture()
43+
{
44+
try
3345
{
3446
_worker.Stop();
3547
}
48+
catch (PcanBasicException e)
49+
{
50+
}
3651
}
3752

3853
public static List<Nmea2000Record> LoadCapture()

0 commit comments

Comments
 (0)