1- using System ;
1+ using Peak . Can . Basic ;
2+ using System ;
23using System . Collections . Generic ;
34using System . Diagnostics ;
45using System . Linq ;
6+ using System . Runtime . CompilerServices ;
57using System . Text ;
68using System . Threading . Channels ;
79using System . Threading . Tasks ;
810using System . Windows ;
9- using Peak . Can . Basic ;
11+ using static NMEA2000Analyzer . MainWindow ;
1012
1113namespace 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