| 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.899'; | ||
| 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__ |