Skip to content

[Bug]: Modbus Over TCP Reading Float Value in Negative Issue #1997

Open
@Balaji-Sundar

Description

@Balaji-Sundar

What happened?

Modbus Over TCP Reading Float Value in Negative Issue. Actual value is 1.93e+07
public static void main(String[] args) {
try {
String ip = "10.7.10.1";
int port = 502;
int slaveId = 2;
int requestTimeOut = 10000;
String payLoadByteOrder = "LITTLE_ENDIAN_BYTE_SWAP";
String connectionUrl = String.format("modbus-tcp:tcp://%s:%d?unit-identifier=%d&request-timeout=%d&default-payload-byte-order=%s", ip, port, slaveId, requestTimeOut, payLoadByteOrder);
try (PlcConnection plcConnection = new DefaultPlcDriverManager().getConnection(connectionUrl)) {
if (!plcConnection.getMetadata().isReadSupported()) {
System.out.println("This connection doesn't support reading.");
}
PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
builder.addTagAddress("value1", "holding-register:159:REAL[2]");
PlcReadRequest readRequest = builder.build();
PlcReadResponse response = readRequest.execute().get();
System.out.println("response = " + response.toString());
for (String tagName : response.getTagNames()) {
System.out.println("Tag: " + tagName);
System.out.println("Response code for " + tagName + ": " + response.getResponseCode(tagName));
int numValues = response.getNumberOfValues(tagName);
System.out.println("numValues = " + numValues);
if (response.getResponseCode(tagName) == PlcResponseCode.OK) {
System.out.println("response = " + response.getTag(tagName));
System.out.println("response = " + response.getFloat(tagName));
//response.getAllBytes(tagName).stream().map(HexUtil::toHex).map(hex -> "Register Value: " + hex).forEach(System.out::println);
} else {
System.out.println("Error[" + tagName + "]: " + response.getResponseCode(tagName).name());
}
}
}
} catch (Exception e) {
System.out.println("e = " + e);
}
}
Output:
response = org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse@6a47b187
Tag: value1
Response code for value1: OK
numValues = 2
response = ModbusTagHoldingRegister {address=158, quantity=2, dataType=REAL }
response = -7.128774E35

Version

v0.12.0

Programming Languages

  • plc4j
  • plc4go
  • plc4c
  • plc4net

Protocols

  • AB-Ethernet
  • ADS /AMS
  • BACnet/IP
  • CANopen
  • DeltaV
  • DF1
  • EtherNet/IP
  • Firmata
  • KNXnet/IP
  • Modbus
  • OPC-UA
  • S7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Modbushttps://plc4x.apache.org/users/protocols/modbus.htmljavaPull requests that update Java code

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions