Java 无法在Arduino上向XBee发送数据

Java 无法在Arduino上向XBee发送数据,java,serial-port,arduino,xbee,Java,Serial Port,Arduino,Xbee,我在Arduino Uno R3上有一个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.S

我在Arduino Uno R3上有一个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");
    }
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
输入gnu.io.CommPortIdentifier;
导入gnu.io.PortinUseeException;
导入gnu.io.SerialPort;
导入gnu.io.SerialPortEvent;
导入gnu.io.SerialPortEventListener;
导入gnu.io.UnsupportedComOperationException;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.Enumeration;
导入java.util.ToomanyListenerException;
公共类NewClass实现SerialPortEventListener{
串行端口串行端口;
输出流输出;
专用静态最终字符串端口_NAME=“COM10”/(XBee变送器)
专用缓冲读取器输入;
专用静态最终整数超时=2000;
专用静态最终整数数据率=9600;
//输入字符结束
//专用静态最终字节EOIC=3;
公共无效初始化(){
CommPortIdentifier portId=null;
枚举portEnum=CommPortIdentifier.getPortIdentifiers();
//首先,找到port_NAME中设置的串行端口的实例。
while(portEnum.hasMoreElements()){
CommPortIdentifier currPortId=(CommPortIdentifier)portEnum.nextElement();
if(currPortId.getName().equals(端口名称)){
portId=currPortId;
}
}
if(portId==null){
System.out.println(“找不到COM端口”);
返回;
}
试一试{
serialPort=(serialPort)portId.open(this.getClass().getName(),超时);
serialPort.setSerialPortParams(数据速率,serialPort.DATABITS_8,serialPort.STOPBITS_1,serialPort.PARITY_无);
//打开输入流
输入=新的BufferedReader(新的InputStreamReader(serialPort.getInputStream());
serialPort.addEventListener(此);
serialPort.notifyOnDataAvailable(true);
serialPort.setEndOfInputChar((字节)3);
}
catch(PortinUseeException |不支持的操作异常| IOException | ToomanyListenerException e){
System.err.println(例如toString());
}
}
公共同步作废关闭(){
如果(serialPort!=null){
serialPort.removeEventListener();
serialPort.close();
}
}
@凌驾
公共同步的void serialEvent(SerialPortEvent-oEvent){
if(oEvent.getEventType()==SerialPortEvent.DATA\u可用){
试一试{
字符输入线;
//dat=新的ArrayList();
if(input.ready()){
inputLine=(char)input.read();
睡眠(1500);
sendChar();
}
}
捕获(例外e){
System.err.println(例如toString());
System.out.println(“错误读取”);
}
}
/忽略所有其他事件类型,但您应该考虑其他事件类型。
}
公共同步的void sendChar(){
试一试{
睡眠(1500);
out=serialPort.getOutputStream();
System.out.println(“out不为空”);

对于(int i=0;i),您应该将问题分解为更小的、可管理的部分。(1)将Arduino的串行端口连接到运行终端仿真器程序的PC上,以调试您的代码。一旦这项工作正常,然后(2)将XBee连接到电脑,确保它已配置,并且可以进行射频传输。请参阅此内容,确保您没有卡在命令模式。“像发射机一样工作……像接收机一样工作”--XBee模块是进行发送和接收的无线电收发器。@sawdust:没错。XBee处于命令模式,这就是为什么它没有发送Arduino串行接收到的数据。问题解决了!