Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modbus: AbstractReadTask: Enhance Parsing Response; exception catch #2879

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -58,7 +58,7 @@ public ExecuteState execute(AbstractModbusBridge bridge) {

return ExecuteState.OK;

} catch (OpenemsException e1) {
} catch (Exception e1) {
logError(this.log, e1, "Parsing Response failed.");
throw e1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openems.edge.bridge.modbus;

import static io.openems.edge.bridge.modbus.api.ModbusComponent.ChannelId.MODBUS_COMMUNICATION_FAILED;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand All @@ -24,6 +25,9 @@
import io.openems.edge.common.test.ComponentTest;
import io.openems.edge.common.test.TestUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class BridgeModbusTcpImplTest {

private static final int UNIT_ID = 1;
Expand All @@ -43,6 +47,8 @@ public void test() throws Exception {
var processImage = new SimpleProcessImage(UNIT_ID);
Register register100 = new SimpleRegister(123);
processImage.addRegister(100, register100);
Register register101 = new SimpleRegister(321);
processImage.addRegister(101, register101);
slave.addProcessImage(UNIT_ID, processImage);
slave.open();

Expand Down Expand Up @@ -91,15 +97,63 @@ public void test() throws Exception {
}
}

@Test
public void testTriggerLogIllegalArgumentException() throws Exception {
final ThrowingRunnable<Exception> sleep = () -> Thread.sleep(CYCLE_TIME);
var port = TestUtils.findRandomOpenPortOnAllLocalInterfaces();
ModbusSlave slave = null;
PrintStream originalOut = System.out;
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
try {
/*
* Open Modbus/TCP Slave
*/
slave = ModbusSlaveFactory.createTCPSlave(port, 1);
var processImage = new SimpleProcessImage(UNIT_ID);
Register register100 = new SimpleRegister(123);
Register register101 = new SimpleRegister(Integer.MAX_VALUE); // this will cause the IllegalArgumentException
processImage.addRegister(100, register100);
processImage.addRegister(101, register101);
slave.addProcessImage(UNIT_ID, processImage);
slave.open();
System.setOut(new PrintStream(outContent));
/*
* Instantiate Modbus-Bridge
*/
var sut = new BridgeModbusTcpImpl();
var test = new ComponentTest(sut) //
.activate(MyConfigTcp.create() //
.setId("modbus0") //
.setIp("127.0.0.1") //
.setPort(port) //
.setInvalidateElementsAfterReadErrors(1) //
.setLogVerbosity(LogVerbosity.NONE) //
.build());
test.addComponent(new MyModbusComponent("device0", sut, UNIT_ID));
test //
.next(new TestCase() //
.onAfterProcessImage(sleep)); //
assertTrue(outContent.toString().contains("IllegalArgumentException"));

} finally {
if (slave != null) {
slave.close();
System.setOut(originalOut);
System.out.println(outContent);
}
}
}

private static class MyModbusComponent extends DummyModbusComponent {

public MyModbusComponent(String id, AbstractModbusBridge bridge, int unitId) throws OpenemsException {
super(id, bridge, unitId, ChannelId.values());
}

public enum ChannelId implements io.openems.edge.common.channel.ChannelId {
REGISTER_100(Doc.of(OpenemsType.INTEGER)); //

REGISTER_100(Doc.of(OpenemsType.INTEGER)), //
REGISTER_101(Doc.of(OpenemsType.SHORT)), //
;
private final Doc doc;

private ChannelId(Doc doc) {
Expand All @@ -116,7 +170,8 @@ public Doc doc() {
protected ModbusProtocol defineModbusProtocol() {
return new ModbusProtocol(this, //
new FC3ReadRegistersTask(100, Priority.HIGH, //
m(ChannelId.REGISTER_100, new UnsignedWordElement(100)))); //
m(ChannelId.REGISTER_100, new UnsignedWordElement(100)),
m(ChannelId.REGISTER_101, new UnsignedWordElement(101)))); //
}

}
Expand Down