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 SKIPPING!\n", $data_id, $data_id, $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";
86 if ( $data_id == 0x00 ) {
89 $data_range = substr($data,2,4);
90 $data = substr($data,6);
92 } elsif ( $up_down eq 'down' && $function_code == 0x03 ) {
95 $data_range = substr($data,0,1);
96 $data = substr($data,1);
98 } elsif ( $up_down eq 'up' && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
101 my $data_len = unpack('C', substr($data,1,1));
102 $data_range = substr($data,2, $data_len);
104 $data = substr($data,2 + $data_len);
106 } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
109 my $data_len = unpack('C', substr($data,1,1));
110 $data_range = substr($data,2, $data_len);
112 $data = substr($data,2 + $data_len);
114 } elsif ( $up_down eq 'up' && $function_code == 0x06 ) {
117 # FIXME check first byte?
118 my $data_range = substr($data,1,1);
120 $data = substr($data,2);
123 warn "ERROR unknown function_code = $function_code";
126 my $v = unpack($pack_fmt, $data_range);
128 if ( $data_id == 0x0c ) {
130 } elsif ( $data_id == 0x0d ) {
134 my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
135 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";
142 my ( $timestamp, $sensor, $imei, $up_down );
147 if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
149 $raw =~ s{^\s+}{}; # strip leading spaces
150 print "## $timestamp $sensor $imei $up_down $raw\n";
151 protocol_decode( $up_down, $raw );
154 ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
155 $stat->{$sensor}->{$imei}->{$up_down}++;
156 } elsif ( m{^\s+} ) {
157 s/\s\s\S\S\S+//g; # remove ascii
168 $raw =~ s{^\s+}{}; # strip leading spaces
169 print "## $timestamp $sensor $imei $up_down $raw\n";
170 protocol_decode( $up_down, $raw );
173 print "stat = ",dump($stat), "\n";