fix formatting, divide by 100 if needed
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 17 Sep 2020 12:00:13 +0000 (14:00 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 17 Sep 2020 12:00:13 +0000 (14:00 +0200)
unpack.pl

index 906c4de..51a3786 100755 (executable)
--- a/unpack.pl
+++ b/unpack.pl
@@ -118,7 +118,8 @@ push @raw, qq{
   45 01 11 01 01 17 01 16 18 01 01 19 01 02 2a 69  E.............*i
 };
 
-my $raw = $raw[0];
+my $raw_nr = shift @ARGV || 0;
+my $raw = $raw[$raw_nr];
 $raw =~ s/  \S{16}\n//gs;
 $raw =~ s/^\s+//;
 my $up_down = 'up';
@@ -139,11 +140,11 @@ if ( my $crc = modbus_crc16( substr($bin,0,-2) ) ) {
        }
 }
 
-my ( $header, $ver, $function_code, $len ) = unpack "CCCv", $bin;
+my ( $header, $ver, $function_code, $len ) = unpack("CCCv", $bin);
 
 my $data = substr($bin,5,-2);
 
-warn "header = $header ver = $ver function_code = $function_code len = $len == ",length($data), " data = ",dump($data), " cksum = $cksum";
+warn "header = $header ver = $ver function_code = $function_code len = $len == ",length($data), " data = ",dump($data), " cksum = ", unpack('H*',$cksum);
 
 warn "header corrupt: $header" if $header != 0x5a;
 warn "invalid length $len got ",length($data) if length($data) != $len;
@@ -214,8 +215,16 @@ while ( $data ) {
                warn "ERROR unknown function_code = $function_code";
        }
 
+       my $v = unpack($pack_fmt, $data_range);
+
+       if ( $data_id == 0x0c ) {
+               $v = $v / 100;
+       } elsif ( $data_id == 0x0d ) {
+               $v = $v / 100;
+       }
+
        my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
-       print "$up_down | $data_id | $description | hex:", unpack('h*', $data_range), " fmt:$pack_fmt [",unpack($pack_fmt, $data_range), "] range:",$protocol->{$data_id}->{range}, " remark:", $protocol->{$data_id}->{remark},"\n";
+       print "$up_down | $data_id | $description | hex:", unpack('H*', $data_range), " fmt:$pack_fmt [v=$v] range:",$protocol->{$data_id}->{range}, " remark:", $protocol->{$data_id}->{remark},"\n";
 }
 
 # 6. Protocol Format 
@@ -231,17 +240,17 @@ __DATA__
 0x09   X axis relative angle           f Float(4)      R       ‐90°~90°    0       Return the X angle value according to the set relative zero 
 0x0A   Y axis relative angle           f Float(4)      R       ‐90°~90°    0       Return the Y angle value according to the set relative zero 
 0x0C   Sensor temperature              s Word(2)       R       ‐32768~32767  /       signed,sensor temperature =Data/100, unit ℃ 
-0x0D   Power source voltage            s Word(2)       R       0~65535         /       Voltage= Data/100, unit V 
+0x0D   Power source voltage            S Word(2)       R       0~65535         /       Voltage= Data/100, unit V 
 0x11   Arming/disarming                C Byte(1)       R/W     0~255           1       0 means disarming, non‐zero means arming 
 0x12   Alarm delay time                C Byte(1)       R/W     3~255           20       The unit is 0.1 second, which means that the product responds to the alarm only after the alarm has exceeded the alarm angle for a certain period of time. 
 
 0x13   Restore factory setting         C Byte(1)       R/W     0~255           0       0: Do nothing Non‐zero: Restore the non‐network‐related parameters of the sensor. 
-0x14   Server IP&port                  CCCCs 4*Byte(1)+Word(2) R/W     /       CTIOT:117.60.157.137,5683 MQTT:0.0.0.0,0        The server address should be IP, not the domain name; using the domain name may cause the connection server to be unstable and cause data loss.
+0x14   Server IP&port                  CCCCs 4*Byte(1)+Word(2) R/W             /       CTIOT:117.60.157.137,5683 MQTT:0.0.0.0,0        The server address should be IP, not the domain name; using the domain name may cause the connection server to be unstable and cause data loss.
 0x17   Signal strength                 C Byte(1)       R       10~34           /       A larger value indicates a stronger signal 
 0x18   Sensor operating mode           C Byte(1)       R/W     0               0       The sensor can only work in absolute measurement mode 
-0x19   Alarm axis                      C Byte(1)       R       0~      3       /       0: no alarm; 1: X‐axis alarm 2: Y axis alarm; 3: X/Y axis alarm at the same time 
+0x19   Alarm axis                      C Byte(1)       R       0~3             /       0: no alarm; 1: X‐axis alarm 2: Y axis alarm; 3: X/Y axis alarm at the same time 
 0x1A   SIM card ID                     Q QWord(8)      R       0~18446744073709551615  /       Take the first 19 digits, the last digit is discarded 
-0x21   Heartbeat interval              l DWord(4)      R/W     60~131071        86400  Interval at which the device periodically uploads data to the server 
+0x21   Heartbeat interval              l DWord(4)      R/W     60~131071       86400   Interval at which the device periodically uploads data to the server 
 0x22   IMEI number of the device       Q QWord(8)       R      0~18446744073709551615  /       Refers to the IMEI of the NB‐IOT network module in the product. 
 0x23   Backup server IP&port           CCCCs 4*Byte(1)+Word(2) R/W     /       CTIOT:117.60.157.137,5683 MQTT:0.0.0.0,0        / 
 0x24   Backup server enable            C Byte(1)       R/W     0~255   0       0 means off, non‐zero means on