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

How to catch exception when disconnecting USB-COM device #108

Open
jreker opened this issue Aug 6, 2018 · 5 comments
Open

How to catch exception when disconnecting USB-COM device #108

jreker opened this issue Aug 6, 2018 · 5 comments

Comments

@jreker
Copy link

jreker commented Aug 6, 2018

Hello,

i am curently experimenting with PJC. I want to use it in a production enviroment.
My problem is, that the lib threws a exception when i disconnect my COM device ( just unplug the cable):

This is the exception i got:

jtermios.windows.JTermiosImpl$Fail
at jtermios.windows.JTermiosImpl$Port.fail(JTermiosImpl.java:100)
at jtermios.windows.JTermiosImpl.select(JTermiosImpl.java:924)
at jtermios.JTermios.select(JTermios.java:474)
at purejavacomm.PureJavaSerialPort$3.run(PureJavaSerialPort.java:1228)
at java.lang.Thread.run(Thread.java:748)

That's good, but i cannot catch the exception because it appears an another thread i think. Is there already a solution for this problem?

Thanks in advance.

@nyholku
Copy link
Owner

nyholku commented Aug 6, 2018 via email

@jreker
Copy link
Author

jreker commented Aug 7, 2018

I want to catch it, because i want to react when the device is disconnected to set a error message.

@nyholku
Copy link
Owner

nyholku commented Aug 7, 2018 via email

@jreker
Copy link
Author

jreker commented Aug 7, 2018

I tried to change your code but without luck. It was not working. The problem is the runnable in the constructor of the class "PureJavaSerialPort". I try to change it to callable but then the code inside the callable was not working as before.

My workaround now was that i created a watchdog thread and send periodically data to my serial device so that i will get a exception when it is not possible to send data to the device.

I was great when it is possible to integrate an event for unplug or change to code that the exception was rethrown.

Thanks
BR Hannes

@swarwick
Copy link

I also wanted some callback to know if the main thread failed. I also resorted to using an external watch dog of sorts but in my code I do a simple check of the NativeFileDescriptor

public boolean isRunning() {
return serialPort != null && ((PureJavaSerialPort) serialPort).getNativeFileDescriptor() >= 0; // check the native file descriptor is still valid
}

I used this in my manager class like this:

   checkRunningThread = new Thread(new Runnable() {

       @Override
       public void run() {            	
           int sleepMSValue = 100;
           int failedCount = 0;
           int retryCount = 10;
           int delayBetweenRetries = 1;
           int delayBetweenRetriesMS = delayBetweenRetries * 1000;
           while (shouldRun.get()) {                    
                   if (serialDevice != null && !serialDevice.isRunning()) {
                   	try {                        	
                   		failedCount++;
                   		witsProcessor.closeSerialConnection();
                   		sleepMSValue = delayBetweenRetriesMS;
  					} catch (StreamBaseException e) {
  					}
                   }
                   if (serialDevice != null && serialDevice.isRunning()) {
                   	failedCount = 0;
                   }
                   if (failedCount > 0 && failedCount < retryCount) {
                   	try {
  						createSerialConnection();
  						getLogStatusManager().sendLogAndStatusOutput(LogLevel.INFO, LogLevel.INFO, "Control", configElement, "Serial connection created");
  						failedCount = 0;
  						sleepMSValue = 100;
  					} catch (StreamBaseException e) {
  						getLogStatusManager().sendLogAndStatusOutput(LogLevel.ERROR, LogLevel.ERROR, "Control", configElement, "Error creating serial connection: " + e.getMessage(), e);									
  						failedCount++;
  					}
                   }
               
                   if (failedCount == retryCount) {
                   	getLogStatusManager().sendLogAndStatusOutput(LogLevel.ERROR, "Serial", configElement, "The serial port failed for config[" + configElement + "]");
                   	failedCount++;
                   }                    
               try {
                   Thread.sleep(sleepMSValue);
               } catch (InterruptedException e) {
               }
           }
       }
   });
   checkRunningThread.start();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants