Using Ruby to connect with multiple IP Input output interface,Netiom.

N

Nick Jackson

Hello all,

I am trying to create an application written in ruby which communicates
with multiple input/output interfaces over TCP/IP. The IO interface I am
using is called a NETIOM. The interface has 16 digital inputs and 16
digital outputs.

This code works for one Netiom device and is also able to detect inputs
being pressed but the code exits after a little while. I have been
experimenting with classes and arrays but not getting anywhere.

require 'socket'
server = TCPServer.open(3012)
@socket = server.accept
@socket.puts "SEND\r\n"
while line = @socket.gets
puts line.chop
end

this code returns:

Digital inputs 1-8 : 11111111
Digital inputs 9-16: 11111111
Digital outputs 1-8 : 00000000
Digital outputs 9-16: 00000000


It would be great if I can have some help with the following:
handling multiple interfaces
being able to send a message to a single interface and then see its response
'doing something' when an input is triggered and being able to
identify which input and interface it was triggered on.

Take a look at this for more info.
http://stackoverflow.com/questions/5066943

I appreciate any help you can give me and many thanks for your time.

Nick.
 
R

Robert Klemme

I am trying to create an application written in ruby which communicates
with multiple input/output interfaces over TCP/IP. The IO interface I am
using is called a NETIOM. The interface has 16 digital inputs and 16
digital outputs.

For us it is more important to know the protocol that this device
speaks (I mean the _network_ protocol which you are trying to
utilize).
This code works for one Netiom device and is also able to detect inputs
being pressed but the code exits after a little while. I have been
experimenting with classes and arrays but not getting anywhere.

require 'socket'
server =3D TCPServer.open(3012)
@socket =3D server.accept
@socket.puts "SEND\r\n"

This should be @socket.write "SEND\r\n".
while line =3D @socket.gets

If your line terminator is "\r\n" then you probably also want to use

TERM =3D "\r\n".freeze

while line =3D @socket.gets TERM
=A0puts line.chop

I'd rather use

line.chomp! TERM
end

this code returns:

Digital inputs 1-8 : 11111111
Digital inputs 9-16: 11111111
Digital outputs 1-8 : 00000000
Digital outputs 9-16: 00000000


It would be great if I can have some help with the following:
=A0 > handling multiple interfaces

Not sure what that means. Do you want multiple clients to connect?
=A0 > being able to send a message to a single interface and then see its
response

You may want to look at IO#expect ("ri19 IO#expect").
=A0 > 'doing something' when an input is triggered and being able to
identify which input and interface it was triggered on.

Take a look at this for more info.
http://stackoverflow.com/questions/5066943

I'd start by creating a handler class per connection. This could look
like this:

require 'socket'

class DeviceHandler
TERM =3D "\r\n".freeze

def initialize(socket)
@socket =3D socket
@Thread =3D Thread.new do
@socket.each_line TERM do |line|
data =3D parse(line)
end
end
end

# more methods...

private
def send(...)
@socket.write(... + TERM)
end
end

server =3D TCPServer.open(3012)
handlers =3D []

while (client =3D server.accept)
handlers << DeviceHandler.new(client)
end

But how this is really set up (and especially if you need to start a
thread in the constructor) hugely depends on the protocol spoken by
the device and what you want to do with it.

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Brian Candler

Nick Jackson wrote in post #982927:
require 'socket'
server = TCPServer.open(3012)
@socket = server.accept

If this is a server waiting for incoming connections from the device (as
it appears to be), you can accept multiple connections and handle each
one in its own Thread. There's code in the standard library which does
that for you: see gserver.rb

Not that it's hard to do in straight Ruby:

while s = server.accept
Thread.new(s) do |socket|
... do stuff with socket
end
end
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top