@Adam
I want to read data from register address 40101 with start address 101 and
float data type.
But I couldn't get what you wanted to convey with this comment *-instead of
register “4001” try “1”, and of that doesn’t work try “0”. *
Can you please elaborate.
On Wed, Aug 12, 2020 at 5:16 PM Adam Rossi wrote:
> Instead of register “4001” try “1”, and of that doesn’t work try “0”.
>
> Regards Adam
>
> > On Aug 12, 2020, at 5:59 AM, Syed Kefayath
> wrote:
> >
> > Hi Everyone,
> >
> > I am using a plc4x modbus driver to read data from a modbus device but i
> am
> > left with an exception. But while reading data from the ModbusPal virtual
> > device I am getting the output. The java code and the error message is
> > given below.
> >
> > I need help from this community to complete my POC.
> >
> > Java code.
> >
> > import org.apache.plc4x.java.PlcDriverManager;
> > import org.apache.plc4x.java.api.PlcConnection;
> > import org.apache.plc4x.java.api.messages.PlcReadRequest;
> > import org.apache.plc4x.java.api.messages.PlcReadResponse;
> > import org.apache.plc4x.java.api.types.PlcResponseCode;
> >
> > import java.util.concurrent.CompletableFuture;
> >
> > public class modbus {
> >
> >public static void main(String[] args) throws Exception {
> >System.out.println("started");
> >String connectionString =
> > "modbus:tcp://192.168.4.163:4001?unit-identifier=2";
> >try {
> >PlcConnection plcConnection = new
> > PlcDriverManager().getConnection(connectionString);
> >
> >if (!plcConnection.getMetadata().canRead()) {
> >System.out.println("This connection doesn't support
> reading.");
> >return;
> >}
> >PlcReadRequest.Builder builder =
> plcConnection.readRequestBuilder();
> >builder.addItem("tag1","holding-register:101");
> > //builder.addItem("tag2","holding-register:1[3]");
> >PlcReadRequest readRequest = builder.build();
> >CompletableFuture asyncResponse
> > = readRequest.execute();
> >asyncResponse.whenComplete((response, throwable) -> {
> >System.out.println("response :"+response.toString() );
> >for (String fieldName : response.getFieldNames()) {
> >if (response.getResponseCode(fieldName) ==
> > PlcResponseCode.OK){
> >int registerValues =
> > response.getNumberOfValues(fieldName);
> >if (registerValues == 1){
> >System.out.println(fieldName +" : "+
> > response.getObject(fieldName));
> >}
> >else {
> >for (int i = 0; i< registerValues;i++){
> >System.out.println(fieldName + i +" :
> > " + response.getObject(fieldName,i));
> >}
> >}
> >
> >}
> >else {
> >System.out.println("Error[" + fieldName+ "]:
> > "+ response.getResponseCode(fieldName).name());
> >}
> >}
> >
> >});
> >
> >
> >} catch (Exception e) {
> >System.out.println("Something went wrong."+e);
> >}
> >System.out.println("Execution completed");
> >}
> > }
> >
> >
> > Output:
> >
> > started
> > SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
> > SLF4J: Defaulting to no-operation (NOP) logger implementation
> > SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for
> further
> > details.
> > Execution completed
> > Aug 12, 2020 3:05:02 PM io.netty.channel.DefaultChannelPipeline
> > onUnhandledInboundException
> > WARNING: An exceptionCaught() event was fired, and it reached at the tail
> > of the pipeline. It usually means the last handler in the pipeline did
> not
> > handle the exception.
> > io.netty.handler.codec.DecoderException:
> > org.apache.plc4x.java.api.exceptions.PlcRuntimeException: Unexpected
> > response type ModbusPDUReadHoldingRegistersResponse
> > at
> >
> io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.