Beispiel #1
0
def io_loop_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
                   source_address=None, timeout=None, **kwargs):
    """
    Factory to create Tornado based asynchronous udp clients
    :param host: Host IP address
    :param port: Port
    :param framer: Modbus Framer
    :param source_address: Bind address
    :param timeout: Timeout in seconds
    :param kwargs:
    :return: event_loop_thread and tornado future
    """
    from tornado.ioloop import IOLoop
    from pymodbus.client.asynchronous.tornado import AsyncModbusUDPClient as \
        Client

    client = Client(host=host, port=port, framer=framer,
                    source_address=source_address,
                    timeout=timeout, **kwargs)
    protocol = EventLoopThread("ioloop", IOLoop.current().start,
                               IOLoop.current().stop)
    protocol.start()
    future = client.connect()

    return protocol, future
Beispiel #2
0
def io_loop_factory(host="127.0.0.1", port=Defaults.Port, framer=None,
                   source_address=None, timeout=None, **kwargs):
    """
    Factory to create Tornado based asynchronous udp clients
    :param host: Host IP address
    :param port: Port
    :param framer: Modbus Framer
    :param source_address: Bind address
    :param timeout: Timeout in seconds
    :param kwargs:
    :return: event_loop_thread and tornado future
    """
    from tornado.ioloop import IOLoop
    from pymodbus.client.asynchronous.tornado import AsyncModbusUDPClient as \
        Client

    client = Client(host=host, port=port, framer=framer,
                    source_address=source_address,
                    timeout=timeout, **kwargs)
    protocol = EventLoopThread("ioloop", IOLoop.current().start,
                               IOLoop.current().stop)
    protocol.start()
    future = client.connect()

    return protocol, future
Beispiel #3
0
def async_io_loop_factory(port=None, framer=None, **kwargs):
    from tornado.ioloop import IOLoop
    from pymodbus_async.client.asynchronous.tornado import (
        AsyncModbusSerialClient as Client)
    ioloop = IOLoop()
    protocol = EventLoopThread("ioloop", ioloop.start, ioloop.stop)
    protocol.start()
    client = Client(port=port, framer=framer, ioloop=ioloop, **kwargs)
    future = client.connect()
    return protocol, future
Beispiel #4
0
def io_loop_factory(port=None, framer=None, **kwargs):
    """
    Factory to create Tornado based asynchronous serial clients
    :param port:  Serial port
    :param framer: Modbus Framer
    :param kwargs:
    :return: event_loop_thread and tornado future
    """

    from tornado.ioloop import IOLoop
    from pymodbus.client.asynchronous.tornado import (AsyncModbusSerialClient as
                                               Client)

    ioloop = IOLoop()
    protocol = EventLoopThread("ioloop", ioloop.start, ioloop.stop)
    protocol.start()
    client = Client(port=port, framer=framer, ioloop=ioloop, **kwargs)

    future = client.connect()

    return protocol, future
Beispiel #5
0
def io_loop_factory(port=None, framer=None, **kwargs):
    """
    Factory to create Tornado based asynchronous serial clients
    :param port:  Serial port
    :param framer: Modbus Framer
    :param kwargs:
    :return: event_loop_thread and tornado future
    """

    from tornado.ioloop import IOLoop
    from pymodbus.client.asynchronous.tornado import (AsyncModbusSerialClient as
                                               Client)

    ioloop = IOLoop()
    protocol = EventLoopThread("ioloop", ioloop.start, ioloop.stop)
    protocol.start()
    client = Client(port=port, framer=framer, ioloop=ioloop, **kwargs)

    future = client.connect()

    return protocol, future
Beispiel #6
0
def reactor_factory(port, framer, **kwargs):
    """
    Factory to create twisted serial asynchronous client
    :param port: Serial port
    :param framer: Modbus Framer
    :param kwargs:
    :return: event_loop_thread and twisted serial client
    """
    from twisted.internet import reactor
    from twisted.internet.serialport import SerialPort
    from twisted.internet.protocol import ClientFactory
    from pymodbus.factory import ClientDecoder

    class SerialClientFactory(ClientFactory):
        def __init__(self, framer, proto_cls):
            ''' Remember things necessary for building a protocols '''
            self.proto_cls = proto_cls
            self.framer = framer

        def buildProtocol(self):
            ''' Create a protocol and start the reading cycle '''
            proto = self.proto_cls(self.framer)
            proto.factory = self
            return proto

    class SerialModbusClient(SerialPort):
        def __init__(self, framer, *args, **kwargs):
            ''' Setup the client and start listening on the serial port

            :param factory: The factory to build clients with
            '''
            self.decoder = ClientDecoder()
            proto_cls = kwargs.pop("proto_cls", None)
            proto = SerialClientFactory(framer, proto_cls).buildProtocol()
            SerialPort.__init__(self, proto, *args, **kwargs)

    proto = EventLoopThread("reactor",
                            reactor.run,
                            reactor.stop,
                            installSignalHandlers=0)
    ser_client = SerialModbusClient(framer, port, reactor, **kwargs)

    return proto, ser_client
Beispiel #7
0
def reactor_factory(host="127.0.0.1",
                    port=Defaults.Port,
                    framer=None,
                    source_address=None,
                    timeout=None,
                    **kwargs):
    """
    Factory to create twisted tcp asynchronous client
    :param host: Host IP address
    :param port: Port
    :param framer: Modbus Framer
    :param source_address: Bind address
    :param timeout: Timeout in seconds
    :param kwargs:
    :return: event_loop_thread and twisted_deferred
    """
    from twisted.internet import reactor, protocol
    from pymodbus.client.asynchronous.twisted import ModbusTcpClientProtocol

    deferred = protocol.ClientCreator(reactor,
                                      ModbusTcpClientProtocol).connectTCP(
                                          host,
                                          port,
                                          timeout=timeout,
                                          bindAddress=source_address)

    callback = kwargs.get("callback")
    errback = kwargs.get("errback")

    if callback:
        deferred.addCallback(callback)

    if errback:
        deferred.addErrback(errback)

    protocol = EventLoopThread("reactor",
                               reactor.run,
                               reactor.stop,
                               installSignalHandlers=0)
    protocol.start()

    return protocol, deferred