Create SerialPort exceptions to differentiate between IO exceptions

This commit is contained in:
hedgecrw85 2018-08-08 12:54:37 -05:00
parent 3991057187
commit 610ad369a1
4 changed files with 44 additions and 28 deletions

View File

@ -2,7 +2,7 @@
* SerialPort.java * SerialPort.java
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Jul 20, 2018 * Last Updated on: Aug 08, 2018
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2018 Fazecast, Inc. * Copyright (C) 2012-2018 Fazecast, Inc.
@ -42,7 +42,7 @@ import java.util.Date;
* This class provides native access to serial ports and devices without requiring external libraries or tools. * This class provides native access to serial ports and devices without requiring external libraries or tools.
* *
* @author Will Hedgecock <will.hedgecock@fazecast.com> * @author Will Hedgecock <will.hedgecock@fazecast.com>
* @version 2.1.2 * @version 2.2.0
* @see java.io.InputStream * @see java.io.InputStream
* @see java.io.OutputStream * @see java.io.OutputStream
*/ */
@ -522,7 +522,7 @@ public final class SerialPort
private final native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1 private final native boolean getDSR(long portHandle); // Returns whether the DSR signal is 1
/** /**
* Returns the number of bytes available without blocking if {@link #readBytes} were to be called immediately * Returns the number of bytes available without blocking if {@link #readBytes(byte[], long)} were to be called immediately
* after this method returns. * after this method returns.
* *
* @return The number of bytes currently available to be read, or -1 if the port is not open. * @return The number of bytes currently available to be read, or -1 if the port is not open.
@ -1189,27 +1189,41 @@ public final class SerialPort
public SerialPortInputStream() {} public SerialPortInputStream() {}
@Override @Override
public final int available() throws IOException public final int available() throws SerialPortIOException
{ {
if (!isOpened) if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
return bytesAvailable(portHandle); return bytesAvailable(portHandle);
} }
@Override @Override
public final int read() throws IOException public final int read() throws SerialPortIOException
{ {
if (!isOpened)
throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
return (readBytes(portHandle, byteBuffer, 1L, 0) < 0) ? -1 : ((int)byteBuffer[0] & 0xFF); return (readBytes(portHandle, byteBuffer, 1L, 0) < 0) ? -1 : ((int)byteBuffer[0] & 0xFF);
} }
@Override @Override
public final int read(byte[] b) throws NullPointerException, IOException public final int read(byte[] b) throws NullPointerException, SerialPortIOException, SerialPortTimeoutException
{ {
return readBytes(portHandle, b, b.length, 0); // Perform error checking
if (b == null)
throw new NullPointerException("A null pointer was passed in for the read buffer.");
if (!isOpened)
throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
if (b.length == 0)
return 0;
// Read from the serial port
int numRead = readBytes(portHandle, b, b.length, 0);
if (numRead == 0)
throw new SerialPortTimeoutException("The read operation timed out before any data was returned.");
return numRead;
} }
@Override @Override
public final int read(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, IOException public final int read(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, SerialPortIOException, SerialPortTimeoutException
{ {
// Perform error checking // Perform error checking
if (b == null) if (b == null)
@ -1217,22 +1231,22 @@ public final class SerialPort
if ((len < 0) || (off < 0) || (len > (b.length - off))) if ((len < 0) || (off < 0) || (len > (b.length - off)))
throw new IndexOutOfBoundsException("The specified read offset plus length extends past the end of the specified buffer."); throw new IndexOutOfBoundsException("The specified read offset plus length extends past the end of the specified buffer.");
if (!isOpened) if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
if (len == 0) if ((b.length == 0) || (len == 0))
return 0; return 0;
// Read from the serial port // Read from the serial port
int numRead = readBytes(portHandle, b, len, off); int numRead = readBytes(portHandle, b, len, off);
if (numRead == 0) if (numRead == 0)
throw new IOException("The read operation timed out before any data was returned."); throw new SerialPortTimeoutException("The read operation timed out before any data was returned.");
return numRead; return numRead;
} }
@Override @Override
public final long skip(long n) throws IOException public final long skip(long n) throws SerialPortIOException
{ {
if (!isOpened) if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
byte[] buffer = new byte[(int)n]; byte[] buffer = new byte[(int)n];
return readBytes(portHandle, buffer, n, 0); return readBytes(portHandle, buffer, n, 0);
} }
@ -1246,26 +1260,26 @@ public final class SerialPort
public SerialPortOutputStream() {} public SerialPortOutputStream() {}
@Override @Override
public final void write(int b) throws IOException public final void write(int b) throws SerialPortIOException, SerialPortTimeoutException
{ {
if (!isOpened) if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
byteBuffer[0] = (byte)(b & 0xFF); byteBuffer[0] = (byte)(b & 0xFF);
int bytesWritten = writeBytes(portHandle, byteBuffer, 1L, 0); int bytesWritten = writeBytes(portHandle, byteBuffer, 1L, 0);
if (bytesWritten < 0) if (bytesWritten < 0)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
else if (bytesWritten == 0) else if (bytesWritten == 0)
throw new IOException("The write operation timed out before all data was written."); throw new SerialPortTimeoutException("The write operation timed out before all data was written.");
} }
@Override @Override
public final void write(byte[] b) throws NullPointerException, IOException public final void write(byte[] b) throws NullPointerException, SerialPortIOException, SerialPortTimeoutException
{ {
write(b, 0, b.length); write(b, 0, b.length);
} }
@Override @Override
public final void write(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, IOException public final void write(byte[] b, int off, int len) throws NullPointerException, IndexOutOfBoundsException, SerialPortIOException, SerialPortTimeoutException
{ {
// Perform error checking // Perform error checking
if (b == null) if (b == null)
@ -1273,16 +1287,16 @@ public final class SerialPort
if ((len < 0) || (off < 0) || ((off + len) > b.length)) if ((len < 0) || (off < 0) || ((off + len) > b.length))
throw new IndexOutOfBoundsException("The specified write offset plus length extends past the end of the specified buffer."); throw new IndexOutOfBoundsException("The specified write offset plus length extends past the end of the specified buffer.");
if (!isOpened) if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
if (len == 0) if (len == 0)
return; return;
// Write to the serial port // Write to the serial port
int numWritten = writeBytes(portHandle, b, len, off); int numWritten = writeBytes(portHandle, b, len, off);
if (numWritten < 0) if (numWritten < 0)
throw new IOException("This port appears to have been shutdown or disconnected."); throw new SerialPortIOException("This port appears to have been shutdown or disconnected.");
else if (numWritten == 0) else if (numWritten == 0)
throw new IOException("The write operation timed out before all data was written."); throw new SerialPortTimeoutException("The write operation timed out before all data was written.");
} }
} }
} }

View File

@ -32,7 +32,7 @@ import java.io.IOException;
* *
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt; * @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.2.0 * @version 2.2.0
* @see java.util.EventObject * @see java.io.IOException
*/ */
public final class SerialPortIOException extends IOException public final class SerialPortIOException extends IOException
{ {

View File

@ -32,7 +32,7 @@ import java.io.IOException;
* *
* @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt; * @author Will Hedgecock &lt;will.hedgecock@fazecast.com&gt;
* @version 2.2.0 * @version 2.2.0
* @see java.util.EventObject * @see java.io.IOException
*/ */
public final class SerialPortTimeoutException extends IOException public final class SerialPortTimeoutException extends IOException
{ {

View File

@ -212,7 +212,9 @@ public class SerialPortTest
System.out.print((char)in.read()); System.out.print((char)in.read());
in.close(); in.close();
in2.close(); in2.close();
} catch (Exception e) { e.printStackTrace(); } }
catch (SerialPortIOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
} }
System.out.println("\n\nEntering Java-based InputStream in Scanner mode and reading 200 lines\n"); System.out.println("\n\nEntering Java-based InputStream in Scanner mode and reading 200 lines\n");
ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0); ubxPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);