2

我在 Arduino Uno R3 上有一个XBee-PRO S1,它的作用就像一个发射器,将数据发送到另一个 XBee,它就像一个接收器来打开连接到它的 LED。这是我正在做的事情:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

public class NewClass implements SerialPortEventListener {

    SerialPort serialPort;
    OutputStream out;

    private static final String PORT_NAME = "COM10"; //(XBee Transmitter)
    private BufferedReader input;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;
    // End of input chars
    //private static final byte EOIC = 3;

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

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

        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // Open the input stream
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setEndOfInputChar((byte) 3);
        }
        catch (PortInUseException | UnsupportedCommOperationException | IOException | TooManyListenersException e) {
            System.err.println(e.toString());
        }
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    @Override
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                char inputLine;
                //dat = new ArrayList<Character>();
                if (input.ready()) {
                    inputLine = (char) input.read();
                    Thread.sleep(1500);
                    sendChar();
                }
            }
            catch (Exception e) {
                System.err.println(e.toString());
                System.out.println("Error reading");
            }
        }
        // Ignore all the other eventTypes, but you should consider the other ones.
    }

    public synchronized void sendChar() {

        try {
            Thread.sleep(1500);
            out = serialPort.getOutputStream();
                System.out.println("out is not null");
                for (int i = 0; i <= 900; i++) {
                    Thread.sleep(1500);
                    out.write((char) 'A');
                    out.flush();
                    System.out.println(i+") written-> A"); //For debugging
                }
        }
        catch (InterruptedException | IOException e) {

        }
    }


    public synchronized void cRead(char data) {
        if (data != ' ') {
            System.out.print(data);
            //getTimestamp();
        }
    }

    public static void main(String[] args) throws Exception {
        NewClass main = new NewClass();
        main.initialize();
        Thread t = new Thread() {
            @Override
            public void run() {
                //The following line will keep this application alive for 12 hours,
                //waiting for events to occur and responding to them (printing
                //incoming messages to console).
                try {
                    Thread.sleep(43200000);
                }
                catch (InterruptedException ie) {

                }
            }
        };
        t.start();
        System.out.println("Started");
    }
}

这段代码的一个问题是,当我在 COM 端口收到一些传入信号时,只有sendChar()由于 condition 的原因而被调用if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)。也没有发送数据。由于缺乏适当的文档,我不知道还有哪些其他事件类型。

我想要的是在不接收任何东西的情况下向 Arduino 发送数据。我做错了什么或错过了什么?

4

0 回答 0