5 use Data::Dump qw(dump);
10 my $debug = $ENV{DEBUG};
12 warn "## protocol = ",dump( $protocol ) if $debug;
17 my ( $up_down, $hex ) = @_;
19 warn "up/down: $up_down hex = ",dump($hex);
21 my $bin = join('', map { chr(hex($_)) } split(/\s+/,$hex));
23 warn "bin = ", dump($bin);
26 open(my $dump, '>', "/dev/shm/zc.$pkg_nr.$up_down");
32 my $cksum = substr($bin, -2, 2);
34 if ( my $crc = modbus_crc16( substr($bin,0,-2) ) ) {
35 if ( $crc ne $cksum ) {
36 warn "CRC error, got ",unpack('H*',$crc), " expected ", unpack('H*',$cksum), "\n";
38 warn "CRC OK ", unpack('H*',$crc), "\n";
42 my ( $header, $ver, $function_code, $len ) = unpack("CCCv", $bin);
44 my $data = substr($bin,5,-2);
46 warn "header = $header ver = $ver function_code = $function_code len = $len == ",length($data), " data = ",dump($data), " cksum = ", unpack('H*',$cksum);
48 warn "header corrupt: $header" if $header != 0x5a;
49 warn "invalid length $len got ",length($data) if length($data) != $len;
51 my $function_code_upstream = {
52 0x07 => 'Upstream Heartbeat Frame',
53 0x08 => 'Upstream alarm frame',
54 0x03 => 'Upstream Read Parameter Frame',
55 0x06 => 'Upstream return write parameter frame',
58 my $function_code_downstream = {
59 0x03 => 'Downstream read parameter frame',
60 0x06 => 'Downstream write parameter frame',
63 print "Function code: $function_code ",
64 $up_down eq 'up' ? $function_code_upstream->{ $function_code } : $function_code_downstream->{ $function_code }, "\n";
70 my $hex = unpack('H*', $data);
72 warn "XXX data = $hex\n";
74 my $data_id = unpack( 'C', substr($data,0,1) );
75 if ( ! exists( $protocol->{$data_id}->{description} ) ) {
76 my $len = unpack('C', substr($data,1,1));
77 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));
78 $data = substr($data,2 + $len);
81 my $data_id_desc = $protocol->{$data_id}->{description};
82 my $pack_fmt = $protocol->{$data_id}->{pack_fmt} || die "can't find pack_fmt for data_id $data_id";
87 if ( $data_id == 0x00 ) {
90 $data_range = substr($data,2,4);
91 $data = substr($data,6);
93 } elsif ( $up_down eq 'down' && $function_code == 0x03 ) {
96 $data_range = substr($data,0,1);
97 $data = substr($data,1);
99 } elsif ( $up_down eq 'up' && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
102 $data_len = unpack('C', substr($data,1,1));
103 $data_range = substr($data,2, $data_len);
105 $data = substr($data,2 + $data_len);
107 } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
110 $data_len = unpack('C', substr($data,1,1));
111 $data_range = substr($data,2, $data_len);
113 $data = substr($data,2 + $data_len);
115 } elsif ( $up_down eq 'up' && $function_code == 0x06 ) {
118 # FIXME check first byte?
119 my $data_range = substr($data,1,1);
121 $data = substr($data,2);
124 warn "ERROR unknown function_code = $function_code";
127 my @v = unpack($pack_fmt, $data_range);
129 my $v = join(' ', @v);
131 if ( $data_id == 0x0c ) {
133 } elsif ( $data_id == 0x0d ) {
137 my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
138 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";
145 my ( $timestamp, $sensor, $imei, $up_down );
150 if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
152 $raw =~ s{^\s+}{}; # strip leading spaces
153 print "## $timestamp $sensor $imei $up_down $raw\n";
154 protocol_decode( $up_down, $raw );
157 ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
158 $stat->{$sensor}->{$imei}->{$up_down}++;
159 } elsif ( m{^\s+} ) {
160 s/\s\s\S\S\S+//g; # remove ascii
171 $raw =~ s{^\s+}{}; # strip leading spaces
172 print "## $timestamp $sensor $imei $up_down $raw\n";
173 protocol_decode( $up_down, $raw );
176 print "stat = ",dump($stat), "\n";