added parts of protocol for ZCT330E
[zc] / unpack.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 use lib '.';
8 use Protocol;
9
10 my $debug = $ENV{DEBUG};
11
12 warn "## protocol = ",dump( $protocol ) if $debug;
13
14 my $pkg_nr = 0;
15
16 sub protocol_decode {
17         my ( $up_down, $hex ) = @_;
18
19 warn "up/down: $up_down hex = ",dump($hex);
20
21 my $bin = join('', map { chr(hex($_)) } split(/\s+/,$hex));
22
23 warn "bin = ", dump($bin);
24
25 if ( $debug ) {
26         open(my $dump, '>', "/dev/shm/zc.$pkg_nr.$up_down");
27         print $dump $bin;
28         close($dump);
29         $pkg_nr++;
30 }
31
32 my $cksum = substr($bin, -2, 2);
33
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";
37         } else {
38                 warn "CRC OK ", unpack('H*',$crc), "\n";
39         }
40 }
41
42 my ( $header, $ver, $function_code, $len ) = unpack("CCCv", $bin);
43
44 my $data = substr($bin,5,-2);
45
46 warn "header = $header ver = $ver function_code = $function_code len = $len == ",length($data), " data = ",dump($data), " cksum = ", unpack('H*',$cksum);
47
48 warn "header corrupt: $header" if $header != 0x5a;
49 warn "invalid length $len got ",length($data) if length($data) != $len;
50
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',
56 };
57
58 my $function_code_downstream = {
59 0x03 => 'Downstream read parameter frame',
60 0x06 => 'Downstream write parameter frame',
61 };
62
63 print "Function code: $function_code ", 
64         $up_down eq 'up' ? $function_code_upstream->{ $function_code } : $function_code_downstream->{ $function_code }, "\n";
65
66
67
68
69 while ( $data ) {
70         my $hex = unpack('H*', $data);
71         $hex =~ s/(..)/$1 /g;
72         warn "XXX data = $hex\n";
73
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);
79                 next;
80         }
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";
83
84         my $data_range;
85         my $data_len;
86
87         if ( $data_id == 0x00 ) {
88                 # sequence number
89
90                 $data_range = substr($data,2,4);
91                 $data = substr($data,6);
92
93         } elsif ( $up_down eq 'down' && $function_code == 0x03 ) {
94                 # type A
95
96                 $data_range = substr($data,0,1);
97                 $data = substr($data,1);
98
99         } elsif ( $up_down eq 'up'   && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
100                 # type B
101
102                 $data_len = unpack('C', substr($data,1,1));
103                 $data_range = substr($data,2, $data_len);
104
105                 $data = substr($data,2 + $data_len);
106
107         } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
108                 # type B
109
110                 $data_len = unpack('C', substr($data,1,1));
111                 $data_range = substr($data,2, $data_len);
112
113                 $data = substr($data,2 + $data_len);
114
115         } elsif ( $up_down eq 'up' && $function_code == 0x06 ) {
116                 # type C
117
118                 # FIXME check first byte?
119                 my $data_range = substr($data,1,1);
120
121                 $data = substr($data,2);
122
123         } else {
124                 warn "ERROR unknown function_code = $function_code";
125         }
126
127         my @v = unpack($pack_fmt, $data_range);
128
129         my $v = join(' ', @v);
130
131         if ( $data_id == 0x0c ) {
132                 $v = $v / 100;
133         } elsif ( $data_id == 0x0d ) {
134                 $v = $v / 100;
135         }
136
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";
139 }
140
141
142 } # protocol_decode
143
144 my $raw;
145 my ( $timestamp, $sensor, $imei, $up_down );
146 my $stat;
147 while(<>) {
148         chomp;
149
150         if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
151                 if ( $raw ) {
152                         $raw =~ s{^\s+}{}; # strip leading spaces
153                         print "## $timestamp $sensor $imei $up_down $raw\n";
154                         protocol_decode( $up_down, $raw );
155                         $raw = '';
156                 }
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
161                 s/^\s+/ /;
162                 $raw .= $_;
163                 warn "++ $_\n";
164         } else {
165                 warn "IGNORE: $_\n";
166         }
167
168 }
169
170 if ( $raw ) {
171         $raw =~ s{^\s+}{}; # strip leading spaces
172         print "## $timestamp $sensor $imei $up_down $raw\n";
173         protocol_decode( $up_down, $raw );
174 }
175
176 print "stat = ",dump($stat), "\n";
177