Class: Lxi::Device

Inherits:
Object
  • Object
show all
Includes:
FFI
Defined in:
lib/lxi/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, protocol = :vxi11) {|_self| ... } ⇒ Device

Returns a new instance of Device.

Yields:

  • (_self)

Yield Parameters:

  • _self (Lxi::Device)

    the object that the method was called on



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lxi/device.rb', line 9

def initialize(address, protocol = :vxi11)
  @address = address
  @port = 0
  @name = nil
  @timeout = 1000
  @protocol = protocol
  @id = -1

  connect

  yield(self) if block_given?
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



7
8
9
# File 'lib/lxi/device.rb', line 7

def address
  @address
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/lxi/device.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/lxi/device.rb', line 7

def name
  @name
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/lxi/device.rb', line 7

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



7
8
9
# File 'lib/lxi/device.rb', line 7

def protocol
  @protocol
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/lxi/device.rb', line 7

def timeout
  @timeout
end

Instance Method Details

#connectObject Also known as: open

Raises:



22
23
24
25
26
27
28
29
# File 'lib/lxi/device.rb', line 22

def connect
  Lxi.init_session

  @id = Lxi.connect(@address, @port, @name, @timeout, @protocol)
  raise(Error, 'LXI Connection Error') if @id == LXI_ERROR

  true
end

#disconnectObject Also known as: close



32
33
34
# File 'lib/lxi/device.rb', line 32

def disconnect
  Lxi.disconnect(@id)
end

#query(message, bytes = 512, resp_delay: 0.02) ⇒ Object



55
56
57
58
59
# File 'lib/lxi/device.rb', line 55

def query(message, bytes = 512, resp_delay: 0.02)
  write(message)
  sleep :resp_delay
  read(bytes)
end

#read(length) ⇒ Object Also known as: gets

Raises:



46
47
48
49
50
51
52
# File 'lib/lxi/device.rb', line 46

def read(length)
  message = FFI::MemoryPointer.new(:char, length)
  bytes_received = Lxi.receive(@id, message, length, @timeout)
  raise(Error, 'LXI communications error') unless bytes_received.positive?

  message.read_string
end

#write(message) ⇒ Object Also known as: scpi, send

Raises:



37
38
39
40
41
42
# File 'lib/lxi/device.rb', line 37

def write(message)
  bytes_sent = Lxi.__send(@id, message, message.length, @timeout)
  raise(Error, 'LXI communications error') unless bytes_sent.positive?

  bytes_sent
end