Francis Cianfrocca wrote:
> Asterix Gallier wrote:
>> 
>> Your solution by using a file descriptor sounds very interesting. I've 
>> searched but didn't find any adequate example or documentation beside 
>> the ruby source itself.
>> 
>> Can you please give me any support for this approach.
>> 
>> As far as i can see, i need to simulate a file filedescriptor to ruby. 
>> Am I right? I've got no idea how to do this. I take a look at the 
>> ruby-serialport source but i think that this is not applicable to my 
>> situation or?
> 
> Asterix, I don't have time right this moment to write and test a working 
> code sample, but you can try using Ruby's IO.pipe to get a pair of 
> descriptors connected to each other.
> 
> rd,wr = IO.pipe
> writeable_descriptor = wr.fileno
> 
> Now your Windows thread can write the writeable_descriptor and your Ruby 
> thread can select([rd]).
> 
> Hope that helps. There are probably pitfalls with this approach on 
> Windows. I know I've done this before or something like it, but will 
> need a bit of time to find the code.

Ok I guess I know how it should work, but I'm not able to implement.

For testing I tried the sample code below.

I experienced following problems:
* the select statement returns immediately weather there is data or not
* if count is higher than one, the skript blocks because of the wrong 
behavior
  of select
* how to write data to the IO Object (wr) with C functions.

Thanks for any help
Asterix

////////////////////////////////////////////////////////
// rubycode

rd, wr = IO.pipe
count = 3

a = Thread.new {
  count.times do
    puts "#{__LINE__} Waiting for data: ..."
    ra, wa, ea = select([rd], nil, nil, nil)
    puts __LINE__
    p ra[0].read
    puts __LINE__
  end
  rd.close
}

b = Thread.new {
  count.times do
    puts "#{__LINE__} Sending message..."
    wr.write("Hello")
    puts __LINE__
  end
  wr.close
}
puts __LINE__

[a, b].each {|t| t.join if t != nil}

# Output when Count = 1
>ruby test.rb
6 Waiting for data: ...
17 Sending message...
8
23
19
"Hello"
10
>Exit code: 0

# Output when Count = 2 --> blocks
>ruby test.rb
6 Waiting for data: ...
17 Sending message...
8
23
19
17 Sending message...
>Exit code: -1073741510

# Output when Count = 2 and "read" replaced with "inspect"
>ruby test.rb
6 Waiting for data: ...
17 Sending message...
8
"#<IO:0x282385c>"
23
10
6 Waiting for data: ...
19
17 Sending message...
8
"#<IO:0x282385c>"
10
test_2.rb:18:in `write': Invalid argument (Errno::EINVAL)
	from test_2.rb:25:in `join'
	from test_2.rb:25
	from test_2.rb:25
>Exit code: 1

-- 
Posted via http://www.ruby-forum.com/.