2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 06:39:11 +00:00
fhem-mirror/fhem/contrib/checkmsg.pl
rudolfkoenig 71fe020f84 Initial version
git-svn-id: https://svn.fhem.de/fhem/trunk@3 2b470e98-0d58-463d-a4d8-8e2adae1ed80
2007-01-30 12:47:36 +00:00

27 lines
664 B
Raku
Executable File

die("Usage: checkmsg HEX-FHZ-MESSAGE\n") if(int(@ARGV) != 1);
my $msg = $ARGV[0];
die("Bad prefix (not 0x81)\n") if($msg !~ m/^81/);
print("Prefix is ok (0x81)\n");
my $l = hex(substr($msg, 2, 2));
my $rl = length($msg)/2-2;
die("Bad length $l (should be $rl)\n") if($rl != $l);
print("Length is ok ($l)\n");
my @data;
for(my $i = 8; $i < length($msg); $i += 2) {
push(@data, ord(pack('H*', substr($msg, $i, 2))));
}
my $rcrc = 0;
map { $rcrc += $_; } @data;
$rcrc &= 0xFF;
my $crc = hex(substr($msg, 6, 2));
my $str = sprintf("Bad CRC 0x%02x (should be 0x%02x)\n", $crc, $rcrc);
die($str) if($crc ne $rcrc);
printf("CRC is ok (0x%02x)\n", $crc);
exit(0);