Sha256: 0ff895629afba1e677bdbf3556b5af609330708f69a6da7d2379019427891fb8

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'timeout'

module PrologixGpib::Lan
  class Error < StandardError
  end

  DEVICE_PORT = 1234
  EOL = "\r\n".freeze

  def initialize(ip, mode: :controller, address: 9)
    open_socket(ip)

    # open_serial_port(paths)
    # flush
    # self.mode = mode
    # self.address = address
    # self.auto = :disable
    # self.eos = 0

    yield self if block_given?
  end

  def write(command)
    return unless connected?

    @socket.send "#{command}#{EOL}", 0
    sleep 0.1
  end

  def read
    return unless connected?

    @socket.gets.chomp
  end

  private

  def open_socket(ip)
    @socket = TCPSocket.new ip, DEVICE_PORT
    write('++ver')
    return if getline.include? 'Prologix'

    raise Error, 'No Prologix LAN controllers found.'
  end

  def connected?
    raise Error, 'ConnectionError: No open Prologix device connections.' if @socket.nil?

    true
  end

  def readline
    return unless connected?

    t = Timeout.timeout(1, Timeout::Error, "No response from device at #{@socket.peeraddr[3]}") { getline }
  end

  # This method will block until the EOL terminator is received
  # The lower level gets method is pure ruby, so can be safely used with Timeout.
  def getline
    return unless connected?

    @socket.gets(EOL).chomp
  end

  def device_query(command)
    write(command)
    readline
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prologix_gpib-0.5.3 lib/prologix_gpib/lan.rb
prologix_gpib-0.5.2 lib/prologix_gpib/lan.rb