gobernador
gobernador

Reputation: 5739

Port in use by "Port currently not owned"

I'm getting some strange results from the javax.comm library. I'm running this constructor:

SerialPort port;

public Scale(String porttotry) {
    CommPortIdentifier cpi = null;
    try {
        cpi = CommPortIdentifier.getPortIdentifier(porttotry);

        //Open Port and establish stream
        log.trace("Opening CPI: {}", cpi.getCurrentOwner());
        port = (SerialPort) cpi.open("My Application", 2000);
        log.trace("CPI opened");

        //... configuration stuff
    } catch (PortInUseException ex) {
        log.error("{}: Port in use by {}", porttotry, cpi.getCurrentOwner());
    } catch (NoSuchPortException ex) {
        log.error("No such port as {}", porttotry);
    }
}

What's really strange is the fact that CommPortIdentifier.open(String, int) appears to be throwing a PortInUseException when the port is not in use. The log output says

TRACE [10:19:03.147] Scale:72 Opening CPI: Port currently not owned
ERROR [10:19:05.149] Scale:98 COM4: Port in use by Port currently not owned

The first log line means to me that open() should succeed, but it doesn't. I've connected to this device before and gotten data from it. This is a strange new error. Does anybody have any idea what could cause this? My gut tells me that this is some tricky thing with Windows having possession issues. I'm open to any ideas and I'll provide more information if you need it.

Upvotes: 0

Views: 372

Answers (1)

gobernador
gobernador

Reputation: 5739

I can't speak for exactly why this strange error occurred, but I did find something of a solution. It seems that I was correct in assuming it was Windows being possessive. When I shut down, unplugged my device, powered on with the device still unplugged, then connected the device, the PortInUseException disappeared.

Upvotes: 1

Related Questions