portList.hasMoreElements()在串口中返回false

时间:2015-01-09 04:13:35

标签: java serial-port windows-8.1 at-command rxtx

我正在开发一个与串口和AT命令相关的应用程序,用于使用Java发送短信,我使用的是Windows8.1 64位,我的代码如下:

public static void main(String[] args) {
System.out.println("here");
    String line1 = "AT+CSMS=1\r\n";
    String line2 = "AT+CMGS=" + dest + "\r\n";
    String line3 = messageString + "\r\n";

    portList = CommPortIdentifier.getPortIdentifiers();

    System.out.println(portList);

    System.out.println(portList.hasMoreElements());

    while (portList.hasMoreElements()) {

        portId = (CommPortIdentifier) portList.nextElement();

        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals("COM3")) {
                System.out.println("SMS Sending....Port Found");
                try {
                    serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
                    SimpleWrite wr = new SimpleWrite(serialPort);

                } catch (PortInUseException e) {
                    System.out.println("Port In Use " + e);
                }
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {
                    System.out.println("Error writing to output stream " + e);
                }
                try {
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {
                }
                try {
                    outputStream.write(line1.getBytes());
                    outputStream.write(line1.getBytes());
                    outputStream.write(line2.getBytes());
                    outputStream.write(line3.getBytes());
                    outputStream.write(26);
                    outputStream.flush();
                } catch (Exception e) {
                    System.out.println("Error writing message " + e);
                }
            }
        }       
    }
}

这是我得到的输出如下:

C:\>javac SimpleWrite.java

C:\>java SimpleWrite
here
gnu.io.CommPortEnumerator@7852e922
false

我得到portList的价值。但portList.hasMoreElements()的值会返回false。由于这个问题,我无法继续。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我从链接http://mfizz.com/oss/rxtx-for-java下载了64位版本的jar。

按照以下步骤操作。

对于JDK安装:

复制RXTXcomm.jar ---> \ JRE \ lib中\分机

复制rxtxSerial.dll ---> \ JRE \ BIN

复制rxtxParallel.dll ---> \ JRE \ BIN

在我的项目中添加了jar,当我执行以下行时,我实现了

Enumeration portL = CommPortIdentifier.getPortIdentifiers();
System.out.println(portL);
System.out.println(portL.hasMoreElements());