def __init__(self, host, port="502", timeout=5, loop=None): # pylint: disable=unpacking-non-sequence LOGGER.debug("creating loop and client") self.loop, self.client = AsyncModbusTCPClient( schedulers.ASYNC_IO, port=port, host=host, loop=loop ) LOGGER.debug("created loop and client") self.timeout = timeout self._time_offset = None
def testTcpTwistedClient(self): """ Test the TCP Twisted client :return: """ from twisted.internet import reactor with patch("twisted.internet.reactor") as mock_reactor: def test_callback(client): pass def test_errback(client): pass AsyncModbusTCPClient(schedulers.REACTOR, framer=ModbusSocketFramer(ClientDecoder()), callback=test_callback, errback=test_errback)
def testTcpTornadoClient(self, mock_iostream, mock_ioloop): """ Test the TCP tornado client client initialize """ protocol, future = AsyncModbusTCPClient(schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder())) client = future.result() assert(isinstance(client, AsyncTornadoModbusTcpClient)) assert(0 == len(list(client.transaction))) assert(isinstance(client.framer, ModbusSocketFramer)) assert(client.port == 502) assert client._connected assert(client.stream.connect.call_count == 1) assert(client.stream.read_until_close.call_count == 1) def handle_failure(failure): assert(isinstance(failure.exception(), ConnectionException)) d = client._build_response(0x00) d.add_done_callback(handle_failure) assert(client._connected) client.close() protocol.stop() assert(not client._connected)
# extra requests # --------------------------------------------------------------------------- # # If you are performing a request that is not available in the client # mixin, you have to perform the request like this instead:: # # from pymodbus.diag_message import ClearCountersRequest # from pymodbus.diag_message import ClearCountersResponse # # request = ClearCountersRequest() # response = client.execute(request) # if isinstance(response, ClearCountersResponse): # ... do something with the response # # --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- # # choose the client you want # --------------------------------------------------------------------------- # # make sure to start an implementation to hit against. For this # you can use an existing device, the reference implementation in the tools # directory, or start a pymodbus server. # --------------------------------------------------------------------------- # if __name__ == "__main__": protocol, deferred = AsyncModbusTCPClient(schedulers.REACTOR, port=5020) # protocol, deferred = AsyncModbusUDPClient(schedulers.REACTOR, port=5020) # callback=beginAsynchronousTest, # errback=err) deferred.addCallback(beginAsynchronousTest) deferred.addErrback(err)