无法在Arduino上向XBee发送数据

时间:2013-08-29 10:08:35

标签: java serial-port arduino xbee

我在Arduino Uno R3上有一个XBee-PRO S1,它就像一个发送器,将数据发送到另一个XBee,这个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(),这是由于条件if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)引起的。此外,数据尚未发送。由于缺乏适当的文档,我不知道还有哪些其他事件类型。

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

0 个答案:

没有答案