Skip to content

Modify SerialPort to implement java.io.Closeable #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.8.1-experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/main/java/org/scream3r/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
*/
package org.scream3r.jssc;

import java.io.Closeable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;

import jssc.SerialNativeAccess;
import jssc.SerialNativeInterface;

/**
*
* @author scream3r
*/
public class SerialPort {
public class SerialPort implements Closeable {

private SerialNativeInterface serialInterface;
private SerialPortEventListener eventListener;
Expand Down Expand Up @@ -1079,11 +1081,15 @@ public boolean removeEventListener() throws SerialPortException {
}

/**
* Close port. This method deletes event listener first, then closes the port
* Close the serial port. Removes the event listener if one exists has been
* added, then closes the underlying native port.
*
* @return whether or not the operation has been successfully completed
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException
*
* @deprecated
* @see #close()
*/
public boolean closePort() throws SerialPortException {
checkPortOpened("closePort()");
Expand All @@ -1098,6 +1104,20 @@ public boolean closePort() throws SerialPortException {
return returnValue;
}

/**
* Close the serial port. Removes the event listener if one exists has been
* added, then closes the underlying native port.
*
* @throws SerialPortException
*/
public void close() throws SerialPortException {
boolean portClosed = closePort();
if(!portClosed){
throw new SerialPortException(portName, "close()",
SerialPortException.TYPE_CANT_CLOSE_PORT);
}
}

private EventThread eventThread;

private class EventThread extends Thread {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/scream3r/jssc/SerialPortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
*/
package org.scream3r.jssc;

import java.io.IOException;

/**
*
* @author scream3r, vogt31337@googlemail.com
*/
public class SerialPortException extends Exception {
public class SerialPortException extends IOException {

final static long serialVersionUID = 5843574354687324684l;

Expand Down Expand Up @@ -62,6 +64,10 @@ public class SerialPortException extends Exception {
* @since 2.3.0
*/
final public static String TYPE_INCORRECT_SERIAL_PORT = "Incorrect serial port";
/**
* @since 2.8.1
*/
final public static String TYPE_CANT_CLOSE_PORT = "Can't close native port";

private String portName;
private String methodName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
*/
package org.scream3r.jssc;

import java.io.IOException;

/**
*
* @author scream3r, vogt31337@googlemail.com
*/
public class SerialPortTimeoutException extends Exception {
public class SerialPortTimeoutException extends IOException {

final static long serialVersionUID = -1584357967321684324l;

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/scream3r/jssc/TestSerialPortRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.scream3r.jssc;

import junit.framework.TestCase;
import static junit.framework.TestCase.assertTrue;
import org.junit.Assert;

/**
Expand Down Expand Up @@ -34,7 +33,7 @@ public void testSerialPortRead() {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
serialPort.closePort();//Close serial port
serialPort.close();//Close serial port
} catch (SerialPortException ex) {
System.out.println(ex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/scream3r/jssc/TestSerialPortWrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testSerialPortWrite() {
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.writeBytes("This is a test string".getBytes());//Write data to port
serialPort.closePort();//Close serial port
serialPort.close();//Close serial port
} catch (SerialPortException ex) {
System.out.println(ex);
}
Expand Down