The code you posted is reading register 4001 on slave ID 2. There are different 
conventions as to addressing registers, sometimes the 4XXX is not used, and 
sometimes the register is 1 less than published. 

Also verify you really want slave ID 2. Usually you want 0 or 1. 

If you want address 101 your code is incorrect. Try “101” and if that doesn’t 
work try “100”. 

> On Aug 12, 2020, at 8:06 AM, Syed Kefayath <syed.kefay...@elmeasure.com> 
> wrote:
> 
> @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 <ac.ro...@gmail.com> 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 <syed.kefay...@elmeasure.com>
>> 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<? extends PlcReadResponse> 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.java:98)
>>> at
>>> 
>> io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
>>> at
>>> 
>> io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
>>> at
>>> 
>> io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
>>> at
>>> 
>> io.netty.handler.codec.ByteToMessageCodec.channelRead(ByteToMessageCodec.java:103)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
>>> at
>>> 
>> io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
>>> at
>>> 
>> io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
>>> at
>>> 
>> io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
>>> at
>>> 
>> io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
>>> at
>>> 
>> io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
>>> at
>>> 
>> io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
>>> at
>>> 
>> io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
>>> at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
>>> at
>>> 
>> io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
>>> at
>> io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
>>> at
>>> 
>> io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
>>> at java.base/java.lang.Thread.run(Thread.java:834)
>>> Caused by: org.apache.plc4x.java.api.exceptions.PlcRuntimeException:
>>> Unexpected response type ModbusPDUReadHoldingRegistersResponse
>>> at
>>> 
>> org.apache.plc4x.java.modbus.protocol.ModbusProtocolLogic.toPlcValue(ModbusProtocolLogic.java:240)
>>> at
>>> 
>> org.apache.plc4x.java.modbus.protocol.ModbusProtocolLogic.lambda$read$2(ModbusProtocolLogic.java:112)
>>> at
>>> 
>> org.apache.plc4x.java.spi.Plc4xNettyWrapper.decode(Plc4xNettyWrapper.java:154)
>>> at
>>> 
>> io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81)
>>> at
>>> 
>> io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88)
>>> ... 23 more
>>> 
>>> When used another Modbus master Simulator  by name - modpoll to cross
>>> verify , getting the correct value as shown below.
>>> 
>>> 
>>> ./modpoll -m tcp -a 2 -r 101 -c 1 -t 4:float -p 4001 192.168.4.163
>>> modpoll 3.9 - FieldTalk(tm) Modbus(R) Master Simulator
>>> Copyright (c) 2002-2020 proconX Pty Ltd
>>> Visit https://www.modbusdriver.com for Modbus libraries and tools.
>>> 
>>> Protocol configuration: MODBUS/TCP, FC3
>>> Slave configuration...: address = 2, start reference = 101, count = 1
>>> Communication.........: 192.168.4.163, port 4001, t/o 1.00 s, poll rate
>>> 1000 ms
>>> Data type.............: 32-bit float, output (holding) register table
>>> 
>>> -- Polling slave... (Ctrl-C to stop)
>>> [101]: 2353.341064
>>> -- Polling slave... (Ctrl-C to stop)
>>> Reply time-out!
>>> -- Polling slave... (Ctrl-C to stop)
>>> Reply time-out!
>>> -- Polling slave... (Ctrl-C to stop)
>>> [101]: 2353.341064
>>> -- Polling slave... (Ctrl-C to stop)
>>> [101]: 2353.341064
>>> -- Polling slave... (Ctrl-C to stop)
>>> [101]: 2353.341064
>>> 
>>> 
>>> --
>>> Thanks and Regards;
>>> 
>>> Syed Kefayath
>>> 
>>> --
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> IMPORTANT: The contents of this email and any attachments are
>>> confidential. They are intended for the named recipient(s) only. If you
>>> have received this email by mistake, please notify the sender
>> immediately
>>> and do not disclose the contents to anyone or make copies thereof.
>>> 
>>> Please,
>>> consider your environmental responsibility. Before printing this e-mail
>> ask
>>> yourself: "Do I need a hard copy?"
>> 
> 
> 
> -- 
> Thanks and Regards;
> 
> Syed Kefayath
> 
> -- 
> 
> 
> 
> 
> 
> 
> 
> IMPORTANT: The contents of this email and any attachments are 
> confidential. They are intended for the named recipient(s) only. If you 
> have received this email by mistake, please notify the sender immediately 
> and do not disclose the contents to anyone or make copies thereof.
> 
> Please, 
> consider your environmental responsibility. Before printing this e-mail ask 
> yourself: "Do I need a hard copy?"

Reply via email to