@@ -19,18 +19,21 @@ public enum FileFormat
1919 CanDump2 ,
2020 YDWG ,
2121 YDBinary ,
22+ YDCsv ,
2223 PCANView
2324 }
2425
2526 public static FileFormat DetectFileFormat ( string filePath )
2627 {
2728 try
2829 {
30+ /*
2931 if (IsBinaryYDFormat(filePath))
3032 {
3133 Debug.WriteLine("Binary CAN format detected");
3234 return FileFormat.YDBinary;
3335 }
36+ */
3437
3538 var lines = File . ReadLines ( filePath ) . Take ( 10 ) . ToList ( ) ; // Read the first few lines
3639
@@ -47,6 +50,12 @@ public static FileFormat DetectFileFormat(string filePath)
4750 return FileFormat . TwoCanCsv ;
4851 }
4952
53+ if ( line . StartsWith ( "Time,CAN,Dir,Bit,ID(hex),DLC" ) )
54+ {
55+ Debug . WriteLine ( "Yacht Devices CSV format detected" ) ;
56+ return FileFormat . YDCsv ;
57+ }
58+
5059 // Actisense: Look for timestamp and structured data
5160 if ( DateTime . TryParse ( line . Split ( ',' ) [ 0 ] , out _ ) )
5261 {
@@ -490,6 +499,9 @@ public static List<Nmea2000Record> LoadYDBinary(string filePath)
490499 source = ( int ) ( messageId & 0xFF ) ;
491500 }
492501
502+ if ( pgn == 126996 )
503+ Debug . WriteLine ( $ "Data Length: { dataLength } ") ;
504+
493505 // Format data bytes as a space-separated hex string
494506 string dataHex = string . Join ( " " , data . Take ( dataLength ) . Select ( b => $ "0x{ b : X2} ") ) ;
495507
@@ -515,5 +527,75 @@ public static List<Nmea2000Record> LoadYDBinary(string filePath)
515527
516528 return records ;
517529 }
530+
531+ public static List < Nmea2000Record > LoadYDCsv ( string filePath )
532+ {
533+ var records = new List < Nmea2000Record > ( ) ;
534+
535+ using ( var reader = new StreamReader ( filePath ) )
536+ {
537+ string ? headerLine = reader . ReadLine ( ) ; // Read the header
538+ if ( headerLine == null ) throw new Exception ( "File is empty." ) ;
539+
540+ // Validate header format
541+ var headers = headerLine . Split ( ',' ) . Select ( h => h . Trim ( ) ) . ToArray ( ) ;
542+ if ( ! headers . SequenceEqual ( new [ ] { "Time" , "CAN" , "Dir" , "Bit" , "ID(hex)" , "DLC" , "D0" , "D1" , "D2" , "D3" , "D4" , "D5" , "D6" , "D7" } ) )
543+ {
544+ throw new Exception ( "Invalid CSV format. Expected columns: Time, CAN, Dir, Bit, ID(hex), DLC, D0-D7." ) ;
545+ }
546+
547+ // Read and parse the rest of the file
548+ while ( ! reader . EndOfStream )
549+ {
550+ var line = reader . ReadLine ( ) ;
551+ if ( string . IsNullOrWhiteSpace ( line ) ) continue ;
552+
553+ var values = line . Split ( ',' ) . Select ( v => v . Trim ( ) ) . ToArray ( ) ;
554+ if ( values . Length < 7 ) continue ; // Ensure there are enough columns
555+
556+ // Extract fields
557+ string timestamp = values [ 0 ] ;
558+ string canInterface = values [ 1 ] ;
559+ string direction = values [ 2 ] ;
560+ int bitType = int . Parse ( values [ 3 ] ) ; // 11-bit or 29-bit identifier
561+ uint canId = uint . Parse ( values [ 4 ] , System . Globalization . NumberStyles . HexNumber ) ;
562+ int dlc = int . Parse ( values [ 5 ] ) ;
563+
564+ // Ensure DLC is within valid range (1-8)
565+ if ( dlc < 1 || dlc > 8 ) continue ;
566+
567+ // Extract data bytes
568+ string data = string . Join ( " " , values . Skip ( 6 ) . Take ( dlc ) . Select ( d => $ "0x{ d . ToUpper ( ) } ") ) ;
569+
570+ // Decode CAN ID
571+ int priority = - 1 , source = - 1 , destination = - 1 ;
572+ uint pgn = canId ; // Default if no 29-bit conversion is needed
573+
574+ if ( bitType == 29 )
575+ {
576+ priority = ( int ) ( ( canId >> 26 ) & 0x07 ) ;
577+ pgn = ( canId >> 8 ) & 0x1FFFF ; // PGN is in bits 9-26
578+ destination = ( int ) ( ( canId >> 8 ) & 0xFF ) ;
579+ source = ( int ) ( canId & 0xFF ) ;
580+ }
581+
582+ // Create the record
583+ var record = new Nmea2000Record
584+ {
585+ Timestamp = timestamp ,
586+ Priority = priority >= 0 ? priority . ToString ( ) : "-" ,
587+ PGN = pgn . ToString ( ) ,
588+ Source = source >= 0 ? source . ToString ( ) : "-" ,
589+ Destination = destination >= 0 ? destination . ToString ( ) : "-" ,
590+ Data = data
591+ } ;
592+ records . Add ( record ) ;
593+ }
594+ }
595+
596+ return records ;
597+ }
598+
599+
518600 }
519601}
0 commit comments