gpt4 book ai didi

ruby - 使用 Ruby 连接多个 IP Input 输出接口(interface),Netiom

转载 作者:行者123 更新时间:2023-12-03 12:06:42 25 4
gpt4 key购买 nike

我正在尝试创建一个用 ruby​​ 编写的应用程序,它通过 TCP/IP 与多个输入/输出接口(interface)进行通信。我使用的 IO 接口(interface)称为 NETIOM。该接口(interface)有 16 个数字输入和 16 个数字输出。

我如何:

  • 处理多个连接?我使用数组吗?
  • 通过单个连接发送消息然后查看其响应?
  • 触发输入时“做某事”?我需要能够识别触发它的输入和接口(interface)。

  • 此代码适用于一个 Netiom 设备,并且还能够检测被按下的输入,但代码会在一段时间后退出。我一直在尝试类和数组,但没有得到任何结果。
    require 'socket'
    server = TCPServer.open(3012)
    @socket = server.accept
    @socket.puts "SEND\r\n"
    while line = @socket.gets
    puts line.chop
    end

    此代码返回:

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



    由于客户以特定方式行事,我已经写了一些关于它如何工作的信息。还有一个指向 pdf 手册的链接(请看第 9 页)。 http://www.phaedrusltd.com/Download/NETIOM%20Instructions.pdf

    我很感激你能给我的任何帮助,非常感谢你的时间。

    When programming the interface you can specify the IP address and port of the server that you want it to communicate, this would be our ruby server. By default, the interface is attempting to connect to the server every 10 seconds until the server finally accepts. Once the server accepts the connection, it should send the message "SEND" and the interface will then return some data with the status of all the inputs and outputs, displayed below. If and when we wanted to close the connection we would send the message "CLOSE".

    The data that is returned:

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

    If one of the 16 inputs is pressed while there is a connection, it sends the same data as before but it changes that particular input to a '0'

    Digital inputs 1-8 : 01111111 // input 1 has been triggered
    Digital inputs 9-16: 11111111
    Digital outputs 1-8 : 00000000
    Digital outputs 9-16: 00000000

    When the button is released or let go the file is sent again but now the input is back to normal.

    Digital inputs 1-8 : 11111111 // input 1 is back to normal.
    Digital inputs 9-16: 11111111
    Digital outputs 1-8 : 00000000
    Digital outputs 9-16: 00000000

    If I want to turn output 8 on, I send A08, and to turn it off I send B08. I can toggle an output by using T08.

    最佳答案

    使用 Thread 处理多个连接,
    使用 TCP 服务器来管理 TCP,使用 NETIOM 管理器来处理您的
    设备。

    类似的东西(未经测试,这是我作品的副本/过去):

    require 'socket'
    require 'thread'
    require 'timeout'


    class TcpServer
    def initialize(iOServeur,port)
    @port=port
    @ioserver=iOServeur
    @th=[]
    @server = TCPServer.new('0.0.0.0', @port)
    @server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
    Thread.new do
    begin
    while (session = @server.accept)
    Thread.new(session) do |sess|
    begin
    @th[Thread.current]=Time.now
    ip=@session.peeraddr.last
    @ioserveur.connection(ip)
    request(ip,sess)
    ensure
    @ioserveur.deconnection(ip)
    sess.close rescue nil
    @th.delete(Thread.current)
    end
    end
    end
    rescue
    error($!.to_s)
    end
    end
    end
    def request(ip,sock)
    while (str=sock.read)
    ret=@ioServer.received(ip,str)
    if ret
    sock.write(ret) if String===ret && ret.length>0
    else
    sock.close raise nil
    return;
    end

    end
    end
    end

    class IoServer
    def initialize() end
    def connection(ip) end
    def deconnection(ip) end
    def received(ip,str) return "" end
    end

    Thread.abort_on_exception = true if $DEBUG
    BasicSocket.do_not_reverse_lookup = true
    ios=IoServer.new
    TcpServer.new(ios,8080)
    gets # !!

    关于ruby - 使用 Ruby 连接多个 IP Input 输出接口(interface),Netiom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5066943/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com