| 1 | package Lab::Moose::Instrument::SCPIBlock; | ||||||||||||||||||||||||||||
| 2 | 4 | $Lab::Moose::Instrument::SCPIBlock::VERSION = '3.881'; | |||||||||||||||||||||||||||
| 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/ | |||||||||||||||||||||||||||
| 11 | precision_param | ||||||||||||||||||||||||||||
| 12 | 4 | ▶ | /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | sub block_length { | ||||||||||||||||||||||||||||
| 25 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
block_length: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub block_to_array { | ||||||||||||||||||||||||||||
| 43 | 9 | ▶ | my ( $self, %args ) = validated_hash( | ||||||||||||||||||||||||||
|
Subroutine
block_to_array: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub set_data_format_precision { | ||||||||||||||||||||||||||||
| 85 | 9 | ▶ | my ( $self, %args ) = validated_hash( | ||||||||||||||||||||||||||
|
Subroutine
set_data_format_precision: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =pod | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 NAME | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Lab::Moose::Instrument::SCPIBlock - Role for handling SCPI/IEEE 488.2 block data | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 VERSION | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | version 3.881 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | So far, only definite length floating point data of type 'REAL' is | ||||||||||||||||||||||||||||
| 119 | supported. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | See "8.7.9 <DEFINITE LENGTH ARBITRARY BLOCK RESPONSE DATA>" in IEEE 488.2. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 METHODS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head2 block_to_array | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | my $array_ref = $self->block_to_array( | ||||||||||||||||||||||||||||
| 128 | binary => "#232${bytes}"; | ||||||||||||||||||||||||||||
| 129 | precision => 'double' | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Convert block data to arrayref, where the binary block holds floating point | ||||||||||||||||||||||||||||
| 133 | values in native byte-order. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 block_length | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | my $read_length = $self->block_length( | ||||||||||||||||||||||||||||
| 138 | num_points => $num_points, | ||||||||||||||||||||||||||||
| 139 | precision => $precision | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Calulate block length for use in C<read_length> parameter. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 set_data_format_precision | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | $self->set_data_format_precision( precision => 'double' ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Set used floating point type. Has to be 'single' (default) or 'double'. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Copyright 2016 Simon Reinhardt | ||||||||||||||||||||||||||||
| 155 | 2017 Andreas K. Huettel, Simon Reinhardt | ||||||||||||||||||||||||||||
| 156 | 2020 Andreas K. Huettel, Sam Bingner | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 160 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||