Sha256: 9a3c0f79af0b84c549e92b5cda7959cabbeec1540fe19278eeb227ee5671577a

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
module Lxi
  class Device
    include FFI

    attr_accessor :id, :address, :port, :name, :timeout, :protocol

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

      connect

      yield self if block_given?
    end

    def connect
      Lxi.init_lxi_session

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

      true
    end
    alias open connect

    def disconnect
      Lxi.lxi_disconnect(@id)
    end
    alias close disconnect

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

      bytes_sent
    end
    alias scpi send
    alias write send

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

      message.read_string
    end
    alias gets read

    def query(message)
      send message
      read 10_000
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lxi_rb-0.7.0 lib/lxi/device.rb
lxi_rb-0.6.4 lib/lxi/device.rb
lxi_rb-0.6.3 lib/lxi/device.rb
lxi_rb-0.4.9 lib/lxi/device.rb
lxi_rb-0.4.8 lib/lxi/device.rb
lxi_rb-0.4.6 lib/lxi/device.rb
lxi_rb-0.4.1 lib/lxi/device.rb
lxi_rb-0.4.0 lib/lxi/device.rb