diff --git a/README.md b/README.md index ea7368d..237e7b7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Option | Description `-o, --output=VALUE` | Output file prefix (defalut port name) `--pipe` | Use named pipe instead of file `-h, --help` | Show this message and exit +`-s, --stdout` | Use stdout instead of file `portName` is `COM1`, `\\.\COM15` or `/dev/ttyUSB0` or similar definition. @@ -37,3 +38,10 @@ It is possible to run the application in pipe mode, so you can see realtime traf mono SerialPcap -o /tmp/wspipe --pipe [options] More info on Wireshark capture pipes can be seen on https://wiki.wireshark.org/CaptureSetup/Pipes + + +Using stdout (realtime) mode in both Windows and Linux +----------- +Another useful option to see realtime traffic in Wireshark is to use stdout piping: + + SerialPcap -s | wireshark -k -i - diff --git a/SerialPCAP/Program.cs b/SerialPCAP/Program.cs index 5fd8f6e..bd3f4ae 100644 --- a/SerialPCAP/Program.cs +++ b/SerialPCAP/Program.cs @@ -17,6 +17,7 @@ class MainClass static char parity = 'n'; static int stopbits = 1; static bool pipe = false; + static bool stdout = false; static void ShowHelp(OptionSet options) { @@ -24,10 +25,10 @@ static void ShowHelp(OptionSet options) Console.WriteLine("usage: serialpcap [options] "); options.WriteOptionDescriptions(Console.Out); Console.WriteLine(); - Console.WriteLine("Available portName:"); - // Get a list of serial port names. - string[] ports = SerialPort.GetPortNames(); - // Display each port name to the console. + Console.WriteLine("Available portName:"); + // Get a list of serial port names. + string[] ports = SerialPort.GetPortNames(); + // Display each port name to the console. foreach (string port in ports) { Console.WriteLine(" " + port); @@ -51,27 +52,37 @@ static void Capture(CaptureSerial capture, BinaryWriter writer) static void RunCapture() { - Console.WriteLine("Serial port: " + portName); - Console.WriteLine("Baud rate: " + baudRate + " Bd"); - Console.WriteLine("Parity: " + parity); - Console.WriteLine("Stopbits: " + stopbits); - Console.WriteLine("Frame gap: " + frameGapMs + " ms"); - Console.WriteLine("DLT: " + dlt); - Console.WriteLine("Output file: " + outputFile); - Console.WriteLine(); - Console.WriteLine("Starting capture (press Ctrl+c to stop)"); - + if (stdout == false) + { + Console.WriteLine("Serial port: " + portName); + Console.WriteLine("Baud rate: " + baudRate + " Bd"); + Console.WriteLine("Parity: " + parity); + Console.WriteLine("Stopbits: " + stopbits); + Console.WriteLine("Frame gap: " + frameGapMs + " ms"); + Console.WriteLine("DLT: " + dlt); + Console.WriteLine("Output file: " + outputFile); + Console.WriteLine(); + Console.WriteLine("Starting capture (press Ctrl+c to stop)"); + } using (var capture = new CaptureSerial(portName, baudRate, parity, stopbits, frameGapMs)) { - if (pipe && Environment.OSVersion.Platform != PlatformID.Unix) + if (stdout) + { + using (BinaryWriter writer = new BinaryWriter(Console.OpenStandardOutput())) + { + Capture(capture, writer); + } + } + else if (pipe && Environment.OSVersion.Platform != PlatformID.Unix) { using (BinaryWriter writer = new BinaryWriter(new NamedPipeServerStream(outputFile, PipeDirection.Out))) { Capture(capture, writer); } } - else { + else + { using (BinaryWriter writer = new BinaryWriter(File.Open(outputFile, FileMode.Create))) { Capture(capture, writer); @@ -94,17 +105,18 @@ public static void Main(string[] args) {"o|output=", "Output file prefix or pipe (defalut port name)", o => outputFilePrefix = o}, {"pipe", "Use named pipe instead of file", p => pipe = p != null}, {"h|help", "Show this message and exit", h => shouldShowHelp = h != null}, + {"s|stdout", "Use stdout instead of file", s => stdout = s != null}, }; List extra; try - { - // parse the command line + { + // parse the command line extra = options.Parse(args); } catch (OptionException e) - { - // output some error message + { + // output some error message Console.Write("serialpcap: "); Console.WriteLine(e.Message); Console.WriteLine("Try `serialpcap --help' for more information."); @@ -127,7 +139,8 @@ public static void Main(string[] args) } outputFile = outputFilePrefix; } - else { + else + { if (outputFilePrefix == "") { var s = portName.Split(new char[] { '/', '\\' });