| 1 | package Lab::Bus::IsoBus; | ||
| 2 | #ABSTRACT: Oxford Instruments ISOBUS bus | ||
| 3 | $Lab::Bus::IsoBus::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Lab::Connection; | ||
| 8 | use Scalar::Util qw(weaken); | ||
| 9 | use Time::HiRes qw (usleep sleep); | ||
| 10 | use Lab::Bus; | ||
| 11 | use Data::Dumper; | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | our @ISA = ("Lab::Bus"); | ||
| 15 | |||
| 16 | our %fields = ( | ||
| 17 | type => 'IsoBus', | ||
| 18 | base_connection => undef, | ||
| 19 | IsoEnableTermChar => 1, | ||
| 20 | IsoTermChar => "\r", | ||
| 21 | brutal => 0, # brutal as default? | ||
| 22 | wait_status => 10e-6, # sec; | ||
| 23 | wait_query => 10e-6, # sec; | ||
| 24 | query_length => 300, # bytes | ||
| 25 | query_long_length => 10240, #bytes | ||
| 26 | read_length => 1000, # bytes | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my $proto = shift; | ||
| 31 | my $class = ref($proto) || $proto; | ||
| 32 | my $twin = undef; | ||
| 33 | my $self = $class->SUPER::new(@_) | ||
| 34 | ; # getting fields and _permitted from parent class | ||
| 35 | $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__); | ||
| 36 | |||
| 37 | # search for twin in %Lab::Bus::BusList. If there's none, place $self there and weaken it. | ||
| 38 | if ( $class eq __PACKAGE__ ) | ||
| 39 | { # careful - do only if this is not a parent class constructor | ||
| 40 | if ( $twin = $self->_search_twin() ) { | ||
| 41 | undef $self; | ||
| 42 | return $twin; # ...and that's it. | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | # TODO implement twin detection | ||
| 46 | $Lab::Bus::BusList{ $self->type() }->{'default'} = $self; | ||
| 47 | weaken( $Lab::Bus::BusList{ $self->type() }->{'default'} ); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | # set the connection $self->base_connection to the parameters required by IsoBus | ||
| 52 | $self->base_connection( $self->config('base_connection') ) | ||
| 53 | if defined $self->config('base_connection'); | ||
| 54 | $self->IsoEnableTermChar( $self->config('IsoEnableTermChar') ) | ||
| 55 | if defined $self->config('IsoEnableTermChar'); | ||
| 56 | $self->IsoTermChar( $self->config('IsoTermChar') ) | ||
| 57 | if defined $self->config('IsoTermChar'); | ||
| 58 | |||
| 59 | # clear the connection if possible | ||
| 60 | |||
| 61 | # we need to set the following RS232 options: 9600baud, 8 data bits, 1 stop bit, no parity, no flow control | ||
| 62 | # what is the read terminator? we assume CR=13 here, but this is not set in stone | ||
| 63 | # write terminator should I think always be CR=13=0x0d | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub connection_new { # @_ = ({ isobus_address => $isobus_address }) | ||
| 69 | my $self = shift; | ||
| 70 | my $args = undef; | ||
| 71 | my $status = undef; | ||
| 72 | my $connection_handle = undef; | ||
| 73 | if ( ref $_[0] eq 'HASH' ) { | ||
| 74 | $args = shift; | ||
| 75 | } # try to be flexible about options as hash/hashref | ||
| 76 | else { $args = {@_} } | ||
| 77 | |||
| 78 | my $isobus_address = $args->{'isobus_address'}; | ||
| 79 | |||
| 80 | # check that this is a number and in the valid range | ||
| 81 | |||
| 82 | # we dont actually have to open anything here | ||
| 83 | |||
| 84 | # we abuse the isobus_address as connection handle | ||
| 85 | return $isobus_address; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub connection_read | ||
| 89 | { # @_ = ( $connection_handle, $args = { read_length, brutal } | ||
| 90 | my $self = shift; | ||
| 91 | my $connection_handle = shift; | ||
| 92 | my $args = undef; | ||
| 93 | if ( ref $_[0] eq 'HASH' ) { | ||
| 94 | $args = shift; | ||
| 95 | } # try to be flexible about options as hash/hashref | ||
| 96 | else { $args = {@_} } | ||
| 97 | |||
| 98 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 99 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 100 | my $result = undef; | ||
| 101 | |||
| 102 | $result = $self->config('base_connection')->Read( | ||
| 103 | { | ||
| 104 | brutal => $brutal, | ||
| 105 | read_length => $read_length, | ||
| 106 | } | ||
| 107 | ); | ||
| 108 | |||
| 109 | return $result; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub connection_write | ||
| 113 | { # @_ = ( $connection_handle, $args = { command, wait_status } | ||
| 114 | my $self = shift; | ||
| 115 | my $connection_handle = shift; | ||
| 116 | my $args = undef; | ||
| 117 | if ( ref $_[0] eq 'HASH' ) { | ||
| 118 | $args = shift; | ||
| 119 | } # try to be flexible about options as hash/hashref | ||
| 120 | else { $args = {@_} } | ||
| 121 | |||
| 122 | my $command = $args->{'command'} || undef; | ||
| 123 | |||
| 124 | my $write_cnt = 0; | ||
| 125 | |||
| 126 | if ( !defined $command ) { | ||
| 127 | Lab::Exception::CorruptParameter->throw( | ||
| 128 | error => "No command given to " | ||
| 129 | . __PACKAGE__ | ||
| 130 | . "::connection_write().\n", ); | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | if ( $self->config("IsoEnableTermChar") ) { | ||
| 134 | $write_cnt = $self->base_connection->Write( | ||
| 135 | { | ||
| 136 | # build the format for an IsoBus command | ||
| 137 | command => sprintf( | ||
| 138 | "@%d%s%s", | ||
| 139 | $connection_handle, $command, $self->IsoTermChar() | ||
| 140 | ), | ||
| 141 | } | ||
| 142 | ); | ||
| 143 | |||
| 144 | } | ||
| 145 | else { | ||
| 146 | $write_cnt = $self->base_connection->Write( | ||
| 147 | { | ||
| 148 | # build the format for an IsoBus command | ||
| 149 | command => | ||
| 150 | sprintf( "@%d%s", $connection_handle, $command ) | ||
| 151 | } | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | return $write_cnt; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | sub connection_query | ||
| 159 | { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal } | ||
| 160 | my $self = shift; | ||
| 161 | my $connection_handle = shift; | ||
| 162 | my $args = undef; | ||
| 163 | if ( ref $_[0] eq 'HASH' ) { | ||
| 164 | $args = shift; | ||
| 165 | } # try to be flexible about options as hash/hashref | ||
| 166 | else { $args = {@_} } | ||
| 167 | |||
| 168 | my $command = $args->{'command'} || undef; | ||
| 169 | my $brutal = $args->{'brutal'} || $self->brutal(); | ||
| 170 | my $read_length = $args->{'read_length'} || $self->read_length(); | ||
| 171 | my $wait_status = $args->{'wait_status'} || $self->wait_status(); | ||
| 172 | my $wait_query = $args->{'wait_query'} || $self->wait_query(); | ||
| 173 | |||
| 174 | my $result = undef; | ||
| 175 | my $status = undef; | ||
| 176 | my $write_cnt = 0; | ||
| 177 | my $read_cnt = undef; | ||
| 178 | |||
| 179 | $write_cnt = $self->connection_write($args); | ||
| 180 | |||
| 181 | usleep($wait_query) | ||
| 182 | ; #<---ensures that asked data presented from the device | ||
| 183 | |||
| 184 | $result = $self->connection_read($args); | ||
| 185 | return $result; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub connection_clear { | ||
| 189 | my $self = shift; | ||
| 190 | |||
| 191 | $self->config('base_connection')->Clear(); | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | # | ||
| 196 | # search and return an instance of the same type in %Lab::Bus::BusList | ||
| 197 | # | ||
| 198 | sub _search_twin { | ||
| 199 | my $self = shift; | ||
| 200 | |||
| 201 | # Only one VISA bus for the moment, stored as "default" | ||
| 202 | if ( !$self->ignore_twins() ) { | ||
| 203 | |||
| 204 | # if(defined $Lab::Bus::BusList{$self->type()}->{'default'}) { | ||
| 205 | # return $Lab::Bus::BusList{$self->type()}->{'default'}; | ||
| 206 | # } | ||
| 207 | } | ||
| 208 | |||
| 209 | return undef; | ||
| 210 | } | ||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | |||
| 215 | 1; | ||
| 216 | |||
| 217 | __END__ |