Skip to content

Commit a3a9397

Browse files
committed
YD CSV, .can support removed due to data size bug
1 parent 21c1ec8 commit a3a9397

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

FileFormats.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

MainWindow.xaml.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Nmea2000Record
5050
private class FastPacketMessage
5151
{
5252
public int TotalBytes { get; set; }
53+
public int ReceivedByteCount { get; set; }
5354
public Dictionary<int, string[]> Frames { get; set; } = new Dictionary<int, string[]>();
5455
public string? Timestamp { get; set; }
5556
public string Source { get; set; }
@@ -84,7 +85,7 @@ private async void OpenMenuItem_ClickAsync(object sender, RoutedEventArgs e)
8485
// Open file picker dialog
8586
OpenFileDialog openFileDialog = new OpenFileDialog
8687
{
87-
Filter = "(*.csv, *.can, *.log, *.txt, *.dump)|*.csv;*.can;*.log;*.txt;*.dump|All Files (*.*)|*.*",
88+
Filter = "(*.csv, *.log, *.txt, *.dump)|*.csv;*.log;*.txt;*.dump|All Files (*.*)|*.*",
8889
Title = "Open File"
8990
};
9091

@@ -117,8 +118,8 @@ private async void OpenMenuItem_ClickAsync(object sender, RoutedEventArgs e)
117118
case FileFormats.FileFormat.PCANView:
118119
_Data = await Task.Run(() => FileFormats.LoadPCANView(filePath));
119120
break;
120-
case FileFormats.FileFormat.YDBinary:
121-
_Data = await Task.Run(() => FileFormats.LoadYDBinary(filePath));
121+
case FileFormats.FileFormat.YDCsv:
122+
_Data = await Task.Run(() => FileFormats.LoadYDCsv(filePath));
122123
break;
123124
default:
124125
MessageBox.Show("Unsupported or unknown file format.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
@@ -155,6 +156,13 @@ private void ApplyFilters()
155156
var includeAddress = ParseList(IncludeAddressTextBox.Text);
156157
var excludePGNs = ParseList(ExcludePGNTextBox.Text);
157158

159+
// Reset to original data when all filters are cleared
160+
if (includePGNs.Count == 0 && includeAddress.Count == 0 && excludePGNs.Count == 0)
161+
{
162+
DataGrid.ItemsSource = _assembledData; // Restore original data
163+
return;
164+
}
165+
158166
// Apply filters to the original data
159167
var filteredData = _assembledData.Where(record =>
160168
{
@@ -334,16 +342,18 @@ private List<Nmea2000Record> AssembleFrames(List<Nmea2000Record> records)
334342
}
335343

336344
message.Frames[sequenceNumber] = framePayload;
345+
message.ReceivedByteCount += framePayload.Length;
337346
//Debug.WriteLine($"Added frame {sequenceNumber} to {messageKey}: {string.Join(" ", framePayload)}");
338347
}
339348
else
340349
{
341350
Debug.WriteLine($"Duplicate frame {sequenceNumber} for {messageKey}. Ignoring.");
342351
}
343352

353+
//Debug.WriteLine($"Message {messageKey}, TotalBytes = {message.TotalBytes}");
354+
344355
// Calculate the total number of bytes received so far
345356
int receivedBytes = message.Frames.Values.Sum(f => f.Length);
346-
//Debug.WriteLine($"Message {messageKey}: ReceivedBytes = {receivedBytes}, TotalBytes = {message.TotalBytes}");
347357

348358
// Check if the message is complete
349359
if (message.TotalBytes > 0 && receivedBytes >= message.TotalBytes)
@@ -429,7 +439,6 @@ private void ClearData()
429439
DataGrid.ItemsSource = null;
430440
JsonViewerTextBox.Text = null;
431441

432-
// Optionally, clear filters if needed
433442
IncludePGNTextBox.Text = string.Empty;
434443
ExcludePGNTextBox.Text = string.Empty;
435444
IncludeAddressTextBox.Text = string.Empty;

local.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
{
22
"PGNs": [
3+
{
4+
"PGN": 65096,
5+
"Id": "WheelSpeedDistance",
6+
"Description": "Wheel-based Speed and Distance",
7+
"Type": "Single"
8+
},
9+
{
10+
"PGN": 65130,
11+
"Id": "EngineFuelLube",
12+
"Description": "Engine Fuel/Lube Systems",
13+
"Type": "Single"
14+
},
315
{
416
"PGN": 126975,
517
"Id": "Unknown126975",

0 commit comments

Comments
 (0)