In article <20011219171405.E9A281C2 / helium.ruby-lang.org>, ned@bike-
nomad.com says...


Thanks
I have already found quite good library at Japanese. 
But nevertheless it would be desirable to receive the answer to these 
questions.


> Whether I can use here for this purpose IOCTL? 
> If not it is difficult, write an example of use of function IOCTL c 
> FIONREAD and TIOCMGET in parameters. 


Somebody did it?

----------------------------------------------------------------------
#!/usr/local/bin/ruby

require "termios"

class Comport

  def initialize(port)
    @dev = open(port,File::RDWR | File::NOCTTY)
    @speed = 9600
    @parity = "N"
    @numbit = 8
    @stopbit = 1
    @timeout = 5

    @oldatt = Termios::getattr(@dev)
    @newatt = Termios::new_termios()
    @newatt.c_cflag = (Termios::B9600 | Termios::CRTSCTS | Termios::CS8 
| Termios::CLOCAL | Termios::CREAD)
    @newatt.c_iflag = Termios::IGNPAR
    @newatt.c_oflag = 0
    @newatt.c_lflag = 0
    @newatt.c_ispeed = @newatt.c_ospeed = Termios::B9600
    @newatt.c_cc[Termios::VTIME] = @timeout * 10 # timeout = 5 sec
    @newatt.c_cc[Termios::VMIN] = 0  # nobuffer
    
    Termios::flush(@dev, Termios::TCIFLUSH)
    Termios::setattr(@dev, Termios::TCSANOW, @newatt)
  end

  def speed(n=nil)
    return @speed if n == nil
    @newatt.c_ispeed = @newatt.c_ospeed = eval("Termios::" + "B" + 
n.to_s)
    
    Termios::flush(@dev, Termios::TCIFLUSH)
    Termios::setattr(@dev, Termios::TCSANOW, @newatt)
    @speed = n
  end

  def parity(p=nil)
    return @parity if p == nil
  end

  def bitlen(l=nil)
    return @numbit if l == nil
  end

  def stopbit(s=nil)
    return @stopbit if s == nil
  end

  def timeout(n=nil)
    return @timeout if n == nil
    @newatt.c_cc[Termios::VTIME] = n * 10 # timeout = n sec
    
    Termios::flush(@dev, Termios::TCIFLUSH)
    Termios::setattr(@dev, Termios::TCSANOW, @newatt)
    @timeout = n
  end

  def putc(c)
    @dev.putc(c)
  end

  def getc
    @dev.getc
  end

  def read(cnt=1000 ,termchar=nil)
    (termchar = cnt ; cnt = 0) if cnt.type == String
    r = ""
    begin
      n = @dev.getc
      return r if n == nil # may be time out
      r << n.chr 
      return r if r[-1,1] == termchar
    end while (cnt -= 1) != 0
    r
  end

  def write(s)
    cnt = 0
    s.each_byte {|c| @dev.putc c.chr ; cnt += 1}
    cnt
  end

  def close
    Termios::flush(@dev, Termios::TCIFLUSH)
    Termios::setattr(@dev, Termios::TCSANOW, @oldatt)
    @dev.close
  end
end

if __FILE__ == $0
  dev = Comport.new(if /WIN/oi =~ RUBY_PLATFORM then "/dev/com1" else 
"/dev/ttyS0" end)
  dev.write("*idn?\n")
  print dev.read("\n")
end
----------------------------------------------------------------------



Many thanks