Java CommPortIdentifier.getPortIdentifiers()返回null Windows 7

时间:2015-05-15 21:12:24

标签: java serial-port

我正试图从我的PC的串口读取,并且我不断得到一组空的端口。我在stackoverflow,coderanch和oracle论坛上查了很多其他问题,所有这些都提到需要在jre文件夹中的特定目录中安装win32com.dll,comm.jar和javax.comm.properties 。完成所有这些后,我仍然得到一组空的端口。这是我的代码(几乎从网上复制和粘贴):

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;

        }
    }

}

和我的主要课程:

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

这些文件放在此目录中的lib,bin和lib / ext文件夹中:

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

Java 8不再支持不支持 javax.com lib。 您可以使用jssc lib(https://code.google.com/p/java-simple-serial-connector/)进行串行通信。

答案 1 :(得分:0)

我正在使用JRE7(1.7.0),但我仍然有这个问题。有趣的是,它是在eclipse中从已安装的JRE(Window / Preferences / Java / Installed JRE)列表中删除jdk后解决的(即使它没有被选中,jre7是活动的和选择的JRE7)。