purejavacomm icon indicating copy to clipboard operation
purejavacomm copied to clipboard

Exception "purejavacomm.PureJavaIllegalStateException: File descriptor is -1 < 0, maybe closed by previous error condition" while writing to serial port using purejava

Open deepakahuja opened this issue 9 years ago • 20 comments

Hi,

I am getting following exception while using purejavacomm API for serial port communication. I have a Test class which receive & also send some data over serial port. I have used a MOXA device to send data from one serial port & receive data on another serial port. At both the ends i.e. sender & receiver the same Test class is used. It does not fail in first attempt but after sending & receiving some data may be after 10/15 minutes. Until 10/15 minutes, both sender & receiver communicate with each other using same method but after that receiver usually stops & we can see following exception. The version that we are using of purejavacomm is 28 & JNA is 4.0. Please help.

purejavacomm.PureJavaIllegalStateException: File descriptor is -1 < 0, maybe closed by previous error condition at purejavacomm.PureJavaSerialPort.failWithIllegalStateException(PureJavaSerialPort.java:1289) at purejavacomm.PureJavaSerialPort.checkState(PureJavaSerialPort.java:1294) at purejavacomm.PureJavaSerialPort.access$0(PureJavaSerialPort.java:1292) at purejavacomm.PureJavaSerialPort$1.write(PureJavaSerialPort.java:631) at purejavacomm.PureJavaSerialPort$1.write(PureJavaSerialPort.java:657)

Regards Deepak

deepakahuja avatar Sep 30 '15 13:09 deepakahuja

Please turn on the logging to see the actual reason why the port is closed.

See here:

https://github.com/nyholku/purejavacomm/wiki/Trouble-shooting

nyholku avatar Sep 30 '15 13:09 nyholku

Hi,

First of all thanks for the prompt reply.

I turned on the logging but the sender & receiver threads stopped immediately with following exception. But this exception was with log level 3 or 4 or 5 or 6 or 7. With log level 1 or 2 we got the same result that is also mentioned below:

With log level 3 or 4 or 5 or 6 or 7 : 2015-09-30 17:56:52,523 INFO [com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test.jt workReceiver.InstrumentAdaptor] Driver start 2015-09-30 17:56:52,523 FATAL [com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test.jt workReceiver] Driver version string: com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test: 1.1(4)-1.1(1) 2015-09-30 17:56:53,101 FATAL [com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test.jt workReceiver.InstrumentAdaptor] Error initializing SerielPortCommunicationDevice. Port: COM11 java.util.IllegalFormatConversionException: x != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045) at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2748) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2702) at java.util.Formatter.format(Formatter.java:2488) at java.util.Formatter.format(Formatter.java:2423) at java.lang.String.format(String.java:2797) at jtermios.JTermios$JTermiosLogging.log(JTermios.java:633) at jtermios.JTermios.ioctl(JTermios.java:446) at purejavacomm.PureJavaSerialPort.(PureJavaSerialPort.java:1131) at purejavacomm.CommPortIdentifier.open(CommPortIdentifier.java:159) at com.csc.opuslab.is.drivers.communication.devices.SerialPortCommunicationDevice.init(SerialPortCommunicationDevice.java:116) at com.csc.opuslab.is.server.idmbeans.InstrumentAdaptor.internalStart(InstrumentAdaptor.java:113) at com.csc.opuslab.is.server.idmbeans.DriverAdaptor.start(DriverAdaptor.java:374) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75) at sun.reflect.GeneratedMethodAccessor105.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279) at com.sun.jmx.mbeanserver.StandardMBeanIntrospector.invokeM2(StandardMBeanIntrospector.java:112) at com.sun.jmx.mbeanserver.StandardMBeanIntrospector.invokeM2(StandardMBeanIntrospector.java:46) at com.sun.jmx.mbeanserver.MBeanIntrospector.invokeM(MBeanIntrospector.java:237) at com.sun.jmx.mbeanserver.PerInterface.invoke(PerInterface.java:138) at com.sun.jmx.mbeanserver.MBeanSupport.invoke(MBeanSupport.java:252) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$16.run(WLSMBeanServerInterceptorBase.java:449) at java.security.AccessController.doPrivileged(Native Method) at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.invoke(WLSMBeanServerInterceptorBase.java:447) at weblogic.management.mbeanservers.internal.JMXContextInterceptor.invoke(JMXContextInterceptor.java:263) at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase$16.run(WLSMBeanServerInterceptorBase.java:449) at java.security.AccessController.doPrivileged(Native Method) at weblogic.management.jmx.mbeanserver.WLSMBeanServerInterceptorBase.invoke(WLSMBeanServerInterceptorBase.java:447) at weblogic.management.mbeanservers.internal.SecurityInterceptor.invoke(SecurityInterceptor.java:444) at weblogic.management.jmx.mbeanserver.WLSMBeanServer.invoke(WLSMBeanServer.java:323) at com.csc.opuslab.is.server.idmbeans.DriverManager.startInstrument(DriverManager.java:867) at com.csc.opuslab.is.server.idmbeans.DriverManager.startDrivers(DriverManager.java:747) at com.csc.opuslab.is.server.startup.servlet.StartupServlet$DriversStarter.run(StartupServlet.java:336) at java.lang.Thread.run(Thread.java:744) 2015-09-30 17:56:53,102 INFO [com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test.jt workReceiver.CommunicationDevice] Closed

And with log level 1 or 2, we got failed with same exception as mentioned in my first comment but a little logging was there on weblogic console as mentioned below: <30-09-2015 18:05:55 CEST> <Notice> <WebLogicServer> <BEA-000329> <Started the WebLogic Server Administration Server "IsNjord64" for domain "IsNjord64Domain" running in production mode.> <30-09-2015 18:05:55 CEST> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to RUNNING.> <30-09-2015 18:05:55 CEST> <Notice> <WebLogicServer> <BEA-000360> <The server started in RUNNING mode.> log: instantiating jtermios.windows.JTermiosImpl log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event . . . log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: missed EV_RXCHAR event log: fail() class jtermios.windows.JTermiosImpl line 863, Windows GetLastError()= 87, The parameter is incorrect.

jtermios.windows.JTermiosImpl$Fail at jtermios.windows.JTermiosImpl$Port.fail(JTermiosImpl.java:100) at jtermios.windows.JTermiosImpl.select(JTermiosImpl.java:863) at jtermios.JTermios.select(JTermios.java:467) at purejavacomm.PureJavaSerialPort$3.run(PureJavaSerialPort.java:1222) at java.lang.Thread.run(Thread.java:744) log: select() or poll() returned -1, errno 24 Exception in thread "Thread-26" purejavacomm.PureJavaIllegalStateException: File descriptor is -1 < 0, maybe closed by previous error condition at purejavacomm.PureJavaSerialPort.failWithIllegalStateException(PureJavaSerialPort.java:1285) at purejavacomm.PureJavaSerialPort.checkState(PureJavaSerialPort.java:1290) at purejavacomm.PureJavaSerialPort.access$0(PureJavaSerialPort.java:1288) at purejavacomm.PureJavaSerialPort$1.write(PureJavaSerialPort.java:627) at purejavacomm.PureJavaSerialPort$1.write(PureJavaSerialPort.java:653) at com.csc.opuslab.is.drivers.communication.devices.SerialPortCommunicationDevice.send(SerialPortCommunicationDevice.java:391) at com.csc.opuslab.is.drivers.instrumentdrivers.Test.Test.run(Test.java:74) at java.lang.Thread.run(Thread.java:744)

deepakahuja avatar Oct 01 '15 13:10 deepakahuja

Hi, Is there any update on the issue reported? Regards Deepak

deepakahuja avatar Oct 07 '15 12:10 deepakahuja

Hi,

I've not had time to look at this and it will be some time before I do. Have you tried an earlier version like 0.0.22 after which a lot changes were made, Would be interesting to know if those changes are causing the problem.'

nyholku avatar Oct 07 '15 14:10 nyholku

Actually, something just caught my eye in the log you provided.

It appear that in JTermios.java line 446 there is a problem that only appears when logging is turned on and which renders the logging useless:

It now reads:

    log = log && log(5, "> ioctl(%d,%d,[%08X])\n", fd, cmd, Arrays.toString(data));
    int ret = m_Termios.ioctl(fd, cmd, data);
    log = log && log(3, "< ioctl(%d,%d,[%08X]) => %d\n", fd, cmd, Arrays.toString(data), ret);

it should be

    log = log && log(5, "> ioctl(%d,%d,[%s])\n", fd, cmd, Arrays.toString(data));
    int ret = m_Termios.ioctl(fd, cmd, data);
    log = log && log(3, "< ioctl(%d,%d,[%s]) => %d\n", fd, cmd, Arrays.toString(data), ret);

Note the %08X should be %s. Please do that change in the code and rerun with the log enabled. The log you provided is wrong/bad because of that bug.

br Kusti

nyholku avatar Oct 07 '15 14:10 nyholku

Hi,

is there any update on this issue? Since we are using this library in our development tool, it would be nice to know when we can expect an update, if ever ;)

We are facing the same problems on windows 10. There seem to be an exception from jtermios.windows.JTermiosImpl$Fail thrown when removing a device from the USB port.

It works for windows 7 for us.

Best regards, Thomas

tkutz avatar Nov 06 '15 15:11 tkutz

it'd be useful for us also to fix this issue. is there any progress? thanks

lfarkas avatar Dec 07 '15 15:12 lfarkas

I really don't have much time to work on this right now.

I would have appreciated it if 'deepakahuja ' had reported back with the log fix in place, now the log is useless.

Also it appears 'tkutz' has different problem because he/she has this problem when unpluggin a device.

Since I do NOT experience this problems (and I've got no Windows 10) it is hard for me to debug this.

Contributions in the form of debugging would be more than welcome, after all this is open source and Java so easy to debug, this is really the point of this library ... while I'm happy to work on this when I have the time, if I had problems with this and this was not my project I would just dig in deep to find the problem and not expect the author to do my work for me....

nyholku avatar Dec 07 '15 18:12 nyholku

Hi Guys,

Sorry! We put this ticket on hold for some priority work. So, do not have updated logs. But I will be back with logs as soon as we start working on this again.

deepakahuja avatar Dec 18 '15 06:12 deepakahuja

Closing this as there is nothing to do ATM. Please feel free to open anytime.

nyholku avatar Feb 25 '16 13:02 nyholku

I am having the same issue - please reopen this issue for discussion.

WolfgangFahl avatar Aug 16 '17 09:08 WolfgangFahl

Please provide more details.

nyholku avatar Aug 17 '17 03:08 nyholku

The exception is misleading and you might want to change it's wording. In my case it happened simply because there was no connection. I was using an USB to serial converter an the USB cable was not plugged in. I'll try to setup the circumstances again to see if things are reproducible. For the time being my workaround will be to display a different message to my users.

WolfgangFahl avatar Aug 17 '17 04:08 WolfgangFahl

How do you mean it is misleading, can you suggest a better wording?

nyholku avatar Aug 17 '17 06:08 nyholku

IMHO it's better to do it like logging frameworks do it. In the sourc code checkstate() is called at different points e.g. https://github.com/nyholku/purejavacomm/blob/master/src/purejavacomm/PureJavaSerialPort.java#L127

so the call is from addEventListener .. if you change the function to checkState(msg) and call it with checkState("addEventListener") you can now create a message like: IllegalStateException: can't addEventListener while closed (maybe by previous error condition)

So instead of giving the internals about the encoding of the error flag I'd give more information on the context. E.g. "can't write while closed ..."

WolfgangFahl avatar Aug 19 '17 13:08 WolfgangFahl

Is there some logging framework that can trace when a close is forced by an error condition?

WolfgangFahl avatar Aug 19 '17 13:08 WolfgangFahl

Now I understand your suggestion. But the same information is available from the stacktrace of isn't it?

Loggin can be enabled like this:

https://github.com/nyholku/purejavacomm/wiki/Trouble-shooting

nyholku avatar Aug 19 '17 18:08 nyholku

Did I miss something, the latest code doesnt seem to have this fix? or is the a new branch some place? The master branch doesnt seem to be setup for 1.0.2 even though maven has it listed.

swarwick avatar Nov 16 '18 20:11 swarwick

Fix for what, a lot things were discussed in this thread?

nyholku avatar Nov 17 '18 07:11 nyholku

The fix to change the error thrown to an IOException. Also the code files dont seem to reflect the change to 1.0.2 as the version, and the releases tab does not show that release. The pom.xml still reports 1.0.1 thus I was asking if the code moved to some other repo to got closed sourced for that version?

swarwick avatar Nov 19 '18 19:11 swarwick