英文:
Write hexadecimal values into register with leading zeros
问题
我有一个包含16个十六进制值的字符串数组。我需要将它们转换为字节,以便将它们写入Modbus的设备寄存器(每个寄存器的大小为16位)。我们有8个寄存器用于写入16个十六进制值。因此,我将它们从short转换为字节,以便将它们写入设备的寄存器。
以下是我的代码--
String[] advanceByte = {"00", "00", "00", "00", "07", "46", "46", "07", "01", "00", "02", "02", "02", "03", "00", "00"};
short[] hexToShort = new short[16];
for (int i = 0; i < 16; i++) {
hexToShort[i] = Short.parseShort(advanceByte[i], 16);
}
// 转换为字节
byte[] byteData = new byte[16];
for (int i = 0; i < advanceByte.length; i++) {
byteData[i] = (byte) hexToShort[i];
}
for (int i = 0; i < byteData.length - 1;) {
byte[] byteArr1 = {(byte) byteData[i], (byte) byteData[i + 1]};
try {
trans = new ModbusSerialTransaction(serialConnection);
SimpleRegister myregister;
myregister = new SimpleRegister(byteArr1[0], byteArr1[1]);
writeSingleRegisterRequest = new WriteSingleRegisterRequest();
writeSingleRegisterRequest.setUnitID(slave_Address);
writeSingleRegisterRequest.setHeadless();
writeSingleRegisterRequest.setReference(ref2); //register number
myregister.setValue(byteArr1);
Register[] register = {myregister};
writeSingleRegisterRequest.setRegister(myregister);
trans.setRequest(writeSingleRegisterRequest);
trans.execute();
i = i + 2;
ref2++;
} catch (com.ghgande.j2mod.modbus.ModbusException exception) {
advancedStatus = false;
System.out.println("Table 4x4 data Writing Exception");
System.out.println("Exception Occured ----" + exception);
}
serialConnection.close();
}
每当我通过QModMaster从寄存器中读取写入的数据时,前导零都会丢失。
需要帮助解决此问题。
这是我从其他应用程序中读取的结果。在此结果中,前导零丢失。
这是期望的结果。
英文:
I have an string array which contains 16 hexadecimal values. I need to convert them into byte to write them at Modbus's device register(size of each register is 16-bit). We have 8 registers to write 16 hexadecimal values. So, I'm converting them into the short and from short to byte for write them at device's register.
Here is my code --
String[] advanceByte = { "00", "00", "00", "00", "07", "46", "46", "07", "01", "00", "02", "02", "02", "03", "00", "00"};
short[] hexToShort = new short[16];
for (int i = 0; i < 16; i++) {
hexToShort[i] = Short.parseShort(advanceByte[i], 16);
}
// Converting Integer to Byte
byte[] byteData = new byte[16];
for (int i = 0; i < advanceByte.length; i++) {
byteData`[i] = (byte) hexToShort[i];
}
for (int i = 0; i < byteData.length - 1;) {
byte[] byteArr1 = {(byte) byteData[i], (byte) byteData[i + 1]};
try {
trans = new ModbusSerialTransaction(serialConnection);
SimpleRegister myregister;
myregister = new SimpleRegister(byteArr1[0], byteArr1[1]);
writeSingleRegisterRequest = new WriteSingleRegisterRequest();
writeSingleRegisterRequest.setUnitID(slave_Address);
writeSingleRegisterRequest.setHeadless();
writeSingleRegisterRequest.setReference(ref2); //register number
myregister.setValue(byteArr1);
Register[] register = {myregister};
writeSingleRegisterRequest.setRegister(myregister);
trans.setRequest(writeSingleRegisterRequest);
trans.execute();
i = i + 2;
ref2++;
} catch (com.ghgande.j2mod.modbus.ModbusException exception) {
advancedStatus = false;
System.out.println("Table 4x4 data Writing Exception");
System.out.println("Exception Occured ----" + exception);
}
serialConnection.close();
}
Whenever, I am reading the written data from the register through QModMaster , leading zeros are missing.
Need help to solve this Issue.
This is the result, when I read them from other application. In this result, leading zero are missing.
This is the expected result.
答案1
得分: 1
我能看到你的代码中唯一的问题是类型转换。我认为你应该避免 String
到 Short
的转换,特别是在你可以直接将 hex
转换为 byte
的情况下。你可以这样做:
String[] advanceByte = {"00", "00", "00", "00", "07", "46", "46", "07", "01", "00", "02", "02", "02", "03", "00", "00"};
byte[] byteData = new byte[16];
for (int i = 0; i < advanceByte.length; i++) {
byteData[i] = (byte) Integer.parseInt(advanceByte[i], 16);
}
for (int i = 0; i < byteData.length - 1;) {
byte[] byteArr1 = {(byte) byteData[i], (byte) byteData[i + 1]};
try {
trans = new ModbusSerialTransaction(serialConnection);
SimpleRegister myregister;
myregister = new SimpleRegister(byteArr1[0], byteArr1[1]);
writeSingleRegisterRequest = new WriteSingleRegisterRequest();
writeSingleRegisterRequest.setUnitID(slave_Address);
writeSingleRegisterRequest.setHeadless();
writeSingleRegisterRequest.setReference(ref2); //register number
myregister.setValue(byteArr1);
Register[] register = {myregister};
writeSingleRegisterRequest.setRegister(myregister);
trans.setRequest(writeSingleRegisterRequest);
trans.execute();
i = i + 2;
ref2++;
} catch (com.ghgande.j2mod.modbus.ModbusException exception) {
advancedStatus = false;
System.out.println("Table 4x4 data Writing Exception");
System.out.println("Exception Occurred ----" + exception);
}
serialConnection.close();
}
请注意,我将 Byte.parseByte()
更改为了 Integer.parseInt()
,并将结果强制转换为 byte
类型,以便更准确地处理十六进制字符串到字节的转换。
英文:
The only wrong thing I can see in your code is conversion. I think you should have avoided the String to Short
conversion especially when, you can directly convert hex to byte
.
You should have done it something like this -
String[] advanceByte = { "00", "00", "00", "00", "07", "46", "46", "07", "01", "00", "02", "02", "02", "03", "00", "00"};
byte[] byteData = new byte[16];
for (int i = 0; i < advanceByte.length; i++) {
byteData[i] = Byte.parseByte(advanceByte[i],16);
}
for (int i = 0; i < byteData.length - 1;) {
byte[] byteArr1 = {(byte) byteData[i], (byte) byteData[i + 1]};
try {
trans = new ModbusSerialTransaction(serialConnection);
SimpleRegister myregister;
myregister = new SimpleRegister(byteArr1[0], byteArr1[1]);
writeSingleRegisterRequest = new WriteSingleRegisterRequest();
writeSingleRegisterRequest.setUnitID(slave_Address);
writeSingleRegisterRequest.setHeadless();
writeSingleRegisterRequest.setReference(ref2); //register number
myregister.setValue(byteArr1);
Register[] register = {myregister};
writeSingleRegisterRequest.setRegister(myregister);
trans.setRequest(writeSingleRegisterRequest);
trans.execute();
i = i + 2;
ref2++;
} catch (com.ghgande.j2mod.modbus.ModbusException exception) {
advancedStatus = false;
System.out.println("Table 4x4 data Writing Exception");
System.out.println("Exception Occured ----" + exception);
}
serialConnection.close();
}
答案2
得分: 0
我所看到的唯一问题是,QModMaster程序不显示前导零,但值被正确传输。
请不要混淆值与它们的字符串表示,Modbus传输的是值,而不是表示方式,因此这些值可以以十六进制、十进制、二进制等形式显示,而QModMaster的作者决定不显示前导零。
你的问题应该以“我有一个包含了16个字节值十六进制表示的字符串数组”的方式开始。
英文:
The only thing I see is that the QModMaster program does not show the leading zeros but values are transmitted correctly
Do not confuse values with their string representation, Modbus transmits values, not representations, then those values can be shown in hexadecimal, decimal, binary etc. and QModMaster author decided to don't show leading zeros.
Your question should have started with "I have an string array which contains the hexadecimal representation of 16 byte values".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论