Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Serial port programming

Reply
Thread Tools

Serial port programming

 
 
Dan Caugherty
Guest
Posts: n/a
 
      08-21-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulate a serial port in windows (create a virtual 'com' port) Pom Python 2 01-31-2007 07:49 PM
Serial Port programming - Reading DSR from port msalerno Perl Misc 3 07-14-2005 12:58 PM
Can I connect router Serial interface directly to a PC serial port? Faustino Dina Cisco 2 08-18-2004 02:30 AM
Re: Serial port and PS/2 port schematics OR Assistive Tech. suggestion naive.verizon@locality.net Computer Support 1 07-10-2003 11:46 AM
Re: Serial port and PS/2 port schematics °Mike° Computer Support 1 07-09-2003 10:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57