diff --git a/Port/dotnet/Program.cs b/Port/dotnet/Program.cs index 5578749..fe62071 100644 --- a/Port/dotnet/Program.cs +++ b/Port/dotnet/Program.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using System.Linq; namespace V2GDecoderNet { @@ -107,19 +108,22 @@ namespace V2GDecoderNet return -1; } - Console.Error.WriteLine($"🔍 [Program] EXI data length: {exiData.Length} bytes"); - Console.Error.WriteLine($"🔍 [Program] First 10 bytes: {BitConverter.ToString(exiData.Take(10).ToArray())}"); + // Check if output is redirected + bool isRedirected = Console.IsOutputRedirected; - // For debugging: also save to temp file - string debugPath = Path.Combine("temp", "debug_output.exi"); - File.WriteAllBytes(debugPath, exiData); - Console.Error.WriteLine($"🔍 [Program] Also saved to: {debugPath}"); - - // Windows binary output - direct approach - using (var stdout = Console.OpenStandardOutput()) + if (isRedirected) { - stdout.Write(exiData, 0, exiData.Length); - stdout.Flush(); + // Binary output for redirection (file output) + using (var stdout = Console.OpenStandardOutput()) + { + stdout.Write(exiData, 0, exiData.Length); + stdout.Flush(); + } + } + else + { + // Hex string output for console display + Console.Write(BitConverter.ToString(exiData).Replace("-", "")); } return 0; @@ -168,14 +172,35 @@ namespace V2GDecoderNet } // Decode EXI message - var result = V2GMessageProcessor.DecodeExiMessage(exiBuffer); + DecodeResult result; + if (xmlMode) + { + // Suppress debug output for XML-only mode + using (var sw = new StringWriter()) + { + var originalOut = Console.Out; + Console.SetOut(sw); + try + { + result = V2GMessageProcessor.DecodeExiMessage(exiBuffer); + } + finally + { + Console.SetOut(originalOut); + } + } + } + else + { + result = V2GMessageProcessor.DecodeExiMessage(exiBuffer); + } if (result.Success) { if (xmlMode) { - // XML decode mode - output Wireshark-style XML - Console.WriteLine(result.XmlOutput); + // XML decode mode - output clean XML only + Console.Write(result.XmlOutput); } else { @@ -325,11 +350,22 @@ namespace V2GDecoderNet return -1; } - // Output binary data to stdout - using (var stdout = Console.OpenStandardOutput()) + // Check if output is redirected + bool isRedirected = Console.IsOutputRedirected; + + if (isRedirected) { - stdout.Write(exiData, 0, exiData.Length); - stdout.Flush(); + // Binary output for redirection + using (var stdout = Console.OpenStandardOutput()) + { + stdout.Write(exiData, 0, exiData.Length); + stdout.Flush(); + } + } + else + { + // Hex string output for console display + Console.Write(BitConverter.ToString(exiData).Replace("-", "")); } return 0;