From a9f1f1b4203303d3e934f3d2a5910260a3e22344 Mon Sep 17 00:00:00 2001 From: Tomas Nieboer Date: Fri, 30 Apr 2021 12:06:08 +0200 Subject: [PATCH 1/2] Pass the bytes along with the SerialPortEvent --- src/main/java/jssc/SerialPort.java | 12 ++++++++++-- src/main/java/jssc/SerialPortEvent.java | 11 ++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/jssc/SerialPort.java b/src/main/java/jssc/SerialPort.java index f078e4eb9..1c12cffab 100644 --- a/src/main/java/jssc/SerialPort.java +++ b/src/main/java/jssc/SerialPort.java @@ -1110,7 +1110,11 @@ public void run() { int[][] eventArray = waitEvents(); for(int i = 0; i < eventArray.length; i++){ if(eventArray[i][0] > 0 && !threadTerminated){ - eventListener.serialEvent(new SerialPortEvent(portName, eventArray[i][0], eventArray[i][1])); + try { + eventListener.serialEvent(new SerialPortEvent(readBytes(), portName, eventArray[i][0], eventArray[i][1])); + } catch (SerialPortException ignored) { + //TODO: How should this be handled? + } //FIXME /*if(methodErrorOccurred != null){ try { @@ -1297,7 +1301,11 @@ public void run() { break; } if(sendEvent){ - eventListener.serialEvent(new SerialPortEvent(portName, eventType, eventValue)); + try { + eventListener.serialEvent(new SerialPortEvent(readBytes(), portName, eventType, eventValue)); + } catch (SerialPortException ignored) { + //TODO: How should this be handled? + } } } } diff --git a/src/main/java/jssc/SerialPortEvent.java b/src/main/java/jssc/SerialPortEvent.java index 1953555ee..a11a51aff 100644 --- a/src/main/java/jssc/SerialPortEvent.java +++ b/src/main/java/jssc/SerialPortEvent.java @@ -30,6 +30,7 @@ */ public class SerialPortEvent { + private byte[] bytes; private String portName; private int eventType; private int eventValue; @@ -44,12 +45,20 @@ public class SerialPortEvent { public static final int ERR = 128; public static final int RING = 256; - public SerialPortEvent(String portName, int eventType, int eventValue){ + public SerialPortEvent(byte[] bytes, String portName, int eventType, int eventValue){ + this.bytes = bytes; this.portName = portName; this.eventType = eventType; this.eventValue = eventValue; } + /** + * Getting the bytes that set off this event + */ + public byte[] getBytes() { + return bytes; + } + /** * Getting port name which sent the event */ From 53692502e68c5a219953b2369fc9bc377b385d0e Mon Sep 17 00:00:00 2001 From: Tomas Nieboer Date: Tue, 4 May 2021 11:32:48 +0200 Subject: [PATCH 2/2] Refactor SerialPort(Event/Exception) to pass port instead of portName Makes usage of PortName in both cases deprecated. Removed internal usage of the deprecated code --- src/main/java/jssc/SerialPort.java | 34 +++++++++------------ src/main/java/jssc/SerialPortEvent.java | 26 +++++++++++----- src/main/java/jssc/SerialPortException.java | 31 +++++++++++++++---- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/main/java/jssc/SerialPort.java b/src/main/java/jssc/SerialPort.java index 5f537236a..a40a5858c 100644 --- a/src/main/java/jssc/SerialPort.java +++ b/src/main/java/jssc/SerialPort.java @@ -151,7 +151,7 @@ public boolean isOpened() { */ public boolean openPort() throws SerialPortException { if(portOpened){ - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_PORT_ALREADY_OPENED); + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_PORT_ALREADY_OPENED); } if(portName != null){ boolean useTIOCEXCL = (System.getProperty(SerialNativeInterface.PROPERTY_JSSC_NO_TIOCEXCL) == null && @@ -159,19 +159,19 @@ public boolean openPort() throws SerialPortException { portHandle = serialInterface.openPort(portName, useTIOCEXCL);//since 2.3.0 -> (if JSSC_NO_TIOCEXCL defined, exclusive lock for serial port will be disabled) } else { - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_NULL_NOT_PERMITTED);//since 2.1.0 -> NULL port name fix + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_NULL_NOT_PERMITTED);//since 2.1.0 -> NULL port name fix } if(portHandle == SerialNativeInterface.ERR_PORT_BUSY){ - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_PORT_BUSY); + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_PORT_BUSY); } else if(portHandle == SerialNativeInterface.ERR_PORT_NOT_FOUND){ - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_PORT_NOT_FOUND); + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_PORT_NOT_FOUND); } else if(portHandle == SerialNativeInterface.ERR_PERMISSION_DENIED){ - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_PERMISSION_DENIED); + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_PERMISSION_DENIED); } else if(portHandle == SerialNativeInterface.ERR_INCORRECT_SERIAL_PORT){ - throw new SerialPortException(portName, "openPort()", SerialPortException.TYPE_INCORRECT_SERIAL_PORT); + throw new SerialPortException(this, "openPort()", SerialPortException.TYPE_INCORRECT_SERIAL_PORT); } portOpened = true; return true; @@ -277,7 +277,7 @@ public boolean setEventsMask(int mask) throws SerialPortException { } boolean returnValue = serialInterface.setEventsMask(portHandle, mask); if(!returnValue){ - throw new SerialPortException(portName, "setEventsMask()", SerialPortException.TYPE_CANT_SET_MASK); + throw new SerialPortException(this, "setEventsMask()", SerialPortException.TYPE_CANT_SET_MASK); } if(mask > 0){ maskAssigned = true; @@ -876,7 +876,7 @@ private int[][] waitEvents() { */ private void checkPortOpened(String methodName) throws SerialPortException { if(!portOpened){ - throw new SerialPortException(portName, methodName, SerialPortException.TYPE_PORT_NOT_OPENED); + throw new SerialPortException(this, methodName, SerialPortException.TYPE_PORT_NOT_OPENED); } } @@ -1030,13 +1030,13 @@ private void addEventListener(SerialPortEventListener listener, int mask, boolea eventListenerAdded = true; } else { - throw new SerialPortException(portName, "addEventListener()", SerialPortException.TYPE_LISTENER_ALREADY_ADDED); + throw new SerialPortException(this, "addEventListener()", SerialPortException.TYPE_LISTENER_ALREADY_ADDED); } } /** * Create new EventListener Thread depending on the type of operating system - * + * * @since 0.8 */ private EventThread getNewEventThread() { @@ -1059,7 +1059,7 @@ private EventThread getNewEventThread() { public boolean removeEventListener() throws SerialPortException { checkPortOpened("removeEventListener()"); if(!eventListenerAdded){ - throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER); + throw new SerialPortException(this, "removeEventListener()", SerialPortException.TYPE_CANT_REMOVE_LISTENER); } eventThread.terminateThread(); setEventsMask(0); @@ -1069,7 +1069,7 @@ public boolean removeEventListener() throws SerialPortException { eventThread.join(5000); } catch (InterruptedException ex) { - throw new SerialPortException(portName, "removeEventListener()", SerialPortException.TYPE_LISTENER_THREAD_INTERRUPTED); + throw new SerialPortException(this, "removeEventListener()", SerialPortException.TYPE_LISTENER_THREAD_INTERRUPTED); } } } @@ -1110,11 +1110,11 @@ public void run() { int[][] eventArray = waitEvents(); for(int[] event : eventArray){ if(event[0] > 0 && !threadTerminated){ - eventListener.serialEvent(new SerialPortEvent(portName, event[0], event[1])); + eventListener.serialEvent(new SerialPortEvent(SerialPort.this, event[0], event[1])); //FIXME /*if(methodErrorOccurred != null){ try { - methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException("port", "method", "exception")}); + methodErrorOccurred.invoke(eventListener, new Object[]{new SerialPortException(SerialPort.this, "method", "exception")}); } catch (Exception ex) { System.out.println(ex); @@ -1297,11 +1297,7 @@ public void run() { break; } if(sendEvent){ - try { - eventListener.serialEvent(new SerialPortEvent(readBytes(), portName, eventType, eventValue)); - } catch (SerialPortException ignored) { - //TODO: How should this be handled? - } + eventListener.serialEvent(new SerialPortEvent(SerialPort.this, eventType, eventValue)); } } } diff --git a/src/main/java/jssc/SerialPortEvent.java b/src/main/java/jssc/SerialPortEvent.java index ae9719743..e37be380f 100644 --- a/src/main/java/jssc/SerialPortEvent.java +++ b/src/main/java/jssc/SerialPortEvent.java @@ -30,11 +30,14 @@ */ public class SerialPortEvent { - private byte[] bytes; - private String portName; + + private SerialPort port; private int eventType; private int eventValue; + @Deprecated + private String portName; + public static final int RXCHAR = 1; public static final int RXFLAG = 2; public static final int TXEMPTY = 4; @@ -45,25 +48,32 @@ public class SerialPortEvent { public static final int ERR = 128; public static final int RING = 256; - public SerialPortEvent(byte[] bytes, String portName, int eventType, int eventValue){ - this.bytes = bytes; + public SerialPortEvent(SerialPort port, int eventType, int eventValue){ + this.port = port; + this.eventType = eventType; + this.eventValue = eventValue; + } + + @Deprecated + public SerialPortEvent(String portName, int eventType, int eventValue){ this.portName = portName; this.eventType = eventType; this.eventValue = eventValue; } /** - * Getting the bytes that set off this event + * Getting the port that set off this event */ - public byte[] getBytes() { - return bytes; + public SerialPort getPort(){ + return port; } /** * Getting port name which sent the event */ + @Deprecated public String getPortName() { - return portName; + return port.getPortName(); } /** diff --git a/src/main/java/jssc/SerialPortException.java b/src/main/java/jssc/SerialPortException.java index a455da7dd..464c41d3b 100644 --- a/src/main/java/jssc/SerialPortException.java +++ b/src/main/java/jssc/SerialPortException.java @@ -61,11 +61,22 @@ public class SerialPortException extends Exception { */ final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port"; - private String portName; + private SerialPort port; private String methodName; private String exceptionType; - public SerialPortException(String portName, String methodName, String exceptionType){ + public SerialPortException(SerialPort port, String methodName, String exceptionType) { + super("Port name - " + port.getPortName() + "; Method name - " + methodName + "; Exception type - " + exceptionType + "."); + this.port = port; + this.methodName = methodName; + this.exceptionType = exceptionType; + } + + @Deprecated + private String portName; + + @Deprecated + public SerialPortException(String portName, String methodName, String exceptionType) { super("Port name - " + portName + "; Method name - " + methodName + "; Exception type - " + exceptionType + "."); this.portName = portName; this.methodName = methodName; @@ -75,21 +86,29 @@ public SerialPortException(String portName, String methodName, String exceptionT /** * Getting port name during operation with which the exception was called */ - public String getPortName(){ - return portName; + @Deprecated + public String getPortName() { + return port.getPortName(); + } + + /** + * Getting the port which threw the exception + */ + public SerialPort getPort() { + return port; } /** * Getting method name during execution of which the exception was called */ - public String getMethodName(){ + public String getMethodName() { return methodName; } /** * Getting exception type */ - public String getExceptionType(){ + public String getExceptionType() { return exceptionType; } }