Serial port programming

Discussion in 'Ruby' started by Dan Caugherty, Aug 21, 2007.

  1. Hey all --

    I'm trying to find what seems to be the holy grail of Ruby
    programmers: a portable platform-neutral way to access serial ports.
    As you'd expect Windows is the most vexing platform to deal with.

    I *thought* I could get away with something like the following under
    Cygwin (note the default string for opening a Windows serial port).
    I'm perfectly happy with opening a port and kicking off a thread to
    keep reading data into a buffer every few milliseconds:

    def initialize(filename = "COM1:9600,8,N,1")
    # die horribly if we can't open file for read/write/append

    @mon = Monitor.new
    @buf = ""
    # kick off our collector thread
    @collector = Thread.new(filename) do |fname|
    begin
    if (! (@hdl = open(filename, "w+")))
    raise "Cannot open for IO"
    end

    loop do
    if (select([@hdl],nil,nil,0.01))
    if newchars = @hdl.read
    @mon.synchronize do
    if (0 < newchars.length) then
    # stick new data into a buffer, let the read method
    of the instance read the buffer instead
    @buf << newchars
    newchars = nil
    end
    end
    end
    end
    end
    rescue
    $stderr.print "#{filename}: IO failed: " + $!
    return
    end
    @hdl.close
    end

    # needed for Cygwin at least
    @collector.wakeup if (@collector && @collector.alive?)
    end

    --
    The big problem is that even though the select call returns with a
    return value indicating that data is waiting, the read call hangs.
    Using Fcntl to set the IO instance to use non-blocking IO solves
    nothing.

    Am I stuck with using the ruby-serialport extension? (And why isn't it
    a gem by now anyway? Grr..)


    Thanks in advance,
    -- Dan
     
    Dan Caugherty, Aug 21, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Hatfield

    Serial Port programming

    Jim Hatfield, Jun 15, 2004, in forum: Java
    Replies:
    3
    Views:
    719
    Dale King
    Apr 15, 2006
  2. rfid
    Replies:
    0
    Views:
    5,104
  3. crypto

    Serial Port Programming

    crypto, May 16, 2005, in forum: Java
    Replies:
    3
    Views:
    1,900
    Dale King
    May 18, 2005
  4. Pom
    Replies:
    2
    Views:
    1,678
    Bas-i
    Jan 31, 2007
  5. msalerno
    Replies:
    3
    Views:
    413
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page