need help to understand a Perl script
I'm working on serial communication with a multimeter VA18B that uses PC Link data protocol. The p开发者_运维百科roblem is that I cannot find any documentation for this protocol. The only thing I've got is a Perl script that decodes the frame (as far as I know, the frame consists of 14 bytes). Unfortunately the script is pretty complicated for someone who don't know Perl at all.
Can someone explain the below code?
sub decode_bin_str { my ($AC, $DC, $auto, $unknown1, $minus, $digi1, $dot1, $digi2, $dot2, $digi3, $dot3, $digi4, $micro, $unknown2, $kilo, $diode_test, $milli, $percent, $mega, $cont_check, $unknown3, $ohm, $rel, $hold, $amp, $volt, $hz, $unknown4, $min, $unknown5, $celsius, $max) = shift =~ /^(.)(.)(.)(.)(.)(.{7})(.)(.{7})(.)(.{7})(.)(.{7}) (.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) *$/x; my %digi = ( "1111101" => 0, "0000101" => 1, "1011011" => 2, "0011111" => 3, "0100111" => 4, "0111110" => 5, "1111110" => 6, "0010101" => 7, "1111111" => 8, "0111111" => 9, ); my $val = ($minus ? "-" : "") . $digi{$digi1} . ($dot1 ? "." : "") . $digi{$digi2} . ($dot2 ? "." : "") . $digi{$digi3} . ($dot3 ? "." : "") . $digi{$digi4}; my $flags = join(" ", $AC ? "AC" : (), $DC ? "DC" : (), $auto ? "auto" : (), $diode_test ? "diode_test" : (), $cont_check ? "cont_check" : (), $rel ? "rel" : (), $hold ? "hold" : (), $min ? "min" : (), $max ? "max" : ()); my $unit = ($micro ? "u" : "") . ($kilo ? "k" : "") . ($milli ? "m" : "") . ($mega ? "M" : "") . ($percent ? "%" : "") . ($ohm ? "Ohm" : "") . ($amp ? "A" : "") . ($volt ? "V" : "") . ($hz ? "Hz" : "") . ($celsius ? "C" : ""); $val, $flags, $unit; }
This function take a binary (0/1) string. The regex is a pattern:
/^(.)(.).....(.{7})......
(.)
mean one charactor, (.{7})
means 7 of them.
my ($AC, $DC, $auto, $unknown1.......= shift =~ /^(.)(.)(.)(.) ....
means, given 1011.....
as input, AC
would be 1
, DC
would be 0
and auto
/unknown1
will be 1
.
Digit1/2/3/4 are the digits, dot1
...dot4
tells where you put the decimal point.
auto
/diode_test
/.. say the mode..
ohm
/volt
/.... say which unit you are using.
The rest is pretty easy.
精彩评论