Add DataListener class which can handle exceptions and reset Interrupted state on threads when that may occur
This commit is contained in:
parent
fbb51c858b
commit
058a4dcc3a
|
@ -2,7 +2,7 @@
|
|||
* SerialPort.java
|
||||
*
|
||||
* Created on: Feb 25, 2012
|
||||
* Last Updated on: Jul 08, 2019
|
||||
* Last Updated on: Jul 23, 2019
|
||||
* Author: Will Hedgecock
|
||||
*
|
||||
* Copyright (C) 2012-2019 Fazecast, Inc.
|
||||
|
@ -51,7 +51,7 @@ import java.util.Date;
|
|||
public final class SerialPort
|
||||
{
|
||||
// Static initializer loads correct native library for this machine
|
||||
private static final String versionString = "2.5.1";
|
||||
private static final String versionString = "2.5.2";
|
||||
private static volatile boolean isAndroid = false;
|
||||
private static volatile boolean isUnixBased = false;
|
||||
private static volatile boolean isWindows = false;
|
||||
|
@ -395,7 +395,7 @@ public final class SerialPort
|
|||
|
||||
// Force a sleep to ensure that the port does not become unusable due to rapid closing/opening on the part of the user
|
||||
if (safetySleepTime > 0)
|
||||
try { Thread.sleep(safetySleepTime); } catch (Exception e) {}
|
||||
try { Thread.sleep(safetySleepTime); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
|
||||
// If this is an Android root application, we must explicitly allow serial port access to the library
|
||||
File portFile = isAndroid ? new File(comPort) : null;
|
||||
|
@ -425,7 +425,7 @@ public final class SerialPort
|
|||
try { process.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
|
||||
try { process.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
|
||||
try { process.getErrorStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
|
||||
try { Thread.sleep(500); } catch (InterruptedException e) { return false; }
|
||||
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -684,17 +684,18 @@ public final class SerialPort
|
|||
* <p>
|
||||
* Calling this function enables event-based serial port callbacks to be used instead of, or in addition to, direct serial port read/write calls or the {@link java.io.InputStream}/{@link java.io.OutputStream} interface.
|
||||
* <p>
|
||||
* The parameter passed into this method must be an implementation of either {@link SerialPortDataListener}, {@link SerialPortPacketListener}, or {@link SerialPortMessageListener}.
|
||||
* The parameter passed into this method must be an implementation of either {@link SerialPortDataListener}, {@link SerialPortDataListenerWithExceptions}, {@link SerialPortPacketListener}, or {@link SerialPortMessageListener}.
|
||||
* The {@link SerialPortMessageListener} interface <b>should</b> be used if you plan to use event-based reading of <i>delimited</i> data messages over the serial port.
|
||||
* The {@link SerialPortPacketListener} interface <b>should</b> be used if you plan to use event-based reading of <i>full</i> data packets over the serial port.
|
||||
* Otherwise, the simpler {@link SerialPortDataListener} may be used.
|
||||
* Otherwise, the simpler {@link SerialPortDataListener} or {@link SerialPortDataListenerWithExceptions} may be used.
|
||||
* <p>
|
||||
* Only one listener can be registered at a time; however, that listener can be used to detect multiple types of serial port events.
|
||||
* Refer to {@link SerialPortDataListener}, {@link SerialPortPacketListener}, and {@link SerialPortMessageListener} for more information.
|
||||
* Refer to {@link SerialPortDataListener}, {@link SerialPortDataListenerWithExceptions}, {@link SerialPortPacketListener}, and {@link SerialPortMessageListener} for more information.
|
||||
*
|
||||
* @param listener A {@link SerialPortDataListener}, {@link SerialPortPacketListener}, or {@link SerialPortMessageListener} implementation to be used for event-based serial port communications.
|
||||
* @param listener A {@link SerialPortDataListener}, {@link SerialPortDataListenerWithExceptions}, {@link SerialPortPacketListener}, or {@link SerialPortMessageListener} implementation to be used for event-based serial port communications.
|
||||
* @return Whether the listener was successfully registered with the serial port.
|
||||
* @see SerialPortDataListener
|
||||
* @see SerialPortDataListenerWithExceptions
|
||||
* @see SerialPortPacketListener
|
||||
* @see SerialPortMessageListener
|
||||
*/
|
||||
|
@ -817,7 +818,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -864,7 +865,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -929,7 +930,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configTimeouts(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -947,7 +948,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -965,7 +966,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -989,7 +990,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -1034,7 +1035,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -1058,7 +1059,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -1085,7 +1086,7 @@ public final class SerialPort
|
|||
|
||||
if (isOpened)
|
||||
{
|
||||
try { Thread.sleep(200); } catch (Exception e) {}
|
||||
try { Thread.sleep(200); } catch (Exception e) { Thread.currentThread().interrupt(); }
|
||||
configPort(portHandle);
|
||||
}
|
||||
}
|
||||
|
@ -1243,7 +1244,16 @@ public final class SerialPort
|
|||
@Override
|
||||
public void run()
|
||||
{
|
||||
while (isListening && isOpened) { try { waitForSerialEvent(); } catch (NullPointerException e) { isListening = false; } }
|
||||
while (isListening && isOpened)
|
||||
{
|
||||
try { waitForSerialEvent(); }
|
||||
catch (NullPointerException e)
|
||||
{
|
||||
isListening = false;
|
||||
if (userDataListener instanceof SerialPortDataListenerWithExceptions)
|
||||
((SerialPortDataListenerWithExceptions)userDataListener).catchException(e);
|
||||
}
|
||||
}
|
||||
isListening = false;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* SerialPortDataListenerWithExceptions.java
|
||||
*
|
||||
* Created on: Jul 11, 2019
|
||||
* Last Updated on: Jul 11, 2019
|
||||
* Author: Will Hedgecock
|
||||
*
|
||||
* Copyright (C) 2012-2019 Fazecast, Inc.
|
||||
*
|
||||
* This file is part of jSerialComm.
|
||||
*
|
||||
* jSerialComm is free software: you can redistribute it and/or modify
|
||||
* it under the terms of either the Apache Software License, version 2, or
|
||||
* the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, version 3 or above.
|
||||
*
|
||||
* jSerialComm is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of both the GNU Lesser General Public
|
||||
* License and the Apache Software License along with jSerialComm. If not,
|
||||
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.fazecast.jSerialComm;
|
||||
|
||||
/**
|
||||
* This interface must be implemented to enable simple event-based serial port I/O with a custom Exception callback.
|
||||
*
|
||||
* @author Will Hedgecock <will.hedgecock@fazecast.com>
|
||||
* @version 2.5.2
|
||||
* @see com.fazecast.jSerialComm.SerialPortDataListener
|
||||
* @see java.util.EventListener
|
||||
*/
|
||||
public interface SerialPortDataListenerWithExceptions extends SerialPortDataListener
|
||||
{
|
||||
/**
|
||||
* Must be overridden to handle any Java exceptions that occur asynchronously in this data listener.
|
||||
*/
|
||||
public abstract void catchException(Exception e);
|
||||
}
|
Loading…
Reference in New Issue