nyholku / purejavacomm

Pure Java implementation of JavaComm SerialPort
http://www.sparetimelabs.com/purejavacomm/index.html
BSD 3-Clause "New" or "Revised" License
362 stars 146 forks source link

Opening results is a Port In Use Exception #136

Closed 11robert11 closed 3 years ago

11robert11 commented 3 years ago

I not able to open the connection. Every time it try it throws purejavacomm.PortInUseException: errno() == 13 at purejavacomm.PureJavaSerialPort.(PureJavaSerialPort.java:1093) at purejavacomm.CommPortIdentifier.open(CommPortIdentifier.java:159) at com.company.Main.foo(Main.java:27) at com.company.Main.main(Main.java:21) Exception in thread "main" java.lang.NullPointerException at com.company.Main.foo(Main.java:32) at com.company.Main.main(Main.java:21) Heres my code package com.company; import purejavacomm.*;

import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.TooManyListenersException; import java.util.logging.Level; import java.util.logging.Logger; import java.util.Enumeration;

public class Main { public static Logger logger = Logger.getLogger(Main.class.getName()); public static List serialPorts = new ArrayList(); public static InputStream inputStream; public static Thread readThread;

public static void main(String[] args) throws IOException {
    updateSerialPorts();
    foo();
}
public static void foo() throws IOException {
    CommPort serialPort = null;
    System.out.println(serialPorts.get(0).getName());
    try {
        serialPort = serialPorts.get(0).open("Reader", 166);
    } catch (PortInUseException e) {
        e.printStackTrace();
    }
    finally {
        serialPort.close();
    }
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    while (inputStream.available() > 0) {
        try {
            System.out.println(inputStream.read());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public static void updateSerialPorts() {
    serialPorts.clear();
    Enumeration<CommPortIdentifier> ports = CommPortIdentifier.getPortIdentifiers();
    while (ports.hasMoreElements()) {
        serialPorts.add(ports.nextElement());
        System.out.println(serialPorts.get(serialPorts.size() - 1).getName() + ":" + serialPorts.get(serialPorts.size() - 1).isCurrentlyOwned());
    }
    logger.log(Level.INFO, "Total Avalible Comm Ports: " + serialPorts.size());
    //logger.log(Level.INFO, "Total Avalible Com Ports: " + CommPortIdentifier.getPortIdentifiers().);
}

} //Users need to be in I also added user to the lock and uucp grounds

11robert11 commented 3 years ago

It turns out it was just improper file permissions.