Skip to content

Commit

Permalink
Showing 5 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public static void assertByteEquals(byte expected, byte actual) {
assertEquals(expectedHex, actualHex);
}

public static void assertByteEquals(byte[] expected, byte[] actual) throws Exception {
public static void assertByteEquals(byte[] expected, byte[] actual) throws IOException {
String expectedHex = cleanHexDump(dump(expected));
String actualHex = cleanHexDump(dump(actual));
assertEquals(expectedHex, actualHex);
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one

public class Junit5Backport {

public static void assertThrows(Class<? extends Exception> exception, Acceptor acceptor) {
public static void assertThrows(Class<? extends Exception> exception, Acceptor<? extends Exception> acceptor) {
try {
acceptor.accept();
fail("Expected exception " + exception + " not thrown.");
@@ -34,7 +34,7 @@ public static void assertThrows(Class<? extends Exception> exception, Acceptor a
}

@FunctionalInterface
public interface Acceptor {
void accept() throws Exception;
public interface Acceptor<T extends Exception> {
void accept() throws T;
}
}
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ private void encodeWriteRequest(PlcRequestContainer<PlcRequest, PlcResponse> msg
modbusRequest = new WriteMultipleRegistersRequest(registerModbusAddress.getAddress(), quantity, bytesToWrite);
} else {
byte[] register = produceRegisterValue(writeRequestItem.getValues());
int intToWrite = register[0] << 8 | register[1];
int intToWrite = register[0] << 8 | register[1] & 0xff;
modbusRequest = new WriteSingleRegisterRequest(registerModbusAddress.getAddress(), intToWrite);
}
} else if (address instanceof CoilModbusAddress) {
@@ -333,8 +333,8 @@ private byte[] produceCoilValues(List<?> values) throws PlcProtocolException {
} else {
throw new PlcRuntimeException("Unsupported datatype detected " + value.getClass());
}
byte coilToSet = (coilSet ? (byte) 1 : (byte) 0);
actualCoil = (byte) (actualCoil | coilToSet << i);
byte coilToSet = coilSet ? (byte) 1 : (byte) 0;
actualCoil = (byte) (actualCoil & 0xff | coilToSet << i);
i--;
if (i < 0) {
coils.add(actualCoil);
@@ -443,13 +443,15 @@ private <T> List<T> produceCoilValueList(RequestItem requestItem, Class<T> dataT
}
byteBuf.readBytes(bytes);
List<T> data = new LinkedList<>();
for (int i = 0, j = 0; data.size() < readRequestItem.getSize(); i++) {
if (i > 7) {
int bitIndex = 0;
int coilIndex = 0;
while (data.size() < readRequestItem.getSize()) {
if (bitIndex > 7) {
// Every 8 Coils we need to increase the access
j++;
i = 0;
coilIndex++;
bitIndex = 0;
}
boolean coilSet = (bytes[j] & (1L << i)) != 0;
boolean coilSet = (bytes[coilIndex] & 0xff & (1L << bitIndex)) != 0;
byte coilFlag = coilSet ? (byte) 1 : (byte) 0;
if (dataType == Boolean.class) {
@SuppressWarnings("unchecked")
@@ -488,6 +490,7 @@ private <T> List<T> produceCoilValueList(RequestItem requestItem, Class<T> dataT
} else {
throw new PlcRuntimeException("Unsupported datatype detected " + dataType);
}
bitIndex++;
}
return data;
}
@@ -502,7 +505,7 @@ private <T> List<T> produceRegisterValueList(RequestItem requestItem, Class<T> d
for (int i = 0; i < readRequestItem.getSize(); i++) {
byte[] register = new byte[2];
byteBuf.readBytes(register);
int intValue = register[0] << 8 | register[1];
int intValue = register[0] << 8 | register[1] & 0xff;
if (dataType == Boolean.class) {
@SuppressWarnings("unchecked")
T itemToBeAdded = (T) Boolean.valueOf(intValue == 1);
Original file line number Diff line number Diff line change
@@ -61,12 +61,12 @@ Licensed to the Apache Software Foundation (ASF) under one
/**
* Class implementing the Connection handling for Siemens S7.
* The adressing of Values in S7 works as follows:
*
* <p>
* For adressing values from Datablocks the following syntax is used:
* <pre>
* DATA_BLOCKS/{blockNumer}/{byteOffset}
* </pre>
*
* <p>
* For adressing data from other memory segments like I/O, Markers, ...
* <pre>
* {memory area}/{byte offset}
@@ -225,8 +225,7 @@ public void close() throws PlcConnectionException {
// If the remote didn't close the connection within the given time-frame, we have to take
// care of closing the connection.
catch (TimeoutException e) {
logger.info("Remote didn't close connection within the configured timeout of " +
CLOSE_DEVICE_TIMEOUT_MS + "ms, shutting down actively.");
logger.info("Remote didn't close connection within the configured timeout of {}ms, shutting down actively.", CLOSE_DEVICE_TIMEOUT_MS, e);
channel.close();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -237,7 +236,7 @@ public void close() throws PlcConnectionException {
// Do some additional cleanup operations ...
// In normal operation, the channels event loop has a parent, however when running with
// the embedded channel for unit tests, parent is null.
if(channel.eventLoop().parent() != null) {
if (channel.eventLoop().parent() != null) {
channel.eventLoop().parent().shutdownGracefully();
}
}
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public static byte[] encodeData(Object[] values) throws PlcProtocolException {
if (length == 0) {
return new byte[]{};
}
byte[] result = null;
final byte[] result;
Class valueType = values[0].getClass();
if (valueType == Boolean.class) {
result = encodeBoolean(values, length);
@@ -97,7 +97,7 @@ public static byte[] encodeDouble(Object[] values, int length) {
for (int i = 0; i < length; i++) {
double doubleValue = (double) values[i];
long longValue = Double.doubleToLongBits(doubleValue);
result[(i * 8)] = (byte) ((longValue & 0xFF000000_00000000L) >> 56);
result[(i * 8)] = (byte) ((longValue & 0xFF000000_00000000L) >> 56);
result[(i * 8) + 1] = (byte) ((longValue & 0x00FF0000_00000000L) >> 48);
result[(i * 8) + 2] = (byte) ((longValue & 0x0000FF00_00000000L) >> 40);
result[(i * 8) + 3] = (byte) ((longValue & 0x000000FF_00000000L) >> 32);

0 comments on commit 7a38a36

Please sign in to comment.