gpt4 book ai didi

java - 如何组合三个连续的Modbus寄存器以获得整数值?

转载 作者:行者123 更新时间:2023-12-02 01:43:55 24 4
gpt4 key购买 nike

我必须使用 Modbus 协议(protocol)读取设备的值。它有一个 6 字节长的值,由三个连续的保持寄存器表示。这是有符号和无符号类型的图形表示。

enter image description here

enter image description here

我可以使用 Java 中的 Modbus 库读取三个寄存器。每个寄存器都被读取为 2 个字节的短值。现在我需要知道如何组合这三个值以形成有符号和无符号格式的整数值?

最佳答案

请记住,我已经很长时间没有写过java了,所以我的java非常生疏。因为 Java 中没有无符号值,所以有点困难。尝试这样的事情:

class ModbusConvert
{
public static long toUnsigned(int register0, int register1, int register2)
{
//NOTE: Assumes long is 64-bit, int is 32-bit

// Convert each register to a long in the appropriate bit position and OR them together
long v2 = (((long)register2) & 0x0FFFF);
long v1 = (((long)register1) & 0x0FFFF) << 16;
long v0 = (((long)register0) & 0x0FFFF) << 32;
return v0 | v1 | v2;
}

public static long toSigned(int register0, int register1, int register2)
{
//NOTE: Assumes long is 64-bit, int is 32-bit

// Convert each register to a long in the appropriate bit position and OR them together
if ((register0 & 0x8000) != 0) register0 |= 0xFFFF0000; // Sign extend

long v2 = (((long)register2) & 0x0FFFF);
long v1 = (((long)register1) & 0x0FFFF) << 16;
long v0 = ((long)register0) << 32;
return v0 | v1 | v2;
}

public static void main(String args[])
{
// Example Register Values (value = 0xFEDC76543210)
int register0 = 0xFEDC;
int register1 = 0x7654;
int register2 = 0x3210;

long unsigned = toUnsigned(register0, register1, register2);
long signed = toSigned(register0, register1, register2);

System.out.println(String.format("UNSIGNED: 0x%016X", unsigned));
System.out.println(String.format("SIGNED : 0x%016X", signed));
}
}

The output is:
UNSIGNED: 0x0000FEDC76543210
SIGNED : 0xFFFFFEDC76543210

关于java - 如何组合三个连续的Modbus寄存器以获得整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53990300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com