Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to properly release a serial port?

807603Feb 8 2006 — edited Jan 27 2008
I have a rather large application, in which when the user hits a button, an existing open serial port connection is closed, and then a new one is opened (on the same COMM port).

However, when I try to re-open the COMM port, I found that it is still owned. I tried to create a simple example (below) without a GUI to show this behavior, but the example works fine.

I was wondering if anyone has ran into this problem before?

It seems that the serial port is never closed. I put a loop in my code which continues until the isCurrentlyOwned() call returns false, but it never happens (i.e. the loop is still running)

here is my test code, which incidentally works (hate it when this happens). I also assume the first port is the COMM port I am interested in (I only have 1 COMM port on my system).

Anyone have any ideas as to what might cause the serial port to not be released? thanks!
import javax.comm.SerialPort;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.*;
import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.ActionEvent;

public class SerialPortTest extends JFrame {

	private SerialPort serialPort = null;
    public static int BAUD_RATE = 57600;
	private PortThread portThread = null;

    private class PortThread implements Runnable {
	    private boolean polling = true;

	    protected void stopPolling() { polling = false; }

	    public void run() {
		    Enumeration portList = CommPortIdentifier.getPortIdentifiers();
		    List<SerialPort> serialPortList = new ArrayList<SerialPort>();

		    try {
			    CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();

			    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL
					    && !(portId.isCurrentlyOwned())) {
				    serialPortList.add((SerialPort) portId.open("Test", 1000));
			    }

			    if (serialPortList.size() == 0) {
				    serialPort = null;
				    System.err.println("No Ports in System");
			    }

			    for ( int i = 0; i < serialPortList.size(); i++) {
				    serialPortList.get(i).setSerialPortParams(BAUD_RATE,
						    SerialPort.DATABITS_8,
						    SerialPort.STOPBITS_1,
						    SerialPort.PARITY_NONE);
			    }
			    //for this test, just grab the first serial port
	            if(serialPortList.size() > 0) {
		            serialPort = serialPortList.get(0);
		            System.err.println("Serial Port Opened");
	            }

			    while(polling) {
				    Thread.sleep(1000);
			    }
		    }
		    catch (PortInUseException exc) {
			    exc.printStackTrace();
		    }
		    catch (UnsupportedCommOperationException exc) {
			    exc.printStackTrace();
		    }
		    catch(InterruptedException e) {
			    e.printStackTrace();
		    }
	    }
    };

	public SerialPortTest() {
		setupGUI();
	}

	private void setupGUI() {
		this.getContentPane().setLayout(new GridLayout(1,0));

		this.getContentPane().add(new JButton(openAction));
		this.getContentPane().add(new JButton(closeAction));
		this.pack();
		this.setVisible(true);
	}

	private Action openAction = new AbstractAction("Open Port") {
		public void actionPerformed(ActionEvent e) {
			System.err.println("Attempting to open port");
			portThread = new PortThread();
			new Thread(portThread).start();
		}
	};


	private Action closeAction = new AbstractAction("Close Port") {
		public void actionPerformed(ActionEvent e) {
			System.err.println("Closing Port");
			if (portThread != null) { portThread.stopPolling(); }
			if (serialPort != null) { serialPort.close(); }
			portThread = null;
			serialPort = null;
		}
	};


	public static void main(String[] args) {

		new SerialPortTest();

	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2008
Added on Feb 8 2006
7 comments
184 views