Java BufferedReader在从USB读取时跳过随机字符

Java BufferedReader在从USB读取时跳过随机字符,java,usb,arduino,buffer,bufferedreader,Java,Usb,Arduino,Buffer,Bufferedreader,我使用BufferedReader从USB网关读取数据,该网关定期从Arduino设备接收ZigBee网络帧 这就是框架的外观: ~f�}3�@v<-,R#}3�@v<--mac:0013A20040763C2D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35 我从USB端口读取的代码如下所示。它在自己的线程中运行 public void run() { Boolean keepRunning = true;

我使用BufferedReader从USB网关读取数据,该网关定期从Arduino设备接收ZigBee网络帧

这就是框架的外观:

~f�}3�@v<-,R#}3�@v<--mac:0013A20040763C2D -H:-25.80 -T:22.58 -L:2.6451 -N:100.00 -D:0.0290 -B:35 
我从USB端口读取的代码如下所示。它在自己的线程中运行

public void run() {
    Boolean keepRunning = true;
    BufferedReader br = new BufferedReader(new InputStreamReader(portReader.getInputStream()));
    String line;

    while (keepRunning) {
        try {   
            while ((line = br.readLine()) != null) {
                handleData(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
我使用的是RXTXcomm.jar,可以在这里找到

这是我打开端口的地方

while (!connected && !timedOut) {
                System.out.print("\n\nConnecting to " + portName);
                //Open Ports
                CommPort commPort = portIdentifier.open(this.getClass()
                        .getName(), 9600);

                //TODO Should we rule out other kinds?
                if (commPort instanceof SerialPort) {

                    //Pass the open port
                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort.enableReceiveTimeout(15000);

                    //Configure the port communication interface
                    serialPort.setSerialPortParams(bauds,
                            SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);

                    //Open a stream and read from the port
                    inputStream = serialPort.getInputStream();
                    int portBuffer = inputStream.read();

                    //Check if there is something in the buffer, which
                    //means that a connection was established
                    if (portBuffer > -1) {
                        connected = true;                           
                    } else {
                        System.err.println("Connection to " + portName
                                + " timed out");
                        serialPort.close();
                        inputStream.close();
                        timedOut = true;
                    }
                } else {
                    System.err
                            .println("Error: Only serial ports are handled by this application.");
                }
            }

对可能出现的问题有什么想法吗?

嗯,我不确定这是否是问题所在,但您不应该在串行端口字符设备上进行cat,因为它无法正确设置串行设备(具有正确的速度读数、奇偶校验等,并且没有缓冲)。始终使用
screen/dev/ttyUSB0 SPEED
,或
python-m serial.tools.miniterm
minicom

您所说的打印出来的垃圾实际上是帧数据。您可以在XBee手册中找到它是如何构建的(在API模式下标记新帧开始的
~
字符,后跟帧类型、长度、内容和CRC)。给定CRC,如果没有丢失位,您可以检查帧是否正确读取

我用C++编写了一个XBee数据报解析器:

并参加了一次C:

你可以用它来激发灵感,把事情做好


最后,我经常遇到XBee和主板之间的问题(使所有传入的数据报出错等等)。每次数据出错时,您可能需要重启和/或重新插入xbee加密狗(因此需要检查传入的数据报)。

是这两个特定的输出始终存在,还是有所不同?它以明显的随机方式变化。不过,总是有一些角色靠近同一个位置,这很有趣。这不是我所期望的串行连接的行为。是否尝试将字符串打印为二进制数组?我很好奇是否有任何模式。(另外,您可能必须使用@Telthien通知我;不确定原因,但标准通知没有触发)@Telthien Ok。这是在工作中发生的,所以我将在周一尝试。情况如何?谢谢,尽管问题发生在使用Java API读取时,而不是使用cat命令获取数据时。不过,我会尝试一下这些解析器。好吧,我只是说,在没有正确设置设备进行进一步读取的情况下,直接在端口上使用
cat
可能会有副作用,尽管我觉得奇怪,直接在串行chardev上使用cat不会出现问题。但我不是说这是你的麻烦的原因。你最好彻底检查XBee数据报,因为他们来了,并检查他们的CRC!
cat /dev/ttyUSB0
public void run() {
    Boolean keepRunning = true;
    BufferedReader br = new BufferedReader(new InputStreamReader(portReader.getInputStream()));
    String line;

    while (keepRunning) {
        try {   
            while ((line = br.readLine()) != null) {
                handleData(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
while (!connected && !timedOut) {
                System.out.print("\n\nConnecting to " + portName);
                //Open Ports
                CommPort commPort = portIdentifier.open(this.getClass()
                        .getName(), 9600);

                //TODO Should we rule out other kinds?
                if (commPort instanceof SerialPort) {

                    //Pass the open port
                    SerialPort serialPort = (SerialPort) commPort;
                    serialPort.enableReceiveTimeout(15000);

                    //Configure the port communication interface
                    serialPort.setSerialPortParams(bauds,
                            SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);

                    //Open a stream and read from the port
                    inputStream = serialPort.getInputStream();
                    int portBuffer = inputStream.read();

                    //Check if there is something in the buffer, which
                    //means that a connection was established
                    if (portBuffer > -1) {
                        connected = true;                           
                    } else {
                        System.err.println("Connection to " + portName
                                + " timed out");
                        serialPort.close();
                        inputStream.close();
                        timedOut = true;
                    }
                } else {
                    System.err
                            .println("Error: Only serial ports are handled by this application.");
                }
            }