Commapi serial port close/open

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 3
Reputation: bufo is an unknown quantity at this point 
Solved Threads: 0
bufo bufo is offline Offline
Newbie Poster

Commapi serial port close/open

 
0
  #1
Nov 24th, 2008
Hi,

I am developing a Java application that uses a serial port to communicate. The communication part is working fine but if I close the serialport using the close method on the SerialPort object and then try to open it again using the open method on a CommPortIdentifier object I get a runtime exception saying "Device busy". If I restart the application the port is opened without problems.

Should I do something else besides calling close on the SerialPort object before trying to open it again?

My platform is Sparc Solaris 10 and I'm using Sun's Commapi.

Thanks in advance,

Bufo
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Commapi serial port close/open

 
0
  #2
Nov 24th, 2008
> "Device busy"

Most probably this means that the device is still in use i.e. all the communication channels opened to the device have not been properly closed. In case you are playing around with streams obtained obtained from the port, you should try closing them. But then again, this is just a speculation since I haven't done any serial port programming. Feel free to post the relevant piece of code, I'll try and see what can be done.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: bufo is an unknown quantity at this point 
Solved Threads: 0
bufo bufo is offline Offline
Newbie Poster

Re: Commapi serial port close/open

 
0
  #3
Nov 25th, 2008
Thanks s.o.s. I do close the streams I have gotten from the port.

Here is some code.

Port initialization:
(The exception happens at line 20 where I call open on a CommPortIdentifier object.)

  1.  
  2. portList = CommPortIdentifier.getPortIdentifiers(); //enumeration
  3.  
  4. boolean found = false;
  5. while (portList.hasMoreElements()) {
  6. portId = (CommPortIdentifier) portList.nextElement();
  7. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL && portId.getName().equals(portname)) {
  8. found = true;
  9. break;
  10. }
  11. }
  12.  
  13. if (!found) {
  14. LogHandler.getLogger().error("Specified serial port could not be found.");
  15. return false;
  16. }
  17.  
  18.  
  19. try {
  20. port = (SerialPort) portId.open("Comm", 2000);
  21. } catch (Exception exc) {
  22. LogHandler.getLogger().error("Unable to open serial port.");
  23. LogHandler.getLogger().debug("Details: " + Formatter.getStackTraceString(exc));
  24. return false;
  25. }
  26.  
  27. try {
  28. inStream = port.getInputStream();
  29. portReader = new BufferedReader(new InputStreamReader(inStream), 1);
  30. outStream = port.getOutputStream();
  31. } catch (IOException exc) {
  32. LogHandler.getLogger().error("Unable to get input and/or outputstreams from serialport.");
  33. LogHandler.getLogger().debug("Details: " + Formatter.getStackTraceString(exc));
  34. return false;
  35. }
  36.  
  37. port.notifyOnDataAvailable(true);
  38. port.notifyOnBreakInterrupt(true);
  39. port.notifyOnCTS(true);
  40. port.notifyOnCarrierDetect(true);
  41. port.notifyOnDSR(true);
  42. port.notifyOnFramingError(true);
  43. port.notifyOnOutputEmpty(true);
  44. port.notifyOnOverrunError(true);
  45. port.notifyOnParityError(true);
  46. port.notifyOnRingIndicator(true);
  47.  
  48.  
  49. try {
  50. port.setSerialPortParams(Settings.getIntParameter("baudrate"),
  51. Settings.getIntParameter("databits"),
  52. Settings.getIntParameter("stopbits"),
  53. Settings.getIntParameter("parity"));
  54. } catch (Exception exc) {
  55. LogHandler.getLogger().error("Unable to set serialport parameters.");
  56. LogHandler.getLogger().debug("Details: " + Formatter.getStackTraceString(exc));
  57. return false;
  58. }
  59.  
  60. try {
  61. port.addEventListener(this);
  62. } catch (TooManyListenersException exc) {
  63. LogHandler.getLogger().error("Unable to add serial port event listener. Too many listeners.");
  64. LogHandler.getLogger().debug("Details: " + Formatter.getStackTraceString(exc));
  65. return false;
  66. }
  67.  
  68. port.setDTR(true);
  69. return false;

And port closing:

  1. portReader.close();
  2. inStream.close();
  3. outStream.close();
  4. portReader=null;
  5. inStream=null;
  6. outStream=null;
  7. port.close();
  8. port.removeEventListener();
  9. port=null;

-Anguinus
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Commapi serial port close/open

 
0
  #4
Nov 25th, 2008
Though you did post the code, I am still unclear as to how this all fits in your current application flow. Without the flow, it would be kind of difficult to point out the fault. How about a test snippet which showcases your problem?

Anyways, try this snippet and let us know if you are still facing the same issue:
  1. Port port = null;
  2. for(int i = 0; i < 3; ++i) {
  3. port = (SerialPort)portId.open("comm", 2000);
  4. InputStream in = port.getInputStream();
  5. OutputStream out = port.getOutputStream();
  6. // write and read some junk data
  7. Thread.sleep(2000);
  8.  
  9. in.close();
  10. out.close();
  11. Thread.sleep(2000);
  12. }
If the above code works, you can be pretty sure it has got something to do with your application logic. If even your test code fails, post the stack trace with the sample code as it is.

Your very statement "If I restart the application the port is opened without problems." points to a possibility of a resource being held by the application.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: bufo is an unknown quantity at this point 
Solved Threads: 0
bufo bufo is offline Offline
Newbie Poster

Re: Commapi serial port close/open

 
0
  #5
Nov 28th, 2008
Thank you again, s.o.s. The code you posted worked without problems (I added a port.close()-call) and the port closed and opened fine in the loop.

The whole point of this exercise has been recovering from an error in the serial communication, e.g. unplugging/plugging the cable or remote end doing something funny.

The basic flow is:

1. catch I/O exception
2. close the port and streams
3. try to open the port again

Closing and opening works if there is no I/O error but if I produce an error the port stays busy until I stop and start the application.

I have tried a lot of different approaches but it seems that only shutting down the VM will free the port after an error.

-Wildpointer
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Commapi serial port close/open

 
0
  #6
Nov 28th, 2008
Though it seems like an obvious thing to ask, but are you closing your resources in `finally'?
  1. try {
  2. Port p = null;
  3. InputStream in = null;
  4. OutputStream out = null;
  5. try {
  6. // acquire streams and do something
  7. } finally {
  8. if(in != null) in.close();
  9. if(out != null) out.close();
  10. if(port != null) port.close();
  11. }
  12. } catch(Exception e) {
  13. e.printStackTrace();
  14. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC