tcp – Modbus master cannot update the data written by second client

We have been trying to integrate a conveyor into our application. The supplier told us that they have a modbus master that updates the values ​​every 10ms and that we should have a slave on our end on a certain IP/port. Upon investigation, we discovered that we need to spin up a modbus tcp server. We have tried to use:

PyModbusTCP

from pyModbusTCP.server import ModbusServer
server = ModbusServer(host="", port=502)
server.start()

pymodbus:

import logging
import asyncio

from pymodbus.version import version
from pymodbus.server.async_io import StartTcpServer

from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.datastore import ModbusSequentialDataBlock
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))


# --------------------------------------------------------------------------- #
# configure the service logging
# --------------------------------------------------------------------------- #
FORMAT = ('%(asctime)-15s %(threadName)-15s'
          ' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


async def run_server(host: str="10.0.0.2",
                     port: int = 502,
                     debug: bool = True):
    """Run server."""
    logger.debug(f'Starting server at host="{host}", port="{port}"')
    store = ModbusSlaveContext(
        di=ModbusSequentialDataBlock.create(),
        co=ModbusSequentialDataBlock.create(),
        hr=ModbusSequentialDataBlock.create(),
        ir=ModbusSequentialDataBlock.create(),
        zero_mode=True,
    )

    context = ModbusServerContext(slaves=store, single=True)

    server = await StartTcpServer(
        context,
        address=(host, port),  # nosec
        allow_reuse_address=True,
        defer_start=True,
        console=debug,
    )

    asyncio.get_event_loop().call_later(20, lambda: server.serve_forever)
    await server.serve_forever()


if __name__ == '__main__':
    host: str = ""
    port: int = 502
    debug: bool = 1

    asyncio.run(run_server(host, port, debug))

For both of them:

  • master can write values
  • A second client (from a different process) can write the values
  • The second client can read updated values ​​from master as well as the values ​​it wrote earlier

However, the values ​​written by the second client does not get updated in the master. In fact, the supplier says that they cannot see any values ​​at all although they can see that the modbus connection is active.

The logs from the server(slave) while writing from second client have an error which we do not know what it is:

2022-06-22 12:38:11,796 MainThread      DEBUG    selector_events:59       Using selector: EpollSelector
2022-06-22 12:38:11,796 MainThread      DEBUG    server         :93       Starting server at host="", port="503"
2022-06-22 12:38:19,008 MainThread      DEBUG    async_io       :76       Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,008 MainThread      DEBUG    async_io       :282      TCP client connection established [10.42.0.148:60480]
2022-06-22 12:38:19,008 MainThread      DEBUG    async_io       :76       Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,008 MainThread      DEBUG    async_io       :282      TCP client connection established [10.42.0.148:60478]
2022-06-22 12:38:19,008 MainThread      DEBUG    async_io       :168      Handling data: 0x4d 0x4b 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:19,008 MainThread      DEBUG    socket_framer  :147      Processing: 0x4d 0x4b 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:19,008 MainThread      DEBUG    factory        :137      Factory Request[ReadHoldingRegistersRequest: 3]
2022-06-22 12:38:19,008 MainThread      DEBUG    context        :63       validate: fc-[3] address-24576: count-1
2022-06-22 12:38:19,008 MainThread      DEBUG    context        :77       getValues fc-[3] address-24576: count-1
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :233      send: [ReadHoldingRegistersResponse (1)]- b'4d4b000000050103020000'
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :76       Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :282      TCP client connection established [10.42.0.148:60482]
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :76       Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :282      TCP client connection established [10.42.0.148:60484]
2022-06-22 12:38:19,009 MainThread      DEBUG    async_io       :168      Handling data: 0xd2 0x19 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:19,009 MainThread      DEBUG    socket_framer  :147      Processing: 0xd2 0x19 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:19,009 MainThread      DEBUG    factory        :137      Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:19,010 MainThread      DEBUG    context        :63       validate: fc-[1] address-16384: count-1
2022-06-22 12:38:19,010 MainThread      DEBUG    context        :77       getValues fc-[1] address-16384: count-1
2022-06-22 12:38:19,010 MainThread      DEBUG    async_io       :233      send: [ReadCoilsResponse(1)]- b'd2190000000401010100'
2022-06-22 12:38:19,012 MainThread      DEBUG    async_io       :168      Handling data: 0xec 0x78 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:19,012 MainThread      DEBUG    socket_framer  :147      Processing: 0xec 0x78 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:19,012 MainThread      DEBUG    factory        :137      Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:19,012 MainThread      DEBUG    context        :63       validate: fc-[1] address-16385: count-1
2022-06-22 12:38:19,012 MainThread      DEBUG    context        :77       getValues fc-[1] address-16385: count-1
2022-06-22 12:38:19,012 MainThread      DEBUG    async_io       :233      send: [ReadCoilsResponse(1)]- b'ec780000000401010100'
2022-06-22 12:38:20,011 MainThread      DEBUG    async_io       :168      Handling data: 0xf 0x14 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0x0 0x0
2022-06-22 12:38:20,011 MainThread      DEBUG    socket_framer  :147      Processing: 0xf 0x14 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0x0 0x0
2022-06-22 12:38:20,011 MainThread      DEBUG    factory        :137      Factory Request[WriteSingleCoilRequest: 5]
2022-06-22 12:38:20,011 MainThread      DEBUG    context        :63       validate: fc-[5] address-0: count-1
2022-06-22 12:38:20,011 MainThread      DEBUG    context        :90       setValues[5] 0:1
2022-06-22 12:38:20,011 MainThread      DEBUG    context        :77       getValues fc-[5] address-0: count-1
2022-06-22 12:38:20,011 MainThread      DEBUG    async_io       :233      send: [WriteCoilResponse(0) => 0]- b'0f1400000006010500000000'
2022-06-22 12:38:20,012 MainThread      DEBUG    async_io       :168      Handling data: 0x1a 0x83 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:20,012 MainThread      DEBUG    socket_framer  :147      Processing: 0x1a 0x83 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:20,012 MainThread      DEBUG    factory        :137      Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:20,012 MainThread      DEBUG    context        :63       validate: fc-[1] address-16384: count-1
2022-06-22 12:38:20,012 MainThread      DEBUG    context        :77       getValues fc-[1] address-16384: count-1
2022-06-22 12:38:20,013 MainThread      DEBUG    async_io       :233      send: [ReadCoilsResponse(1)]- b'1a830000000401010100'
2022-06-22 12:38:21,014 MainThread      DEBUG    async_io       :168      Handling data: 0xd1 0x28 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:21,014 MainThread      DEBUG    socket_framer  :147      Processing: 0xd1 0x28 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:21,014 MainThread      DEBUG    factory        :137      Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:21,014 MainThread      DEBUG    context        :63       validate: fc-[1] address-16384: count-1
2022-06-22 12:38:21,014 MainThread      DEBUG    context        :77       getValues fc-[1] address-16384: count-1
2022-06-22 12:38:21,014 MainThread      DEBUG    async_io       :233      send: [ReadCoilsResponse(1)]- b'd1280000000401010100'
2022-06-22 12:38:21,015 MainThread      DEBUG    async_io       :76       Socket [10.42.0.149:503] opened
2022-06-22 12:38:21,015 MainThread      DEBUG    async_io       :282      TCP client connection established [10.42.0.148:60512]
2022-06-22 12:38:21,015 MainThread      DEBUG    async_io       :168      Handling data: 0x80 0xb6 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2022-06-22 12:38:21,015 MainThread      DEBUG    socket_framer  :147      Processing: 0x80 0xb6 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2022-06-22 12:38:21,015 MainThread      DEBUG    factory        :137      Factory Request[WriteSingleCoilRequest: 5]
2022-06-22 12:38:21,015 MainThread      DEBUG    context        :63       validate: fc-[5] address-0: count-1
2022-06-22 12:38:21,015 MainThread      DEBUG    context        :90       setValues[5] 0:1
2022-06-22 12:38:21,016 MainThread      DEBUG    context        :77       getValues fc-[5] address-0: count-1
2022-06-22 12:38:21,016 MainThread      DEBUG    async_io       :233      send: [WriteCoilResponse(0) => 1]- b'80b60000000601050000ff00'
2022-06-22 12:38:21,016 MainThread      ERROR    async_io       :54       Handler for stream [10.42.0.148:60512] has been canceled
2022-06-22 12:38:21,016 MainThread      DEBUG    async_io       :291      TCP client disconnected [10.42.0.148:60512]
2022-06-22 12:38:21,016 MainThread      ERROR    async_io       :54       Handler for stream [10.42.0.148:60512] has been canceled
2022-06-22 12:38:22,016 MainThread      DEBUG    async_io       :168      Handling data: 0xd1 0x8 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:22,016 MainThread      DEBUG    socket_framer  :147      Processing: 0xd1 0x8 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:22,016 MainThread      DEBUG    factory        :137      Factory Request[ReadHoldingRegistersRequest: 3]
2022-06-22 12:38:22,016 MainThread      DEBUG    context        :63       validate: fc-[3] address-24576: count-1
2022-06-22 12:38:22,016 MainThread      DEBUG    context        :77       getValues fc-[3] address-24576: count-1
2022-06-22 12:38:22,016 MainThread      DEBUG    async_io       :233      send: [ReadHoldingRegistersResponse (1)]- b'd108000000050103020000'
2022-06-22 12:38:22,016 MainThread      DEBUG    async_io       :168      Handling data: 0xe4 0x7f 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:22,016 MainThread      DEBUG    socket_framer  :147      Processing: 0xe4 0x7f 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:22,016 MainThread      DEBUG    factory        :137      Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:22,017 MainThread      DEBUG    context        :63       validate: fc-[1] address-16385: count-1
2022-06-22 12:38:22,017 MainThread      DEBUG    context        :77       getValues fc-[1] address-16385: count-1
2022-06-22 12:38:22,017 MainThread      DEBUG    async_io       :233      send: [ReadCoilsResponse(1)]- b'e47f0000000401010100'
2022-06-22 12:38:22,017 MainThread      DEBUG    async_io       :168      Handling data: 0x4f 0x95 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1

Any help would be highly appreciated! Thanks in advance!

Leave a Comment