Skip to content
Merged
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
30 changes: 15 additions & 15 deletions src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,27 @@ 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 &&
System.getProperty(SerialNativeInterface.PROPERTY_JSSC_NO_TIOCEXCL.toLowerCase()) == null);
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1297,7 +1297,7 @@ public void run() {
break;
}
if(sendEvent){
eventListener.serialEvent(new SerialPortEvent(portName, eventType, eventValue));
eventListener.serialEvent(new SerialPortEvent(SerialPort.this, eventType, eventValue));
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/jssc/SerialPortEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
*/
public class SerialPortEvent {

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;
Expand All @@ -44,17 +48,32 @@ public class SerialPortEvent {
public static final int ERR = 128;
public static final int RING = 256;

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 port that set off this event
*/
public SerialPort getPort(){
return port;
}

/**
* Getting port name which sent the event
*/
@Deprecated
public String getPortName() {
return portName;
return port.getPortName();
}

/**
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/jssc/SerialPortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}