From 263c3b9b43340b74ad5d42fe9c880a18e92f3e3e Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Wed, 3 Jan 2018 11:59:15 -0600 Subject: [PATCH] Updated version numbers and added comments. --- .gitignore | 4 ++ .../com/fazecast/jSerialComm/SerialPort.java | 53 ++++++++++++++----- .../jSerialComm/SerialPortDataListener.java | 8 ++- .../fazecast/jSerialComm/SerialPortEvent.java | 6 +-- .../jSerialComm/SerialPortPacketListener.java | 6 +-- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 9fcafb9..ceb26b5 100644 --- a/.gitignore +++ b/.gitignore @@ -22,5 +22,9 @@ build.gradle.private .cproject .classpath +# IntelliJ IDEA Files # +.idea +*.iml + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index f4d5067..f0375ab 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -2,10 +2,10 @@ * SerialPort.java * * Created on: Feb 25, 2012 - * Last Updated on: Dec 05, 2016 + * Last Updated on: Jan 03, 2018 * Author: Will Hedgecock * - * Copyright (C) 2012-2017 Fazecast, Inc. + * Copyright (C) 2012-2018 Fazecast, Inc. * * This file is part of jSerialComm. * @@ -40,7 +40,7 @@ import java.util.Date; * This class provides native access to serial ports and devices without requiring external libraries or tools. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.4.0 + * @version 2.0.0 * @see java.io.InputStream * @see java.io.OutputStream */ @@ -374,11 +374,11 @@ public final class SerialPort { if (process == null) return false; - try { process.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); return false; } + try { process.waitFor(); } catch (InterruptedException e) { return false; } 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) { e.printStackTrace(); return false; } + try { Thread.sleep(500); } catch (InterruptedException e) { return false; } } } @@ -504,13 +504,34 @@ public final class SerialPort */ public final int writeBytes(byte[] buffer, long bytesToWrite) { return writeBytes(portHandle, buffer, bytesToWrite); } + /** + * Sets the BREAK signal on the serial control line. + */ public final void setBreak() { setBreak(portHandle); } + + /** + * Clears the BREAK signal from the serial control line. + */ public final void clearBreak() { clearBreak(portHandle); } + /** + * Sets the state of the RTS line to 1. + */ public final void setRTS() { setRTS(portHandle); } + + /** + * Clears the state of the RTS line to 0. + */ public final void clearRTS() { clearRTS(portHandle); } + /** + * Sets the state of the DTR line to 1. + */ public final void setDTR() { setDTR(portHandle); } + + /** + * Clears the state of the DTR line to 0. + */ public final void clearDTR() { clearDTR(portHandle); } // Default Constructor @@ -966,7 +987,8 @@ public final class SerialPort isListening = true; dataPacketIndex = 0; - serialEventThread = new Thread(new Runnable() { + serialEventThread = new Thread(new Runnable() + { @Override public void run() { @@ -1006,18 +1028,20 @@ public final class SerialPort byte[] newBytes = new byte[numBytesAvailable]; newBytesIndex = 0; bytesRemaining = readBytes(portHandle, newBytes, newBytes.length); - if(dataPacket.length == 0) { + if(dataPacket.length == 0) userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, newBytes.clone())); - } - else { - while (bytesRemaining >= (dataPacket.length - dataPacketIndex)) { + else + { + while (bytesRemaining >= (dataPacket.length - dataPacketIndex)) + { System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, dataPacket.length - dataPacketIndex); bytesRemaining -= (dataPacket.length - dataPacketIndex); newBytesIndex += (dataPacket.length - dataPacketIndex); dataPacketIndex = 0; userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, dataPacket.clone())); } - if (bytesRemaining > 0) { + if (bytesRemaining > 0) + { System.arraycopy(newBytes, newBytesIndex, dataPacket, dataPacketIndex, bytesRemaining); dataPacketIndex += bytesRemaining; } @@ -1068,7 +1092,7 @@ public final class SerialPort bytesRead = readBytes(portHandle, singleByteBuffer, 1L); if (bytesRead > 0) - return ((int) singleByteBuffer[0] & 0xFF); + return ((int)singleByteBuffer[0] & 0xFF); sleep1(); } @@ -1129,11 +1153,12 @@ public final class SerialPort { int bytesWritten = 0; - do { + do + { if (!isOpened) throw new IOException("This port appears to have been shutdown or disconnected."); - singleByteBuffer[0] = (byte) (b & 0xFF); + singleByteBuffer[0] = (byte)(b & 0xFF); bytesWritten += writeBytes(portHandle, singleByteBuffer, 1L); diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java b/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java index 0d12ab1..650dcbb 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortDataListener.java @@ -2,10 +2,10 @@ * SerialPortDataListener.java * * Created on: Feb 25, 2015 - * Last Updated on: Mar 12, 2015 + * Last Updated on: Jan 03, 2018 * Author: Will Hedgecock * - * Copyright (C) 2012-2017 Fazecast, Inc. + * Copyright (C) 2012-2018 Fazecast, Inc. * * This file is part of jSerialComm. * @@ -31,7 +31,7 @@ import java.util.EventListener; * This interface must be implemented to enable simple event-based serial port I/O. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.4.0 + * @version 2.0.0 * @see java.util.EventListener */ public interface SerialPortDataListener extends EventListener @@ -45,8 +45,6 @@ public interface SerialPortDataListener extends EventListener *      {@link SerialPort#LISTENING_EVENT_DATA_RECEIVED}
*      {@link SerialPort#LISTENING_EVENT_DATA_WRITTEN}
*

- * If you choose to listen for the {@link SerialPort#LISTENING_EVENT_DATA_RECEIVED} event, you should implement the sub-interface {@link SerialPortPacketListener} instead of this one. - *

* Two or more events may be OR'd together to listen for multiple events; however, if {@link SerialPort#LISTENING_EVENT_DATA_AVAILABLE} is OR'd with {@link SerialPort#LISTENING_EVENT_DATA_RECEIVED}, the {@link SerialPort#LISTENING_EVENT_DATA_RECEIVED} flag will take precedence. *

* Note that event-based write callbacks are only supported on Windows operating systems. As such, the {@link SerialPort#LISTENING_EVENT_DATA_WRITTEN} diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java b/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java index a761c34..c3cee45 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortEvent.java @@ -2,10 +2,10 @@ * SerialPortEvent.java * * Created on: Feb 25, 2015 - * Last Updated on: Mar 12, 2015 + * Last Updated on: Jan 03, 2018 * Author: Will Hedgecock * - * Copyright (C) 2012-2017 Fazecast, Inc. + * Copyright (C) 2012-2018 Fazecast, Inc. * * This file is part of jSerialComm. * @@ -31,7 +31,7 @@ import java.util.EventObject; * This class describes an asynchronous serial port event. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.4.0 + * @version 2.0.0 * @see java.util.EventObject */ public final class SerialPortEvent extends EventObject diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java b/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java index 1306095..846a340 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPortPacketListener.java @@ -2,10 +2,10 @@ * SerialPortPacketListener.java * * Created on: Feb 25, 2015 - * Last Updated on: Mar 12, 2015 + * Last Updated on: Jan 03, 2018 * Author: Will Hedgecock * - * Copyright (C) 2012-2017 Fazecast, Inc. + * Copyright (C) 2012-2018 Fazecast, Inc. * * This file is part of jSerialComm. * @@ -31,7 +31,7 @@ package com.fazecast.jSerialComm; * Note: Using this interface will negate any serial port read timeout settings since they make no sense in an asynchronous context. * * @author Will Hedgecock <will.hedgecock@fazecast.com> - * @version 1.4.0 + * @version 2.0.0 * @see com.fazecast.jSerialComm.SerialPortDataListener * @see java.util.EventListener */