2

我想使用串口 API 连接到 USB MODEM。所以我使用了以下代码,即使连接了 USB MODEM,它也没有列出。

我在 Fedora 工作,安装了 RXTX 库,但问题就在那里..请帮助我。

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


public class SimpleWrite {

static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) throws NoSuchPortException {

    portList = CommPortIdentifier.getPortIdentifiers();
    //System.out.println("JVM Version: " +System.getProperty("java.runtime.version"));
    System.out.println(portList.hasMoreElements()); //portList.hasMoreElements()
    //portList.hasMoreElements()
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();

        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
            if (portId.getName().equals("/dev/ttyUSB0")) {
                try {
                    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                } catch (PortInUseException e) {
                }
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {
                }
                try {
                    serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {
                }
                try {
                    outputStream.write(messageString.getBytes());
                } catch (IOException e) {
                }
            }
        }
    }
}
}

主要问题是“ portList.hasMoreElements() 返回false

感谢您

4

0 回答 0