protocol_decode
[zc] / unpack.pl
index 2b93800..0251ba8 100755 (executable)
--- a/unpack.pl
+++ b/unpack.pl
@@ -9,6 +9,8 @@ use Protocol;
 
 my $debug = $ENV{DEBUG};
 
+$| = 1;
+
 warn "## protocol = ",dump( $protocol ) if $debug;
 
 my $pkg_nr = 0;
@@ -74,7 +76,7 @@ while ( $data ) {
        my $data_id = unpack( 'C', substr($data,0,1) );
        if ( ! exists( $protocol->{$data_id}->{description} ) ) {
                my $len = unpack('C', substr($data,1,1));
-               printf "ERROR: no description for data_id %d 0x%2x len %d SKIPPING!\n", $data_id, $data_id, $len;
+               printf "ERROR: no description for data_id %d 0x%2x len %d [%s] SKIPPING!\n", $data_id, $data_id, $len, unpack('H*', substr($data,2,$len));
                $data = substr($data,2 + $len);
                next;
        }
@@ -82,6 +84,7 @@ while ( $data ) {
        my $pack_fmt = $protocol->{$data_id}->{pack_fmt} || die "can't find pack_fmt for data_id $data_id";
 
        my $data_range;
+       my $data_len;
 
        if ( $data_id == 0x00 ) {
                # sequence number
@@ -98,7 +101,7 @@ while ( $data ) {
        } elsif ( $up_down eq 'up'   && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
                # type B
 
-               my $data_len = unpack('C', substr($data,1,1));
+               $data_len = unpack('C', substr($data,1,1));
                $data_range = substr($data,2, $data_len);
 
                $data = substr($data,2 + $data_len);
@@ -106,7 +109,7 @@ while ( $data ) {
        } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
                # type B
 
-               my $data_len = unpack('C', substr($data,1,1));
+               $data_len = unpack('C', substr($data,1,1));
                $data_range = substr($data,2, $data_len);
 
                $data = substr($data,2 + $data_len);
@@ -123,7 +126,9 @@ while ( $data ) {
                warn "ERROR unknown function_code = $function_code";
        }
 
-       my $v = unpack($pack_fmt, $data_range);
+       my @v = unpack($pack_fmt, $data_range);
+
+       my $v = join(' ', @v);
 
        if ( $data_id == 0x0c ) {
                $v = $v / 100;
@@ -132,7 +137,7 @@ while ( $data ) {
        }
 
        my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
-       print "$up_down | $data_id | $description | v=$v | hex:", unpack('H*', $data_range), " fmt:$pack_fmt | range:",$protocol->{$data_id}->{range}, " | remark:", $protocol->{$data_id}->{remark},"\n";
+       print "$up_down | $data_id 0x",unpack('H*', chr($data_id))," $data_len | $description | v=$v | hex:", unpack('H*', $data_range), " fmt:$pack_fmt | range:",$protocol->{$data_id}->{range}, " | remark:", $protocol->{$data_id}->{remark},"\n";
 }
 
 
@@ -154,8 +159,8 @@ while(<>) {
                ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
                $stat->{$sensor}->{$imei}->{$up_down}++;
        } elsif ( m{^\s+} ) {
-               s/\s\s\S\S\S+//g; # remove ascii
                s/^\s+/ /;
+               s/\s\s.+$//g; # remove ascii
                $raw .= $_;
                warn "++ $_\n";
        } else {