Friday, May 21, 2010

Java RXTX close port solution




class CloseThread extends Thread
{
   public void run()
   {
      serialPort.removeEventListener();
      serialPort.close();
   }
}


public void closePort()
{

   try
   {
      if (serialPort != null)
      {
         serialPort.getInputStream().close();
         serialPort.getOutputStream().close();

         new CloseThread().start();

      }

   } catch (Exception e) {}
}
/*
Late Note(11.17.2011): I see that there are plenty of views on this article and after more than one year I realize that the context might look ambiguous. What I didn't mention before is that this is an inner class to the class in which you have the methods for communicating over the serial port, and in here serialPort is an object of the SerialPort class.
*/

1 comment:

Ricotool@gmx.net said...

Well, but how can you be sure the port really gets closed? All you got is blocking thread now... Video is cool though