写入32位整数到连续的MODBUS保持寄存器

huangapple go评论59阅读模式
英文:

Writing 32 bit integers to sequential MODBUS holding registers

问题

我有一个一般性的问题。我有32位整数要写入顺序的MODBUS寄存器,然后我将稍后读取它们。我了解保持寄存器是16位的,所以我需要从/向两个连续的MODBUS保持寄存器读/写32位整数。

我的问题是如何在它们不互相干扰的情况下进行顺序写入?

我正在使用的代码是vb.net。

当我将32位整数写入顺序的MODBUS地址时,我从MODBUS寄存器中读取到不正确的值。

写入32位整数到连续的MODBUS保持寄存器

是否有一种使用字节顺序或字顺序的方法,以便保持寄存器可以共享数据,从而使我能够在保持寄存器之间读取/写入32位整数而没有任何问题?

谢谢。

英文:

I have a general question here. I have 32 bit integers that I want to write to sequential MODBUS registers which I will later read back. I understand holding registers are 16 bit so I need to read/write the 32 bit integer from/to two consecutive MODBUS holding registers.

My question is how do I write sequentially without them interfering with each other?

The code I am using is vb.net.

When I write 32 bit integers to sequential MODBUS addresses I get incorrect values read back from the MODBUS registers.

写入32位整数到连续的MODBUS保持寄存器

Is there a way to use the byte order or word order so that holding registers will share data that will allow me to read/write 32 bit integers from/to holding registers without any issues?

Thank you.

答案1

得分: 2

你试图将两个不同的值存储在一个寄存器中(寄存器1包含值1的一部分和值2的一部分)。这样做是不允许的,当写入第二个值时,您将覆盖第一个值的一部分。

我理解保持寄存器是16位的,所以我需要从/向两个连续的MODBUS保持寄存器中读取/写入32位整数。

好的,您需要两个16位寄存器来写入数据。Modbus规范对于32位值没有具体规定,所以如何实现取决于您自己。如果您想将第一个16位写入寄存器0,第二个写入寄存器100,那么这也可以(但可能不容易让其他人理解)。

通常我会期望32位值会写入相邻的两个寄存器。因此,在您的例子中,第一个值可以写入寄存器0和1,第二个值写入寄存器2和3。

我想要写入连续的MODBUS寄存器。

在处理32位值时,每个值需要两个16位寄存器。所以我会理解“连续”是指从每隔一个寄存器开始(例如0、2、4、6)。

需要注意的一点是,由于规范没有涵盖存储32位值,您应该明确使用的字节顺序。例如,在存储0x11223344时,您是将0x1122存储在寄存器0中,0x3344存储在寄存器1中,还是相反?没有绝对正确的答案,但重要的是您要记录程序使用的方法(因为规范没有提供任何指导,两种顺序都有使用!)。

英文:

You are attempting to store two different values in one register (register 1 holds part of value 1 and part of value 2). You cannot do this, when writing the second value you will overwrite part of the first.

>I understand holding registers are 16 bit so I need to read/write the 32 bit integer from/to two consecutive MODBUS holding registers.

Well you need two 16 bit registers to write the data. The Modbus spec says nothing about 32 bit values, so how you do this is up to you. If you want to write the first 16 bits in register 0 and the second in register 100 then that will work (but might not be easy for others to understand!).

Generally I would expect that a 32 bit value would be written across two adjacent registers. So, in your example the first value could be written to registers 0 and 1, with the second in registers 2 and 3.

> I want to write to sequential MODBUS registers

When dealing with 32 bit values you need two 16-bit registers per value. So I would take "sequential" to mean starting every second register (e.g. 0, 2, 4, 6).

One thing to note is that, because the Spec does not cover storing 32 bit values, you should be clear on the endianness in use. For example, when storing 0x11223344 do you store 0x1122 in register 0 and 0x3344 in register 1, or do the opposite? There is no right answer but it's important that you document the approach your program uses (because the spec does not provide any guidance and both orders are used!).

huangapple
  • 本文由 发表于 2023年7月18日 03:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定