From ffe3ff249ffd00c0a21edb2ccda63c720a22c9e4 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 2 Sep 2020 18:43:38 +0200 Subject: [PATCH] specific exception --- .../java/de/hottis/mbusMaster/MbusMaster.java | 16 +++++++++----- .../de/hottis/mbusMaster/MbusgwChild.java | 4 ++++ .../mbusMaster/MbusgwChildException.java | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/hottis/mbusMaster/MbusgwChildException.java diff --git a/src/main/java/de/hottis/mbusMaster/MbusMaster.java b/src/main/java/de/hottis/mbusMaster/MbusMaster.java index ad6f3e4..99af8b4 100644 --- a/src/main/java/de/hottis/mbusMaster/MbusMaster.java +++ b/src/main/java/de/hottis/mbusMaster/MbusMaster.java @@ -1,5 +1,7 @@ package de.hottis.mbusMaster; +import java.io.IOException; + import java.util.Properties; import org.apache.logging.log4j.LogManager; @@ -26,13 +28,17 @@ public class MbusMaster { mbusgw.start(); - mbusgw.sendRequest((byte)0x5b, (byte)80); + try { + mbusgw.sendRequest((byte)0x5b, (byte)80); - byte[] frame = mbusgw.collectResponse(); - for (byte x : frame) { - System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " "); + byte[] frame = mbusgw.collectResponse(); + for (byte x : frame) { + System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " "); + } + System.out.println(); + } catch (IOException e) { + logger.error("Error in Meterbus dialog: " + e.toString() + ", " + e.getMessage()); } - System.out.println(); System.out.println("Stopping mbusgw process"); mbusgw.stop(); diff --git a/src/main/java/de/hottis/mbusMaster/MbusgwChild.java b/src/main/java/de/hottis/mbusMaster/MbusgwChild.java index e35d91c..149d445 100644 --- a/src/main/java/de/hottis/mbusMaster/MbusgwChild.java +++ b/src/main/java/de/hottis/mbusMaster/MbusgwChild.java @@ -99,6 +99,10 @@ public class MbusgwChild { int responseCode = Byte.toUnsignedInt(header[0]); int responseLen = Byte.toUnsignedInt(header[1]); logger.debug("n: " + n + ", h: " + responseCode + ", l: " + responseLen); + if (responseCode != 0) { + logger.debug("Received error from child: " + responseCode); + throw new MbusgwChildException("Error " + responseCode + " from child"); + } byte[] frame = new byte[responseLen]; n = this.processInput.read(frame, 0, responseLen); logger.debug("frame completely read"); diff --git a/src/main/java/de/hottis/mbusMaster/MbusgwChildException.java b/src/main/java/de/hottis/mbusMaster/MbusgwChildException.java new file mode 100644 index 0000000..d0920b6 --- /dev/null +++ b/src/main/java/de/hottis/mbusMaster/MbusgwChildException.java @@ -0,0 +1,21 @@ +package de.hottis.mbusMaster; + +import java.io.IOException; + +public class MbusgwChildException extends IOException { + public MbusgwChildException() { + super(); + } + + public MbusgwChildException(String message, Throwable cause) { + super(message, cause); + } + + public MbusgwChildException(String message) { + super(message); + } + + public MbusgwChildException(Throwable cause) { + super(cause); + } +} \ No newline at end of file