使用RXTX Java库向Arduino发送串行数据

使用RXTX Java库向Arduino发送串行数据,java,serial-port,arduino,rxtx,Java,Serial Port,Arduino,Rxtx,我有一些代码,它不断地从Arduino上读取一行串行数据。我将arduino设置为发送回它读入的任何数据,或者如果没有数据可读取,它将只发送“无数据”。我接收到Java控制台的“无数据”很好,Arduino代码在使用终端时按预期工作。然而,我无法让Java将任何数据发送回arduino,我尝试了各种方法,但没有成功:(理想情况下,另一种用于发送数据的串行事件方法会很好。(接收方法正在工作) 感谢您的帮助,代码如下所示: public class SerialComm implements Ser

我有一些代码,它不断地从Arduino上读取一行串行数据。我将arduino设置为发送回它读入的任何数据,或者如果没有数据可读取,它将只发送“无数据”。我接收到Java控制台的“无数据”很好,Arduino代码在使用终端时按预期工作。然而,我无法让Java将任何数据发送回arduino,我尝试了各种方法,但没有成功:(理想情况下,另一种用于发送数据的串行事件方法会很好。(接收方法正在工作)

感谢您的帮助,代码如下所示:

public class SerialComm implements SerialPortEventListener {
SerialPort serialPort;
    /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { 
        "/dev/tty.usbserial-A9007UX1", // Mac OS X
                    "/dev/ttyACM0", // Raspberry Pi
        "/dev/ttyUSB0", // Linux
        "COM3", // Windows
};
/**
* A BufferedReader which will be fed by a InputStreamReader 
* converting the bytes into characters 
* making the displayed results codepage independent
*/
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 115200;

public void initialize() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=input.readLine();
            System.out.println(inputLine);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
         //Trying to implement another serial event for sending data here
}

}

将草图上传到Arduino,该草图在序列上打印一些内容,并将其连接到计算机,然后运行java程序。

您解决了这个问题吗?