|< 1 2 >| | 11 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
$strToSend = "\xfe\x04\x01";
$CRC16 = GetCRC16($strToSend. "\x00\x00"); // ergibt "BED2"
echo "Checksum 1: ". sprintf("%X", $CRC16). "\r\n";
$strReceived = "\xfe\x04\x01\xbe\xd2";
$CRC16 = GetCRC16($strReceived); // ergibt 0
echo "Checksum 2: ". sprintf("%X", $CRC16). "\r\n";
function GetCRC16($s)
{
$CRCreg = 0xffff00;
for ($cp=0; $cp<strlen($s); $cp++)
{
$CRCreg |= ord($s[$cp]);
for ($bp=0; $bp<8; $bp++)
{
$CRCreg <<= 1;
if ($CRCreg & 0x1000000) $CRCreg ^= 0x100200; // hier ist das Polynom 0x1002 enthalten
$CRCreg &= 0xffffff;
}
}
return $CRCreg >> 8;
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
my $send_cmd;
my $strToSend;
my $strReceived;
my $CRC16;
my $CRCreg;
my $cp;
my $bp;
my @s;
my $s;
sub testcrc {
#Beim Senden ist zu beachten, dass für die Berechnung der Checksumme
#an den String noch zwei Null-Bytes ("\x00\x00") anzuhängen sind.
#Die berechnete Checksumme wird dann anstelle dieser beiden Null-Bytes gesendet.
#("\x00\x00")
$strToSend = "\0xfe\0x04\0x01";
$CRC16 = GetCRC16($strToSend. 0x0000); # ergibt / gives "BED2"
print "Checksum 1: ". sprintf("%X", $CRC16). "\r\n";
$strReceived = "\0xfe\0x04\0x01\0xBE\0xD2";
$CRC16 = GetCRC16($strReceived); # ergibt / gives "0"
print "Checksum 2: ". sprintf("%X", $CRC16). "\r\n";
goto MENUE;
}
sub GetCRC16{
$CRCreg = 0xffff00;
$s = $_[0];
@s=split(//,$s);
for ($cp=0; $cp<length($s); $cp++){
$CRCreg |= ord($s[$cp]);
for ($bp=0; $bp<8; $bp++){
$CRCreg <<= 1;
if ($CRCreg & 0x1000000){
$CRCreg ^= 0x100200; # hier ist das Polynom 0x1002 enthalten
$CRCreg &= 0xffffff;
}
}
}
return $CRCreg >> 8;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/perl
use strict;
use warnings;
my $strToSend = "\xfe\x04\x01";
my $CRC16 = GetCRC16($strToSend. "\x00\x00"); # ergibt "BED2"
print "Checksum 1: ". sprintf("%X", $CRC16). "\n";
my $strReceived = "\xfe\x04\x01\xbe\xd2";
$CRC16 = GetCRC16($strReceived); # ergibt 0
print "Checksum 2: ". sprintf("%X", $CRC16). "\n";
sub GetCRC16 {
my $s = shift;
my $CRCreg = 0xffff00;
for (my $cp=0; $cp<length($s); $cp++) {
$CRCreg |= ord( substr($s,$cp,1) );
for (my $bp=0; $bp<8; $bp++) {
$CRCreg <<= 1;
if ($CRCreg & 0x1000000) {
$CRCreg ^= 0x100200; # hier ist das Polynom 0x1002 enthalten
}
$CRCreg &= 0xffffff;
}
}
return $CRCreg >> 8;
}
1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/perl
use strict;
use warnings;
my $hexdata = "fe0401";
my @split = unpack( "A2" x (length($hexdata)/2), $hexdata );
my $strToSend = ("\\x". join ("\\x",@split)."\\x00\\x00");
print $strToSend,"\n";
my $CRC16 = GetCRC16($strToSend); # ergibt "BED2"
print "Checksum A: ". sprintf("%x", $CRC16). "\n";
$strToSend = "\xfe\x04\x01";
print $strToSend,"\n";
$CRC16 = GetCRC16($strToSend. "\x00\x00"); # ergibt "BED2"
print "Checksum 1: ". sprintf("%x", $CRC16). "\n";
sub GetCRC16 {
my $s = shift;
my $CRCreg = 0xffff00;
for (my $cp=0; $cp<length($s); $cp++) {
$CRCreg |= ord( substr($s,$cp,1) );
for (my $bp=0; $bp<8; $bp++) {
$CRCreg <<= 1;
if ($CRCreg & 0x1000000) {
$CRCreg ^= 0x100200; # hier ist das Polynom 0x1002 enthalten
}
$CRCreg &= 0xffffff;
}
}
return $CRCreg >> 8;
}
1;
$hexdata =~ s/(..)/\\x$1/g;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/perl
use strict;
use warnings;
my @bytes = (0xFE, 0x04, 0x01); #command for get software version from HS485PCI
my $cmd = pack("C*", @bytes);
print $cmd,"\n";
my $CRC16 = GetCRC16($cmd. "\x00\x00"); # ergibt "BED2"
print "Checksum A: ". sprintf("%x", $CRC16). "\n";
my $strToSend = "\xfe\x04\x01";
print $strToSend,"\n";
$CRC16 = GetCRC16($strToSend. "\x00\x00"); # ergibt "BED2"
print "Checksum 1: ". sprintf("%x", $CRC16). "\n";
sub GetCRC16 {
my $s = shift;
my $CRCreg = 0xffff00;
for (my $cp=0; $cp<length($s); $cp++) {
$CRCreg |= ord( substr($s,$cp,1) );
for (my $bp=0; $bp<8; $bp++) {
$CRCreg <<= 1;
if ($CRCreg & 0x1000000) {
$CRCreg ^= 0x100200; # hier ist das Polynom 0x1002 enthalten
}
$CRCreg &= 0xffffff;
}
}
return $CRCreg >> 8;
}
1;
|< 1 2 >| | 11 Einträge, 2 Seiten |