首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Modbus主机无法更新第二个客户端编写的数据。

Modbus主机无法更新第二个客户端编写的数据。
EN

Stack Overflow用户
提问于 2022-06-22 19:58:37
回答 2查看 157关注 0票数 0

我们一直在努力将传送带纳入我们的应用程序。供应商告诉我们,他们有一个modbus主机,每10 us更新一次值,我们应该在某个IP/端口上有一个从服务器。经过调查,我们发现我们需要分拆一个modbus tcp服务器。我们试图利用:

PyModbusTCP

代码语言:javascript
复制
from pyModbusTCP.server import ModbusServer
server = ModbusServer(host="", port=502)
server.start()

pymodbus:

代码语言:javascript
复制
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))

对他们两人来说:

  • 主程序可以编写值
  • 第二个客户端(来自不同的进程)可以编写值
  • ,第二个客户端可以从主服务器读取更新的值以及它早先编写的

值。

但是,第二个客户端编写的值不会在主服务器中得到更新。事实上,供应商说他们根本看不到任何值,尽管他们可以看到modbus连接是活动的。

在从第二个客户端写入时,来自服务器(从服务器)的日志有一个错误,我们不知道它是什么:

代码语言:javascript
复制
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

任何帮助都将不胜感激!提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2022-06-23 13:16:27

这个问题现在已经解决了。问题是供应商已经设置了硬件连接,所以他们使用不同的功能来读取:

而他们使用的是FC2s和FC4s等,因此主程序无法读取我们的值。

票数 1
EN

Stack Overflow用户

发布于 2022-06-22 20:44:17

从您的描述中,我们并不清楚问题是什么(正如您所说的,“有一个错误,我们不知道它是什么”)。来自服务器的日志不会显示任何错误,但它们确实显示了一些可能解释您所看到的内容的内容。

编写单线圈请求是:0f 14 00 00 00 06 01 05 00 00 00 00。这是将线圈01 (物理00)的值设置为off的请求。

然后我们看到一个读取请求1a 83 00 00 00 06 01 01 40 00 00 01。这是要求从线圈0x4001 (物理0x4000)开始的1个线圈。

因此,您似乎没有访问相同的线圈(写入0x01,而是从0x4001读取)(因此,值没有发生变化也就不足为奇了)。请注意,我只快速扫描了日志,因此可能遗漏了一些内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72721324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档