Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.plc4x.java.modbus.base.optimizer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.model.PlcTag;
Expand Down Expand Up @@ -192,33 +194,7 @@ protected PlcReadResponse processReadResponses(PlcReadRequest readRequest, Map<P
}
// Go through all responses till we find one where that contains the current tag's data.
for (Response response : responses.get(tagType)) {
if(modbusTag instanceof ModbusTagCoil) {
if(response.matchesCoil(modbusTag)) {
// If this response was invalid, return all associated addresses as equally invalid.
// TODO: Possibly it would be worth doing a single item request for each of these
// tags in order to find out which ones are actually invalid as if one item in the
// current request exceeds the address range, all items in this chunk will fail, even
// if only one element was invalid.
if(response.getResponseCode() != PlcResponseCode.OK) {
values.put(tagName, new DefaultPlcResponseItem<>(response.getResponseCode(), null));
break;
}

// Coils are read completely different from registers.
ModbusTagCoil coilTag = (ModbusTagCoil) modbusTag;

// Calculate the byte that contains the response for this Coil
byte[] responseData = response.getResponseData();
int bitPosition = coilTag.getAddress() - response.startingAddress;
int bytePosition = bitPosition / 8;
int bitPositionInByte = bitPosition % 8;
boolean isBitSet = (responseData[bytePosition] & (1 << bitPositionInByte)) != 0;
values.put(tagName, new DefaultPlcResponseItem<>(PlcResponseCode.OK, new PlcBOOL(isBitSet)));
break;
}
}
// Read a normal register.
else if (response.matchesRegister(modbusTag)) {
if (response.matchesRegister(modbusTag)) {
// If this response was invalid, return all associated addresses as equally invalid.
// TODO: Possibly it would be worth doing a single item request for each of these
// tags in order to find out which ones are actually invalid as if one item in the
Expand Down Expand Up @@ -407,9 +383,35 @@ public byte[] getResponseData() {

public byte[] getResponseDataForTag(ModbusTag modbusTag) {
byte[] itemData = new byte[modbusTag.getLengthBytes()];
System.arraycopy(responseData, (modbusTag.getAddress() - startingAddress) * 2, itemData, 0, modbusTag.getLengthBytes());
int value = 0;
switch(modbusTag.getDataType()) {
case BOOL: {
itemData = new byte[responseData.length];
for (int i= 0; i < responseData.length; i++){
itemData[i] = byteReverse(responseData[i]);
}
}
break;
default:
System.arraycopy(responseData,
(modbusTag.getAddress() - startingAddress) * 2,
itemData, 0, modbusTag.getLengthBytes());
}

return itemData;
}

public static byte byteReverse(byte x) {
byte b = 0;
for (int i = 0; i < 8; ++i) {
b<<=1;
b|=( x &1);
x>>=1;
}
return b;
}


}

protected interface TagFactory {
Expand Down