############################################################ # A simple script to output a raw dump of serial data. Think # of it as a wireshark for serial data. No fancy filtering # or tools, just a raw dump of serial data. # # Released under the GPLv2 # Scott Baker - scott@perturb.org - 2013-07-31 # # Similar to Serial line sniffer: # http://sourceforge.net/projects/slsnif/ ############################################################ use Device::SerialPort; use Data::Dumper; use autodie; use strict; $| = 1; # No output buffering my $port = Device::SerialPort->new("/dev/ttyUSB0"); # Change to suit your environment $port->baudrate(115200); $port->databits(8); $port->parity("none"); $port->stopbits(1); $port->read_char_time(0); # don't wait for each character $port->read_const_time(1000); # 1 second per unfulfilled "read" call my ($dec,$bin,$hex) = 0; my $args = join(" ",@ARGV); if ($args =~ /--hex/) { $hex = 1; } if ($args =~ /--dec/) { $dec = 1; } if ($args =~ /--bin/) { $bin = 1; } # Default to hex if nothing selected if (!$hex && !$dec && !$bin) { $hex = 1; } ############################################################################ my $num_out = 0; my $tmp_char = ''; my $printable = ''; while (1) { my ($count,$byte) = $port->read(1); # will read _up to_ 255 chars if ($count > 0) { my $ord = ord($byte); $tmp_char = printable_char($ord); $printable .= $tmp_char; $num_out++; if ($hex) { printf("%02x ",$ord); if ($num_out > 0 && $num_out % 30 == 0) { print " $printable\n"; $printable = ''; } } elsif ($dec) { printf("%03i ",$ord); if ($num_out > 0 && $num_out % 21 == 0) { print " $printable\n"; $printable = ''; } } elsif ($bin) { printf("%08b ",$ord); if ($num_out > 0 && $num_out % 10 == 0) { print " $printable\n"; $printable = ''; } } } } sub printable_char { my $num = shift(); my $ret; if ($num >= 32 && $num <= 126) { $ret = chr($num); } else { $ret = "."; } return $ret; }