Skip to content

Commit 12a0e7a

Browse files
authored
Merge pull request #6 from kctowers/main
Added support for live PCAN data
2 parents 7738676 + 1102784 commit 12a0e7a

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

MainWindow.xaml.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,28 @@ private async void RecordMenuItem_ClickAsync(object sender, RoutedEventArgs e)
213213
{
214214
if (!_isRecording)
215215
{
216+
PCAN.InitCan(true);
216217
RecordMenuItem.Header = "Stop recording";
217218
_isRecording = true;
218219
}
219220
else
220221
{
222+
PCAN.InitCan(false);
221223
RecordMenuItem.Header = "Record";
222224
_isRecording = false;
225+
226+
_Data = PCAN.LoadCapture();
227+
UpdateTimestampRange();
228+
229+
_assembledData = AssembleFrames(_Data);
230+
DataGrid.ItemsSource = _assembledData;
231+
232+
GenerateDeviceInfo(_assembledData);
233+
UpdateSrcDevices(_assembledData);
234+
223235
}
224-
PCAN.InitCan();
236+
237+
225238
}
226239

227240
private void FilterTextBoxes_TextChanged(object sender, TextChangedEventArgs e)

PCAN.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1-
using System;
1+
using Peak.Can.Basic;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
6+
using System.Runtime.CompilerServices;
57
using System.Text;
68
using System.Threading.Channels;
79
using System.Threading.Tasks;
810
using System.Windows;
9-
using Peak.Can.Basic;
11+
using static NMEA2000Analyzer.MainWindow;
1012

1113
namespace NMEA2000Analyzer
1214
{
1315
class PCAN
1416
{
1517
private static readonly Worker _worker = new Worker(PcanChannel.Usb01, Bitrate.Pcan250);
16-
17-
public static void InitCan()
18+
private static List<Nmea2000Record>? capture;
19+
20+
public static void InitCan(Boolean start)
1821
{
19-
if (_worker.Active)
20-
{
21-
_worker.Stop();
22-
} else
22+
if ((start))
2323
{
24+
if (capture == null)
25+
{
26+
capture = new List<Nmea2000Record>();
27+
}
2428
_worker.MessageAvailable += OnMessageAvailable;
2529
_worker.Start();
2630
Debug.WriteLine("Worker started successfully.");
2731
}
32+
else
33+
{
34+
_worker.Stop();
35+
}
36+
}
37+
38+
public static List<Nmea2000Record> LoadCapture()
39+
{
40+
return (capture);
2841
}
2942

3043
private static void OnMessageAvailable(object? sender, MessageAvailableEventArgs e)
@@ -36,7 +49,33 @@ private static void OnMessageAvailable(object? sender, MessageAvailableEventArgs
3649
uint priority = (msg.ID >> 26) & 0x7;
3750
uint source = msg.ID & 0xFF;
3851
uint pgn = (msg.ID >> 8) & 0x1FFFF;
39-
Debug.WriteLine($"TS: {timestamp}, Src: {source}, Len: {msg.DLC}, PGN: {pgn}");
52+
uint destination;
53+
DateTime now = DateTime.Now;
54+
55+
if ((pgn & 0xFF00) == 0xEF00) // Check if it's a PDU1 (destination-specific PGN)
56+
{
57+
destination = (pgn & 0xFF); // Extract destination (last byte of PGN)
58+
pgn &= 0x1FF00; // Remove destination from PGN
59+
}
60+
else
61+
{
62+
destination = 255; // Broadcast for PDU2 (no destination-specific PGN)
63+
}
64+
65+
// Format data bytes as a space-separated hex string
66+
byte[] data = msg.Data;
67+
string dataHex = string.Join(" ", data.Select(b => $"0x{b:X2}"));
68+
69+
capture.Add(new Nmea2000Record
70+
{
71+
Timestamp = now.ToString("ss.ffffff"),
72+
Source = source.ToString(),
73+
Destination = destination.ToString(),
74+
PGN = pgn.ToString(),
75+
Priority = priority.ToString(),
76+
Data = dataHex,
77+
});
78+
// Debug.WriteLine($"TS: {timestamp}, Src: {source}, Len: {msg.DLC}, PGN: {pgn}");
4079
}
4180
}
4281

0 commit comments

Comments
 (0)