Heyho,
i got a little question. So I got a measurement device and i retrieve the data via modbus. I usually check the values via "Simply Modbus TCP Client" which displays me the correct data. Now I want to automate this process which takes me into trouble.
I get the correct response but the tricky part seems to be the conversion. I get the same response in hex but when I convert it, I won't get the same result.
There you can see the response on the left, the bytes (4175 AF8A) on the right and the corresponding value (22739113). I expect "bytes" to be a hex representation but I can't convert them into a decimal number.
I wrote a small java-program to display these numbers and their conversions.
public static void main(String[] args) {
HashMap<String, Integer> values = new HashMap<>();
values.put("4175AF52", 22738214);
values.put("41D61FAC", 1484698204);
values.put("419A08A0", 109193237);
Iterator it = values.entrySet().iterator();
while (it.hasNext()) {
System.out.println("/************************************************/");
Map.Entry pair = (Map.Entry) it.next();
int hexIntepretation = (int) Long.parseLong((String) pair.getKey(), 16);
// Print the result
System.out.println(pair.getKey() + " = " + pair.getValue());
System.out.println("Expected:\t" + pair.getValue());
System.out.println("Hex to Int:\t" + hexIntepretation);
System.out.println("Int to Hex:\t" + Integer.toHexString((int) pair.getValue()));
System.out.println("Float to Hex:\t" + Float.toHexString((int) pair.getValue()));
System.out.println("Hex to Int:\t" + Integer.parseInt((String) pair.getKey(), 16));
it.remove(); // avoids a ConcurrentModificationException
}
}
Running this snippet gives this output:
/*******************************************/
419A08A0 = 109193237
Expected: 109193237
Hex to Int: 1100613792
Int to Hex: 6822815
Float to Hex: 0x1.a08a06p26
Hex to Int: 1100613792
/*******************************************/
41D61FAC = 1484698204
Expected: 1484698204
Hex to Int: 1104551852
Int to Hex: 587eb25c
Float to Hex: 0x1.61facap30
Hex to Int: 1104551852
/*******************************************/
4175AF52 = 22738214
Expected: 22738214
Hex to Int: 1098231634
Int to Hex: 15af526
Float to Hex: 0x1.5af526p24
Hex to Int: 1098231634
No matter what I do, I can't get the values. BUT when I do "float-to-hex", I will find some of the initial values in there.
Example:
4175AF52 = 22738214
Float to Hex: 0x1.5af526p24
Yet I can't put the pieces together to solve this puzzle. Any help is appreciated!