简体   繁体   English

使用 python 在 ModbusRTU 中同时调用多个寄存器?

[英]Calling multiple registers simultaneously in a ModbusRTU using python?

I have a Modbus-RTU thats connected to a Modbus-RTU/TCP-Gateway, which i'm trying to call.我有一个连接到 Modbus-RTU/TCP-Gateway 的 Modbus-RTU,我正在尝试调用它。 Works fine, but I have to read multiple values each reading cycle.工作正常,但我必须在每个阅读周期读取多个值。

I'm using the pymodbus library.我正在使用 pymodbus 库。

Say I have 3 Registers to read:假设我有 3 个寄存器要读取:

Address, WordLength地址,字长

1: 0,2 1:0,2

2: 2,2 2:2,2

3: 206, 4 3:206、4

What I'm doing at the moment is calling the register values 1 and 2 together, by starting to read at adress 0, with a word count of 4. But then I have to make another request to register value 3.我现在正在做的是一起调用寄存器值 1 和 2,从地址 0 开始读取,字数为 4。但是我必须再次请求寄存器值 3。

Which means, when trying to timestamp the values I encounter the problem that they were actually not read at the exact same time.这意味着,在尝试为值添加时间戳时,我遇到的问题是它们实际上不是在完全相同的时间读取的。

The question: Is there a way to call multiple modbus registers simultaneously, say by calling read_holding_registers with a list of registers and a list of their respective word counts in python?问题:有没有办法同时调用多个 modbus 寄存器,比如调用 read_holding_registers 和 python 中的寄存器列表和它们各自的字数列表?

In a way that would work like this:以这样的方式工作:

>> response = gateway.read_holding_registers(address=[0,2,206], count=[2,2,4], unit=unit).registers
>> response
<< [[0,26],[0,27],[0,0,0,3008]]

Short answer: No, there is no way you can poll non-contiguous registers on a single Modbus query.简短回答:不,您无法在单个 Modbus 查询上轮询非连续寄存器。 That's a limitation of the Modbus protocol.这是 Modbus 协议的限制。

If your device (you don't mention what device in particular you are talking about, so from here on in I'll be guessing) sticks to the Modbus specification then the maximum number of registers you can interrogate on a single query is 125. That's required to comply with the maximum message length, which is 256 bytes according to the spec :如果您的设备(您没有特别提到您正在谈论的设备,所以从这里开始我会猜测)坚持 Modbus 规范,那么您可以在单个查询中查询的最大寄存器数是 125。这需要符合最大消息长度,根据规范,它是 256 字节:

The quantity of registers to be read, combined with all other fields in the expected response, must not exceed the allowable length of Modbus messages: 256 bytes.要读取的寄存器数量,连同预期响应中的所有其他字段,不得超过 Modbus 消息的允许长度:256 字节。

The lack of synchronization you mention is frequently overcome using one of the following two strategies:您提到的缺乏同步经常使用以下两种策略之一来克服:

-At the device (slave) level (for instance, if your device is a PLC where you have the freedom to program whatever you want in it) you sample all sensors (or those you need to keep in sync) at the same time and do not update again for a certain period of time to give room to the Master to read all data (most sensors are not that fast so this is in general not a problem for most industrial settings). - 在设备(从)级别(例如,如果您的设备是 PLC,您可以自由地在其中编程任何您想要的东西),您可以同时对所有传感器(或您需要保持同步的传感器)进行采样,并且在一段时间内不要再次更新,以便为 Master 提供读取所有数据的空间(大多数传感器没有那么快,因此对于大多数工业设置而言这通常不是问题)。

-At the master level, you can use a multithreading approach to launch two (or more) simultaneous queries (you need to somehow synchronize those queries). - 在主级别,您可以使用多线程方法来启动两个(或更多)同时查询(您需要以某种方式同步这些查询)。 This only works for Modbus TCP devices that accept multiple simultaneous connections (most do).这仅适用于接受多个同时连接的 Modbus TCP 设备(大多数都可以)。 Since you seem to be working with the TCP side of your gateway, this solution might work for your time-stamping problem but be aware that, in essence, it would be just smoke and mirrors to keep the fantasy that your sensors are read in sync (not to mention it would be fancy smoke and mirrors because it would take some effort to come up with the code).由于您似乎正在使用网关的 TCP 端,因此此解决方案可能适用于您的时间戳问题,但请注意,从本质上讲,它只是烟雾和镜子,以保持您的传感器被同步读取的幻想(更不用说它会是花哨的烟雾和镜子,因为编写代码需要一些努力)。

You should recoil a bit and ask yourself what is the reason to have your readings in sync.你应该稍微退缩一下,问问自己让你的读数同步的原因是什么。 If you are in a hard real-time environment (where, for instance, you have to take measurements of two sensors at the exact same time because you need to make a calculation using both variables and they change really fast) you need to rethink your data acquisition system to use the first approach I mentioned above (read in sync and freeze).如果您处于硬实时环境中(例如,您必须同时测量两个传感器,因为您需要使用这两个变量进行计算并且它们变化非常快),您需要重新考虑您的数据采集系统使用我上面提到的第一种方法(同步读取和冻结)。

I did not mention it earlier because I imagine you don't have access to the Modbus mapping of the final device, but if you do the trivial and smart thing to do is to reorganize the order of the registers according to your needs.我之前没有提到它,因为我想您无法访问最终设备的 Modbus 映射,但是如果您做一件简单而聪明的事情,那就是根据您的需要重新组织寄存器的顺序。 Be sure to take a look at the manual of your unit, most high-end and/or real-time devices will actually allow you this kind of change (in an easy way I mean, changing the firmware will always fix your issue).请务必查看您设备的手册,大多数高端和/或实时设备实际上都允许您进行这种更改(我的意思是简单地说,更改固件将始终解决您的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中同时使用多个程序 - Using multiple programs simultaneously in Python AttributeError: 'ExceptionResponse' object 没有属性 'registers' (ModbusRTU over TCP/IP)? - AttributeError: 'ExceptionResponse' object has no attribute 'registers' (ModbusRTU over TCP/IP)? 使用python读取硬件中的寄存器 - reading registers in hw using python 使用 scrapy 同时抓取多个 diffe.net url splash 使用 python - scraping multiple differnet urls simultaneously using scrapy splash using python 使用python在sqlite中同时将数据插入多个表 - Inserting data into multiple tables simultaneously in sqlite using python 使用 Kafka-Python 同时发送和使用多个图像 - Send and Consume multiple images simultaneously using Kafka-Python 使用 Python 同时运行多个脚本的有效方法 - Efficient way of running multiple scripts simultaneously using Python 使用 python paramiko package 同时与多个服务器建立连接 - establish connection with multiple servers simultaneously using python paramiko package 无法同时在python中使用多个特殊字符或模式提取字符串 - Not able to extract string using multiple special characters or pattern in python simultaneously 如何在 python 中使用 os.system() 同时执行多个命令 - How to execute multiple commands simultaneously using os.system() in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM