| 1 | package Lab::Moose::Instrument::SCPIBlock; | ||||||||||||||||||||||||||||
| 2 | 4 | $Lab::Moose::Instrument::SCPIBlock::VERSION = '3.931'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: Role for handling SCPI/IEEE 488.2 block data | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use MooseX::Params::Validate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Lab::Moose::Instrument qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | precision_param | ||||||||||||||||||||||||||||
| 12 | 4 | /; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | with qw/ | |||||||||||||||||||||||||||
| 19 | Lab::Moose::Instrument::SCPI::Sense::Sweep | ||||||||||||||||||||||||||||
| 20 | Lab::Moose::Instrument::SCPI::Format | ||||||||||||||||||||||||||||
| 21 | /; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | sub block_length { | ||||||||||||||||||||||||||
|
Subroutine
block_length: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 9 | my ( $num_points, $precision ) = validated_list( | |||||||||||||||||||||||||||
| 27 | \@_, | ||||||||||||||||||||||||||||
| 28 | num_points => { isa => 'Int' }, | ||||||||||||||||||||||||||||
| 29 | precision_param(), | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 9 | ▶ | my $point_length | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 33 | = $precision eq 'single' ? 4 | ||||||||||||||||||||||||||||
| 34 | : $precision eq 'double' ? 8 | ||||||||||||||||||||||||||||
| 35 | : croak("unknown precision $precision"); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 9 | my $read_length = $num_points * $point_length; | |||||||||||||||||||||||||||
| 38 | 9 | $read_length += length("#d") + length($read_length) + length("\n"); | |||||||||||||||||||||||||||
| 39 | 9 | return $read_length; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 9 | ▶ | sub block_to_array { | ||||||||||||||||||||||||||
|
Subroutine
block_to_array: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 9 | my ( $self, %args ) = validated_hash( | |||||||||||||||||||||||||||
| 44 | \@_, | ||||||||||||||||||||||||||||
| 45 | binary => { isa => 'Str' }, | ||||||||||||||||||||||||||||
| 46 | precision_param(), | ||||||||||||||||||||||||||||
| 47 | , | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 9 | my $precision = delete $args{precision}; | |||||||||||||||||||||||||||
| 51 | 9 | my $binary = delete $args{binary}; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 9 | ▶ | if ( substr( $binary, 0, 1 ) ne '#' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | croak 'does not look like binary data'; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 9 | my %endians = ( | |||||||||||||||||||||||||||
| 58 | native => '', | ||||||||||||||||||||||||||||
| 59 | big => '>', | ||||||||||||||||||||||||||||
| 60 | little => '<' | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | 9 | my $endianflag = $endians{$self->endian}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 9 | my $num_digits = substr( $binary, 1, 1 ); | |||||||||||||||||||||||||||
| 65 | 9 | my $num_bytes = substr( $binary, 2, $num_digits ); | |||||||||||||||||||||||||||
| 66 | 9 | my $expected_length = $num_bytes + $num_digits + 2; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # $binary might have a trailing newline, so do not check for equality. | ||||||||||||||||||||||||||||
| 69 | 9 | ▶ | if ( length $binary < $expected_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | croak "incomplete data: expected_length: $expected_length," | |||||||||||||||||||||||||||
| 71 | . " received length: ", length $binary; | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | ▶ | my @floats = unpack( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | $precision eq 'single' ? "f$endianflag*" : "d$endianflag*", | ||||||||||||||||||||||||||||
| 76 | substr( $binary, 2 + $num_digits, $num_bytes ) | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 9 | return \@floats; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | ▶ | sub set_data_format_precision { | ||||||||||||||||||||||||||
|
Subroutine
set_data_format_precision: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 9 | my ( $self, %args ) = validated_hash( | |||||||||||||||||||||||||||
| 86 | \@_, | ||||||||||||||||||||||||||||
| 87 | precision_param(), | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | my $precision = delete $args{precision}; | |||||||||||||||||||||||||||
| 91 | 9 | ▶ | my $length = $precision eq 'single' ? 32 : 64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 9 | my $format = $self->cached_format_data(); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 9 | ▶ | if ( $format->[0] ne 'REAL' || $format->[1] != $length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$format->[0] ne "REAL" or $format->[1] != $length
MC/DC50%1/2
$format->[0] ne "REAL" or $format->[1] != $length
$format->[0] ne "REAL"
$format->[1] != $length
Truth table66%2/3
A or B
A= $format->[0] ne "REAL"B= $format->[1] != $length
| |||||||||||||||||||||||||||||
| 95 | 3 | carp "setting data format: REAL, $length"; | |||||||||||||||||||||||||||
| 96 | 3 | $self->format_data( format => 'REAL', length => $length ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 4 | 1; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||