Thread python nach perl -> pack / unpack
(10 answers)
Opened by egal123 at 2015-12-09 19:41
so, ich hab das jetzt mit deiner anregung so
Code (perl): (dl
)
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #!/usr/bin/perl use warnings; use strict; sub makeBitstream { my $lenarr = $#_ + 1; if ($lenarr % 2) { die "Uneven number of elements in array."; } my @l = (); my $i; my $x = 0; while ($x < $lenarr / 2) { push(@l, shift(@_)); $x++; } my @v = @_; my $bitstream = ""; for ($i=0; $i<=$#v; $i++) { my $s = "%0" . $l[$i] . "b"; $bitstream .= sprintf("$s", $v[$i]); } return $bitstream; } sub makeThreeBytes { my $bitstream = shift; my $i; my @listOfBytes = (substr($bitstream, 0, 8), substr($bitstream, 8, 8), substr($bitstream, 16, 8)); for ($i=0; $i<=$#listOfBytes;$i++) { $listOfBytes[$i] = oct("0b". $listOfBytes[$i]); } return \@listOfBytes; } sub makeChecksum { my $listOfBytes = shift; my $checksum = 0; foreach my $i (@{$listOfBytes}) { $checksum += $i; } # print "checksum1: $checksum\n"; $checksum -= 512 if ($checksum > 511); $checksum -= 256 if ($checksum > 255); $checksum -= 128 if ($checksum > 127); # print "checksum1: $checksum\n"; return $checksum; } sub makeCommandStream { my @params = @_; my @a = ((5, 1, 10, 8), @params); my $bitstream = makeBitstream(@a); my $listOfBytes = makeThreeBytes($bitstream); push @params, makeChecksum($listOfBytes); my @b = ((5, 1, 10, 8, 8), @params); return makeBitstream(@b); } print makeCommandStream(2, 0, 64, 2) . "\n"; mal schauen ob das so alles passt, mal über ttyUSB0 verschicken und schauen ob was sinnvolles zurück kommt ... |