| 1 | package Lab::Connection::VISA_GPIB; | ||
| 2 | #ABSTRACT: GPIB-type connection class which uses NI VISA (L<Lab::VISA>) as backend | ||
| 3 | $Lab::Connection::VISA_GPIB::VERSION = '3.881'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::VISA; | ||
| 8 | use Lab::Bus::VISA; | ||
| 9 | use Lab::Connection::GPIB; | ||
| 10 | use Lab::Exception; | ||
| 11 | |||
| 12 | our @ISA = ("Lab::Connection::GPIB"); | ||
| 13 | |||
| 14 | our %fields = ( | ||
| 15 | bus_class => 'Lab::Bus::VISA', | ||
| 16 | resource_name => undef, | ||
| 17 | wait_status => 0, # sec; | ||
| 18 | wait_query => 10e-6, # sec; | ||
| 19 | read_length => 1000, # bytes | ||
| 20 | gpib_board => 0, | ||
| 21 | gpib_address => 1, | ||
| 22 | timeout => 2, | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $proto = shift; | ||
| 27 | my $class = ref($proto) || $proto; | ||
| 28 | my $twin = undef; | ||
| 29 | my $self = $class->SUPER::new(@_) | ||
| 30 | ; # getting fields and _permitted from parent class, parameter checks | ||
| 31 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | # | ||
| 37 | # Translating from plain GPIB-driverish to VISAslang | ||
| 38 | # | ||
| 39 | |||
| 40 | # | ||
| 41 | # adapting bus setup to VISA | ||
| 42 | # | ||
| 43 | sub _setbus { | ||
| 44 | my $self = shift; | ||
| 45 | my $bus_class = $self->bus_class(); | ||
| 46 | |||
| 47 | no strict 'refs'; | ||
| 48 | $self->bus( $bus_class->new( $self->config() ) ) | ||
| 49 | || Lab::Exception::Error->throw( | ||
| 50 | error => "Failed to create bus $bus_class in " | ||
| 51 | . __PACKAGE__ | ||
| 52 | . "::_setbus.\n" ); | ||
| 53 | use strict; | ||
| 54 | |||
| 55 | # | ||
| 56 | # build VISA resource name | ||
| 57 | # | ||
| 58 | my $resource_name | ||
| 59 | = 'GPIB' . $self->gpib_board() . '::' . $self->gpib_address(); | ||
| 60 | $resource_name .= '::' . $self->gpib_saddress() | ||
| 61 | if defined $self->gpib_saddress(); | ||
| 62 | $resource_name .= '::INSTR'; | ||
| 63 | $self->resource_name($resource_name); | ||
| 64 | $self->config()->{'resource_name'} = $resource_name; | ||
| 65 | |||
| 66 | # again, pass it all. | ||
| 67 | $self->connection_handle( | ||
| 68 | $self->bus()->connection_new( $self->config() ) ); | ||
| 69 | |||
| 70 | return $self->bus(); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _configurebus { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | # | ||
| 77 | # set VISA Attributes: | ||
| 78 | # | ||
| 79 | |||
| 80 | # termination character | ||
| 81 | if ( defined $self->config()->{termchar} ) { | ||
| 82 | if ( $self->config()->{termchar} =~ m/^[0-9]+$/ ) { | ||
| 83 | $self->config()->{termchar} = chr( $self->config()->{termchar} ); | ||
| 84 | } | ||
| 85 | $self->bus()->set_visa_attribute( | ||
| 86 | $self->connection_handle(), | ||
| 87 | $Lab::VISA::VI_ATTR_TERMCHAR_EN, | ||
| 88 | $Lab::VISA::VI_TRUE | ||
| 89 | ); | ||
| 90 | $self->bus()->set_visa_attribute( | ||
| 91 | $self->connection_handle(), | ||
| 92 | $Lab::VISA::VI_ATTR_TERMCHAR, ord( $self->config()->{termchar} ) | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $self->bus()->set_visa_attribute( | ||
| 97 | $self->connection_handle(), | ||
| 98 | $Lab::VISA::VI_ATTR_TERMCHAR_EN, | ||
| 99 | $Lab::VISA::VI_FALSE | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | # read timeout | ||
| 104 | $self->bus()->set_visa_attribute( | ||
| 105 | $self->connection_handle(), | ||
| 106 | $Lab::VISA::VI_ATTR_TMO_VALUE, $self->config()->{timeout} * 1e3 | ||
| 107 | ); | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | # | ||
| 114 | # Read,Write,Query are OK in the version from Lab::Connection | ||
| 115 | # | ||
| 116 | |||
| 117 | # $self->connection_handle() is the VISA resource handle | ||
| 118 | |||
| 119 | sub EnableTermChar { # 0/1 off/on | ||
| 120 | my $self = shift; | ||
| 121 | my $enable = shift; | ||
| 122 | my $result; | ||
| 123 | |||
| 124 | # print "e/d\n"; | ||
| 125 | if ( $enable == 1 ) { | ||
| 126 | |||
| 127 | # print "enable "; | ||
| 128 | $result = Lab::VISA::viSetAttribute( | ||
| 129 | $self->connection_handle(), | ||
| 130 | $Lab::VISA::VI_ATTR_TERMCHAR_EN, | ||
| 131 | $Lab::VISA::VI_TRUE | ||
| 132 | ); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | # print "disable "; | ||
| 136 | $result = Lab::VISA::viSetAttribute( | ||
| 137 | $self->connection_handle(), | ||
| 138 | $Lab::VISA::VI_ATTR_TERMCHAR_EN, | ||
| 139 | $Lab::VISA::VI_FALSE | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | # print "result: $result\n"; | ||
| 144 | return $result; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub SetTermChar { # the character as string | ||
| 148 | my $self = shift; | ||
| 149 | my $termchar = shift; | ||
| 150 | |||
| 151 | # print "char\n"; | ||
| 152 | my $result = Lab::VISA::viSetAttribute( | ||
| 153 | $self->connection_handle(), | ||
| 154 | $Lab::VISA::VI_ATTR_TERMCHAR, ord($termchar) | ||
| 155 | ); | ||
| 156 | |||
| 157 | # print "result: $result\n"; | ||
| 158 | return $result; | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | __END__ | ||
| 165 | |||
| 166 | =pod | ||
| 167 | |||
| 168 | =encoding utf-8 | ||
| 169 | |||
| 170 | =head1 NAME | ||
| 171 | |||
| 172 | Lab::Connection::VISA_GPIB - GPIB-type connection class which uses NI VISA (L<Lab::VISA>) as backend | ||
| 173 | |||
| 174 | =head1 VERSION | ||
| 175 | |||
| 176 | version 3.881 | ||
| 177 | |||
| 178 | =head1 SYNOPSIS | ||
| 179 | |||
| 180 | This GPIB Connection class for Lab::Bus::VISA implements a GPIB-Standard | ||
| 181 | connection on top of VISA (translates GPIB parameters to VISA resource names, | ||
| 182 | mostly, to be exchangeable with other GPIB connections. | ||
| 183 | |||
| 184 | This class is not called directly. To make a GPIB suppporting instrument use | ||
| 185 | Lab::Connection::VISA_GPIB, set the connection_type parameter accordingly: | ||
| 186 | |||
| 187 | $instrument = new HP34401A( | ||
| 188 | connection_type => 'VISA_GPIB', | ||
| 189 | gpib_board => 0, | ||
| 190 | gpib_address => 14 | ||
| 191 | ) | ||
| 192 | |||
| 193 | =head1 DESCRIPTION | ||
| 194 | |||
| 195 | C<Lab::Connection::VISA_GPIB> provides a GPIB-type connection with L<Lab::Bus::VISA> using | ||
| 196 | NI VISA (L<Lab::VISA>) as backend. | ||
| 197 | |||
| 198 | It inherits from L<Lab::Connection::GPIB> and subsequently from L<Lab::Connection>. | ||
| 199 | |||
| 200 | The main feature is to assemble the standard gpib connection options | ||
| 201 | gpib_board | ||
| 202 | gpib_address | ||
| 203 | gpib_saddress | ||
| 204 | into a valid NI VISA resource name (see L<Lab::Connection::VISA> for more details). | ||
| 205 | |||
| 206 | =head1 CONSTRUCTOR | ||
| 207 | |||
| 208 | =head2 new | ||
| 209 | |||
| 210 | my $connection = new Lab::Connection::VISA_GPIB( | ||
| 211 | gpib_board => 0, | ||
| 212 | gpib_address => $address, | ||
| 213 | gpib_saddress => $secondary_address | ||
| 214 | } | ||
| 215 | |||
| 216 | =head1 METHODS | ||
| 217 | |||
| 218 | This just falls back on the methods inherited from L<Lab::Connection>. | ||
| 219 | |||
| 220 | =head2 config | ||
| 221 | |||
| 222 | Provides unified access to the fields in initial @_ to all the child classes. | ||
| 223 | E.g. | ||
| 224 | |||
| 225 | $GPIB_Address=$instrument->Config(gpib_address); | ||
| 226 | |||
| 227 | Without arguments, returns a reference to the complete $self->Config aka @_ of the constructor. | ||
| 228 | |||
| 229 | $Config = $connection->Config(); | ||
| 230 | $GPIB_Address = $connection->Config()->{'gpib_address'}; | ||
| 231 | |||
| 232 | =head1 TO DO | ||
| 233 | |||
| 234 | Access to GPIB VISA attributes, device clear, ... | ||
| 235 | |||
| 236 | =head1 CAVEATS/BUGS | ||
| 237 | |||
| 238 | Probably few. Mostly because there's not a lot to be done here. Please report. | ||
| 239 | |||
| 240 | =head1 SEE ALSO | ||
| 241 | |||
| 242 | =over 4 | ||
| 243 | |||
| 244 | =item * L<Lab::Connection> | ||
| 245 | |||
| 246 | =item * L<Lab::Connection::GPIB> | ||
| 247 | |||
| 248 | =item * L<Lab::Connection::VISA> | ||
| 249 | |||
| 250 | =back | ||
| 251 | |||
| 252 | =head1 COPYRIGHT AND LICENSE | ||
| 253 | |||
| 254 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 255 | |||
| 256 | Copyright 2011 Andreas K. Huettel, David Kalok, Florian Olbrich | ||
| 257 | 2012 Florian Olbrich, Stefan Geissler | ||
| 258 | 2013 Andreas K. Huettel | ||
| 259 | 2016 Simon Reinhardt | ||
| 260 | 2017 Andreas K. Huettel | ||
| 261 | 2020 Andreas K. Huettel | ||
| 262 | |||
| 263 | |||
| 264 | This is free software; you can redistribute it and/or modify it under | ||
| 265 | the same terms as the Perl 5 programming language system itself. | ||
| 266 | |||
| 267 | =cut |