6
void connect ( String portName ) throws Exception
{
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    if ( portIdentifier.isCurrentlyOwned() )
    {
        System.out.println("Error: Port is currently in use");
    }
    else
    {
        System.out.println(portIdentifier.getCurrentOwner());
        CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

        if ( commPort instanceof SerialPort )
        {
            SerialPort serialPort = (SerialPort) commPort;
            serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

            InputStream in = serialPort.getInputStream();
            OutputStream out = serialPort.getOutputStream();

            (new Thread(new SerialReader(in))).start();
            (new Thread(new SerialWriter(out))).start();

        }
        else
        {
            System.out.println("Error: Only serial ports are handled by this example.");
        }
    }     
}

is giving

gnu.io.PortInUseException: Unknown Application
    at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)

i am using RXTX with Java in windows 7 home 64-bit.

Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90

6 Answers6

15

Check that /var/lock folder exist on your machine.


mkdir /var/lock
chmod go+rwx /var/lock

bmalets
  • 3,207
  • 7
  • 35
  • 64
3

Reboot the system / disable the port.
Actual problem is when the program runs port is opened and it didn't close after the program terminates.
it works.

Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
2

I ran into this problem because the port was actually in use. A previous instance of javaw.exe appeared in the Windows task manager, it hogged the port.

The reason why that previous java process hung was a hardware issue: When plugging the USB-2-serial converter that I happened to use into a USB-2 port, all worked fine. When plugged into a USB-3 port, RXTX CommPortIdentifier code would hang, and then subsequent instances of Java received the PortInUseException.

Kay
  • 527
  • 2
  • 8
0

I used Process Explorer to find a process with the handle \Device\PCISerial0 and closed the handle. If your com ports aren't on a PCI card, the name might be different.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
0

For Windows Open Task Manager under Eclipse (or your ide) find Java application. Right click on it -> End Task

Teo Mihaila
  • 134
  • 1
  • 2
  • 18
0

May be useful, I solved such problem by remove gateway from service and stop it , gateway is instance of SerialModemGateway.

Service.getInstance().stopService();
Service.getInstance().removeGateway(gateway);
gateway.stopGateway();

cepa
  • 11
  • 1