Skip to content
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

Issue75 serial event exits on hardware loss #172

Merged
merged 6 commits into from
May 9, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
.settings/
*~*
/.gradle/
/hs_err_pid26993.log
1 change: 1 addition & 0 deletions src/main/c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build
.project
.cproject
/Default/
2 changes: 1 addition & 1 deletion src/main/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LININCLUDE= -I./include -I./include/target -I$(JDKLOCATION)/include -I$(JDKLO
CCLIN32=gcc $(LININCLUDE) -I./include/ubuntu-multilib-fix -O3 -Wall -fmessage-length=0 -fPIC -m32 -MMD
LINKLIN32=gcc -m32 -shared

CCLIN64=gcc $(LININCLUDE) -O3 -Wall -fmessage-length=0 -fPIC -m64 -MMD
CCLIN64=gcc $(LININCLUDE) -O3 -Wall -fmessage-length=0 -fPIC -m64 -MMD
LINKLIN64=g++ -m64 -shared

CCLINARM32=arm-linux-gnueabi-gcc $(LININCLUDE) -O3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP
Expand Down
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv5.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv6.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv6_HF.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv7.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv7_HF.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv8.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_32/libNRJavaSerialv8_HF.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/ARM_64/libNRJavaSerialv8.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/x86_32/libNRJavaSerial.so
Binary file not shown.
Binary file modified src/main/c/resources/native/linux/x86_64/libNRJavaSerial.so
Binary file not shown.
Binary file modified src/main/c/resources/native/osx/libNRJavaSerial.jnilib
Binary file not shown.
Binary file modified src/main/c/resources/native/windows/x86_32/libNRJavaSerial.dll
Binary file not shown.
Binary file modified src/main/c/resources/native/windows/x86_64/libNRJavaSerial.dll
Binary file not shown.
10 changes: 9 additions & 1 deletion src/main/c/src/SerialImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3690,7 +3690,7 @@ JNIEXPORT jint JNICALL RXTXPort(nativeavailable)( JNIEnv *env,
jobject jobj )
{
int fd = get_java_var( env, jobj,"fd","I" );
int result;
int result=-1;
/*
char message[80];

Expand Down Expand Up @@ -4265,6 +4265,13 @@ JNIEXPORT void JNICALL RXTXPort(eventLoop)( JNIEnv *env, jobject jobj )
do{
report_time_eventLoop( );
do {
if(RXTXPort(nativeavailable)( env, jobj )<0){
report("eventLoop: Hardware Missing\n");
finalize_threads( &eis );
finalize_event_info_struct( &eis );
LEAVE("eventLoop:error");
return;
}
/* nothing goes between this call and select */
if( eis.closing )
{
Expand Down Expand Up @@ -4304,6 +4311,7 @@ JNIEXPORT void JNICALL RXTXPort(eventLoop)( JNIEnv *env, jobject jobj )
}
i = 0;
#endif /* WIN32 */

} while ( eis.ret < 0 && errno == EINTR );
if( eis.ret >= 0 )
{
Expand Down
106 changes: 59 additions & 47 deletions src/main/java/gnu/io/RXTXPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,54 @@ public class RXTXPort extends SerialPort
/* I had a report that some JRE's complain when MonitorThread
tries to access private variables
*/
/**
*/
class MonitorThread extends Thread
{
/** Note: these have to be separate boolean flags because the
SerialPortEvent constants are NOT bit-flags, they are just
defined as integers from 1 to 10 -DPL */
private volatile boolean CTS=false;
private volatile boolean DSR=false;
private volatile boolean RI=false;
private volatile boolean CD=false;
private volatile boolean OE=false;
private volatile boolean PE=false;
private volatile boolean FE=false;
private volatile boolean BI=false;
private volatile boolean Data=false;
private volatile boolean Output=false;

MonitorThread()
{
if (debug)
z.reportln( "RXTXPort:MontitorThread:MonitorThread()");
}
/**
* run the thread and call the event loop.
*/
public void run()
{
try {
if (debug)
z.reportln( "RXTXPort:MontitorThread:run()");
monThreadisInterrupted=false;
eventLoop();
eis = 0;
if (debug)
z.reportln( "eventLoop() returned, this is invalid.");
}catch(Throwable ex) {
HARDWARE_FAULT=true;
sendEvent(SerialPortEvent.HARDWARE_ERROR, true);
}
}
protected void finalize() throws Throwable
{
if (debug)
z.reportln( "RXTXPort:MonitorThread exiting");
}
}
protected boolean HARDWARE_FAULT=false;
protected final static boolean debug = false;
protected final static boolean debug_read = false;
protected final static boolean debug_read_results = false;
Expand All @@ -86,7 +133,7 @@ public class RXTXPort extends SerialPort
static
{
try {
z = new Zystem();
z = new Zystem(Zystem.SILENT_MODE);
} catch ( Exception e ) {}

if(debug )
Expand Down Expand Up @@ -662,7 +709,11 @@ public boolean sendEvent( int event, boolean state )

switch( event )
{
case SerialPortEvent.DATA_AVAILABLE:
case SerialPortEvent.HARDWARE_ERROR:
if( debug_events )
z.reportln( "HARDWARE_ERROR " +
monThread.Data + ")" );
break;case SerialPortEvent.DATA_AVAILABLE:
if( debug_events )
z.reportln( "DATA_AVAILABLE " +
monThread.Data + ")" );
Expand Down Expand Up @@ -756,6 +807,8 @@ public boolean sendEvent( int event, boolean state )
case SerialPortEvent.BI:
if( monThread.BI ) break;
return(false);
case SerialPortEvent.HARDWARE_ERROR:
break;
default:
System.err.println( "unknown event: " + event);
return(false);
Expand Down Expand Up @@ -846,7 +899,7 @@ public void removeEventListener()
SPEventListener = null;
return;
}
else if( monThread != null && monThread.isAlive() )
else if( monThread != null && monThread.isAlive() && !HARDWARE_FAULT)
{
if (debug)
z.reportln( " RXTXPort:Interrupt=true");
Expand Down Expand Up @@ -1058,7 +1111,7 @@ public void notifyOnBreakInterrupt( boolean enable )
/** Close the port */
private native void nativeClose( String name );

public void close()
public void close()
{
if (debug)
z.reportln( "RXTXPort:close( " + this.name + " )");
Expand All @@ -1076,8 +1129,8 @@ public void close()
return;
}
disableRs485();
setDTR(false);
setDSR(false);
if(!HARDWARE_FAULT) setDTR(false);
if(!HARDWARE_FAULT) setDSR(false);
if (debug)
z.reportln( "RXTXPort:close( " + this.name + " ) setting monThreadisInterrupted");
if ( ! monThreadisInterrupted )
Expand Down Expand Up @@ -1582,48 +1635,7 @@ public synchronized int available() throws IOException
}
}
}
/**
*/
class MonitorThread extends Thread
{
/** Note: these have to be separate boolean flags because the
SerialPortEvent constants are NOT bit-flags, they are just
defined as integers from 1 to 10 -DPL */
private volatile boolean CTS=false;
private volatile boolean DSR=false;
private volatile boolean RI=false;
private volatile boolean CD=false;
private volatile boolean OE=false;
private volatile boolean PE=false;
private volatile boolean FE=false;
private volatile boolean BI=false;
private volatile boolean Data=false;
private volatile boolean Output=false;

MonitorThread()
{
if (debug)
z.reportln( "RXTXPort:MontitorThread:MonitorThread()");
}
/**
* run the thread and call the event loop.
*/
public void run()
{
if (debug)
z.reportln( "RXTXPort:MontitorThread:run()");
monThreadisInterrupted=false;
eventLoop();
eis = 0;
if (debug)
z.reportln( "eventLoop() returned, eis is invalid.");
}
protected void finalize() throws Throwable
{
if (debug)
z.reportln( "RXTXPort:MonitorThread exiting");
}
}
/**
* A dummy method added so RXTX compiles on Kaffee
* @deprecated deprecated but used in Kaffe
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gnu/io/SerialPortEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class SerialPortEvent extends EventObject
public static final int PE =8;
public static final int FE =9;
public static final int BI =10;

public static final int HARDWARE_ERROR =11;
private boolean OldValue;
private boolean NewValue;
private int eventType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
app.name = nrjavaserial
app.version = 5.0.1
app.version = 5.0.3-pre
68 changes: 68 additions & 0 deletions test/src/test/ReadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.TooManyListenersException;

import gnu.io.NRSerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.Zystem;
public class ReadTest {
public static void main(String [] args) {

String port = "";
for(String s:NRSerialPort.getAvailableSerialPorts()){
System.out.println("Availible port: "+s);
port=s;
}

int baudRate = 115200;
NRSerialPort serial = new NRSerialPort(port, baudRate);
serial.connect();
DataInputStream ins = new DataInputStream(serial.getInputStream());
try {
serial.addEventListener(ev->{
if(ev.getEventType()==SerialPortEvent.DATA_AVAILABLE) {
//while(ins.available()==0 && !Thread.interrupted());// wait for a byte
try {
while(ins.available()>0) {// read all bytes

char b = (char) ins.read();
//outs.write((byte)b);
System.out.print(b);

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(ev.getEventType()==SerialPortEvent.HARDWARE_ERROR) {
System.out.println("Clean exit of hardware");
serial.disconnect();
}
});
} catch (TooManyListenersException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// DataOutputStream outs = new DataOutputStream(serial.getOutputStream());
// try{
// byte bytes[] = new byte[10];
// //while(ins.available()==0 && !Thread.interrupted());// wait for a byte
// while(!Thread.interrupted()) {// read all bytes
// //if(ins.available()>0) {
// //char b = (char) ins.read();
// int back = ins.read(bytes);
// //outs.write((byte)b);
// for(int i=0;i<back;i++)
// System.out.print((char)bytes[i]);
// //}
// Thread.sleep(5);
// }
// }catch(Exception ex){
// ex.printStackTrace();
// }
// serial.disconnect();
}
}