YazanLpizra
YazanLpizra

Reputation: 540

Java CommPortIdentifier.getPortIdentifiers() returns null Windows 7

I am trying to read from my PC's serial ports and I keep getting an empty set of ports. I've looked up a number of other questions on stackoverflow, coderanch, and the oracle forums, and all of them mention needing to have the win32com.dll, comm.jar, and javax.comm.properties in specific directories in the jre folder. After doing all of that, I am still getting an empty set of ports. Here is my code (pretty much copied and pasted from online):

import java.io.*;
import java.util.*;

import javax.comm.*;



public class ReadWriteSerial implements SerialPortEventListener{

    private Enumeration portList = null;
    private CommPortIdentifier portId = null;
    private String defaultPort = null;
    private boolean portFound = false;
    private int baudRate = 0;
    private SerialPort serialPort = null;
    private DataInputStream is = null;
    private BufferedReader inStream;



    /********************************
     * Constructor for the base class
     * @param defaultPort
     * @param baudrate
     *******************************/
    public ReadWriteSerial(String defaultPort, int baudrate) throws NoSuchPortException{

        this.defaultPort = defaultPort;
        checkPorts();                     // Call a method for checking ports on the System

    }


    /************************************
     * This method checks the presence of
     * ports on the System, in affirmative
     * case initializes and configures it
     * to receive data on the serial port
     ***********************************/

    public void checkPorts() throws NoSuchPortException{

        /***************************************
         * Get a list of all ports on the system
         **************************************/
      //  CommPortIdentifier d = CommPortIdentifier.getPortIdentifier("COM1");
        portList =CommPortIdentifier.getPortIdentifiers();
        System.out.println("List of all serial ports on this system:");

        while(portList.hasMoreElements()){
            portId = (CommPortIdentifier)portList.nextElement();
            if(portId.getName().equals(defaultPort)){
                portFound = true;
                System.out.println("Port found on: " + defaultPort);

                initialize();       // If Port found then initialize the port

            }   
        }

        if(!portFound){
            System.out.println("No serial port found!!!");
        }
    }

    public void initialize(){
        /**********************
         * Open the serial port
         *********************/
        try{
            serialPort = (SerialPort)portId.open("Artificial Horizont", 2000);
        } catch (PortInUseException ex){
            System.err.println("Port already in use!");
        }

        // Get input stream
        try{
            is = new DataInputStream(serialPort.getInputStream());
        } catch (IOException e){
            System.err.println("Cannot open Input Stream " + e);
            is = null;
        }


        try{
            serialPort.setSerialPortParams(this.baudRate,
                                           SerialPort.DATABITS_8,
                                           SerialPort.STOPBITS_1,
                                           SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException ex){
            System.err.println("Wrong settings for the serial port: " + ex.getMessage());
        }


        try{
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex){
            System.err.println("Check the flow control setting: " + ex.getMessage());
        }

        // Add an event Listener
        try{
            serialPort.addEventListener(this);
        } catch (TooManyListenersException ev){
            System.err.println("Too many Listeners! " + ev);
        }

        // Advise if data available to be read on the port
        serialPort.notifyOnDataAvailable(true);
    }


    /**********************************
     * Method from interface definition
     * @param event
     *********************************/
    @Override
    public void serialEvent(SerialPortEvent event){

        inStream = new BufferedReader(new InputStreamReader(is), 5);
        String rawInput = null;

        switch(event.getEventType()){
        case SerialPortEvent.BI:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.FE:
        case SerialPortEvent.OE:
        case SerialPortEvent.PE:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;

        case SerialPortEvent.DATA_AVAILABLE:
            try {
                while((rawInput = inStream.readLine()) != null){
                    System.out.println(rawInput);
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!HERE I GET THE VALUE FROM THE SERIAL PORT AND THOSE MUST BE "VISIBLE" TO THE SUBPANEL CLASS IN ORDER AND RUN THE METHOD REPAINT!!!!!!!!!!!!!!!!!!!!!
*/
                }

                inStream.close();

            } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            break;

        default:
            break;

        }
    }

}

and my main class:

public class RunComReader {
    public static void main(String[] args) throws NoSuchPortException {
        ReadWriteSerial reader = new ReadWriteSerial("COM1", 2000);
    }
}

And those files are placed in the lib, bin, and lib/ext folders in this directory:

C:\Program Files (x86)\Java\jre1.8.0_31

What am I doing wrong?

Upvotes: 1

Views: 5135

Answers (2)

Hamid
Hamid

Reputation: 1

I am using JRE7 (1.7.0), but I still had this problem. Interestingly, it was solved after removing jdk from the list of installed JREs (Window/Preferences/Java/Installed JREs) in eclipse (even though it was not selected and jre7 was the active and selected one).

Upvotes: 0

code_poetry
code_poetry

Reputation: 341

Java 8 does not support javax.com lib any more. You can use jssc lib (https://code.google.com/p/java-simple-serial-connector/) for serial communication.

Upvotes: 1

Related Questions