| 1 | package Lab::Moose::Connection::USB; | ||
| 2 | $Lab::Moose::Connection::USB::VERSION = '3.881'; | ||
| 3 | #ABSTRACT: Connection backend to USB Test & Measurement (USBTMC) bus | ||
| 4 | |||
| 5 | use v5.20; | ||
| 6 | |||
| 7 | |||
| 8 | use Moose; | ||
| 9 | use MooseX::Params::Validate; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | use Lab::Moose::Instrument qw/timeout_param read_length_param/; | ||
| 13 | use USB::TMC; | ||
| 14 | use namespace::autoclean; | ||
| 15 | |||
| 16 | has usbtmc => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'USB::TMC', | ||
| 19 | writer => '_usbtmc', | ||
| 20 | init_arg => undef, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has vid => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Str', | ||
| 26 | required => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has pid => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Str', | ||
| 32 | required => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has serial => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Str', | ||
| 38 | ); | ||
| 39 | |||
| 40 | has debug_mode => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => 'Bool', | ||
| 43 | default => 0, | ||
| 44 | ); | ||
| 45 | |||
| 46 | has write_termchar => ( | ||
| 47 | is => 'ro', | ||
| 48 | isa => 'Maybe[Str]', | ||
| 49 | default => "\n", | ||
| 50 | ); | ||
| 51 | |||
| 52 | has reset_device => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => 'Bool', | ||
| 55 | default => 1 | ||
| 56 | ); | ||
| 57 | |||
| 58 | sub BUILD { | ||
| 59 | my $self = shift; | ||
| 60 | my $serial = $self->serial(); | ||
| 61 | |||
| 62 | my $vid = $self->vid(); | ||
| 63 | my $pid = $self->pid(); | ||
| 64 | if ( $vid =~ /^0x/i ) { | ||
| 65 | $vid = hex($vid); | ||
| 66 | } | ||
| 67 | if ( $pid =~ /^0x/i ) { | ||
| 68 | $pid = hex($pid); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $usbtmc = USB::TMC->new( | ||
| 72 | vid => $vid, | ||
| 73 | pid => $pid, | ||
| 74 | defined($serial) ? ( serial => $serial ) : (), | ||
| 75 | debug_mode => $self->debug_mode(), | ||
| 76 | reset_device => $self->reset_device(), | ||
| 77 | ); | ||
| 78 | $self->_usbtmc($usbtmc); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub Write { | ||
| 82 | my ( $self, %args ) = validated_hash( | ||
| 83 | \@_, | ||
| 84 | timeout_param, | ||
| 85 | command => { isa => 'Str' }, | ||
| 86 | ); | ||
| 87 | |||
| 88 | my $write_termchar = $self->write_termchar() // ''; | ||
| 89 | my $command = $args{command} . $write_termchar; | ||
| 90 | my $timeout = $self->_timeout_arg(%args); | ||
| 91 | my $usbtmc = $self->usbtmc(); | ||
| 92 | $usbtmc->write( data => $command, timeout => $timeout ); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub Read { | ||
| 96 | my ( $self, %args ) = validated_hash( | ||
| 97 | \@_, | ||
| 98 | timeout_param(), | ||
| 99 | read_length_param(), | ||
| 100 | ); | ||
| 101 | my $timeout = $self->_timeout_arg(%args); | ||
| 102 | my $read_length = $self->_read_length_arg(%args); | ||
| 103 | |||
| 104 | my $usbtmc = $self->usbtmc(); | ||
| 105 | |||
| 106 | return $usbtmc->read( length => $read_length, timeout => $timeout ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub Clear { | ||
| 110 | my ( $self, %args ) = validated_hash( | ||
| 111 | \@_, timeout_param, | ||
| 112 | yoko => { isa => 'Bool', default => 0 } | ||
| 113 | ); | ||
| 114 | my $timeout = $self->_timeout_arg(%args); | ||
| 115 | my $is_yoko = delete $args{yoko}; | ||
| 116 | if ($is_yoko) { | ||
| 117 | $self->usbtmc()->clear_without_output_clear( timeout => $timeout ); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $self->usbtmc()->clear( timeout => $timeout ); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | with qw/ | ||
| 125 | Lab::Moose::Connection | ||
| 126 | /; | ||
| 127 | |||
| 128 | __PACKAGE__->meta->make_immutable(); | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | __END__ | ||
| 133 | |||
| 134 | =pod | ||
| 135 | |||
| 136 | =encoding UTF-8 | ||
| 137 | |||
| 138 | =head1 NAME | ||
| 139 | |||
| 140 | Lab::Moose::Connection::USB - Connection backend to USB Test & Measurement (USBTMC) bus | ||
| 141 | |||
| 142 | =head1 VERSION | ||
| 143 | |||
| 144 | version 3.881 | ||
| 145 | |||
| 146 | =head1 SYNOPSIS | ||
| 147 | |||
| 148 | use Lab::Moose; | ||
| 149 | |||
| 150 | my $instrument = instrument( | ||
| 151 | type => 'random_instrument', | ||
| 152 | connection_type => 'USB', | ||
| 153 | connection_options => { | ||
| 154 | vid => 0x0957, | ||
| 155 | pid => 0x0607, | ||
| 156 | serial => 'MY47000419', # only needed if vid/pid is ambiguous | ||
| 157 | } | ||
| 158 | ); | ||
| 159 | |||
| 160 | =head1 DESCRIPTION | ||
| 161 | |||
| 162 | Connection backend based on libusb via the L<USB::TMC> distribution. | ||
| 163 | |||
| 164 | =head1 COPYRIGHT AND LICENSE | ||
| 165 | |||
| 166 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 167 | |||
| 168 | Copyright 2017-2018 Simon Reinhardt | ||
| 169 | 2020 Andreas K. Huettel | ||
| 170 | |||
| 171 | |||
| 172 | This is free software; you can redistribute it and/or modify it under | ||
| 173 | the same terms as the Perl 5 programming language system itself. | ||
| 174 | |||
| 175 | =cut |