You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@plc4x.apache.org by "Christofer Dutz (Jira)" <ji...@apache.org> on 2022/09/18 16:03:00 UTC

[jira] [Commented] (PLC4X-354) ArrayIndexOutOfBoundsException when reading a single bit via Modbus

    [ https://issues.apache.org/jira/browse/PLC4X-354?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17606324#comment-17606324 ] 

Christofer Dutz commented on PLC4X-354:
---------------------------------------

The issue is, that we were using the same parser for the coils as for the registers. I'm working on a fix.

> ArrayIndexOutOfBoundsException when reading a single bit via Modbus
> -------------------------------------------------------------------
>
>                 Key: PLC4X-354
>                 URL: https://issues.apache.org/jira/browse/PLC4X-354
>             Project: Apache PLC4X
>          Issue Type: Bug
>          Components: Driver-Modbus
>    Affects Versions: 0.10.0
>         Environment: Windows 10, openjdk 11.0.15
>            Reporter: Christian Schulz
>            Priority: Major
>
> When using current snapshot version of 0.10.0 (0.10.0-20220916.014352-1) reading a singe bit (coil/discrete-input) via modbus, an ArrayIndexOutOfBoundsException. This does work with version 0.9.1.
> Error can be reproduced using a modbus server which simply returns 0 for any given coil/input-register and the following code.
> {code:java}
> String connectionString = "modbus-tcp://127.0.0.1:502";
> try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString)) {
>   PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
>   String modbusAddress = "coil:101:BOOL";
>   builder.addItem("value", modbusAddress);
>   PlcReadRequest readRequest = builder.build();
>   PlcReadResponse response = null;
>   try {
>     response = readRequest.execute().get();
>     for (String fieldName : response.getFieldNames()) {
>       if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
>         System.out.println("Value[" + fieldName + "]: " + response.getObject(fieldName));
>       }
>       // Something went wrong, to output an error message instead.
>       else {
>         System.out.println("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
>       }
>     }
>     System.out.println("done");
>   } catch (Exception e) {
>     System.out.println(e);
>     System.exit(1);
>   }
> } {code}
> When the error occurs, the returned CompletableFuture doesn't complete excetionally but is stuck seemingly forever.
> The error doesnt occur for modbus addresses like "coil:101:BOOL[n]" where n > 2.
> Where this can be a workaround in some cases, it does not help when tha addresses next to the requested one are invalid, since modbus responses with an illegal-address error in that case.
>  
> Full Stack trace of the Exception:
> {code:java}
> io.netty.handler.codec.DecoderException: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
>     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:336)
>     at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:308)
>     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:166)
>     at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
>     at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
>     at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
>     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
>     at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
>     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:829)
> Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
>     at com.github.jinahya.bit.io.ArrayByteInput.read(ArrayByteInput.java:106)
>     at com.github.jinahya.bit.io.DefaultBitInput.read(DefaultBitInput.java:57)
>     at com.github.jinahya.bit.io.AbstractBitInput.unsigned8(AbstractBitInput.java:63)
>     at com.github.jinahya.bit.io.AbstractBitInput.unsigned16(AbstractBitInput.java:92)
>     at com.github.jinahya.bit.io.AbstractBitInput.readInt(AbstractBitInput.java:135)
>     at org.apache.plc4x.java.spi.generation.ReadBufferByteBased.readUnsignedInt(ReadBufferByteBased.java:174)
>     at org.apache.plc4x.java.modbus.readwrite.DataItem.staticParse(DataItem.java:51)
>     at org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic.readBooleanList(ModbusProtocolLogic.java:328)
>     at org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic.toPlcValue(ModbusProtocolLogic.java:232)
>     at org.apache.plc4x.java.modbus.tcp.protocol.ModbusTcpProtocolLogic.lambda$read$2(ModbusTcpProtocolLogic.java:104)
>     at org.apache.plc4x.java.spi.Plc4xNettyWrapper.decode(Plc4xNettyWrapper.java:187)
>     at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81)
>     at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88)
>     ... 23 more {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)