处理跳过彼此

时间:2013-08-01 15:09:15

标签: processing xbee

我正在尝试将XBee输出的片段分组为一个连接它们的变量。我正在使用Processing进行编码和编译。我遇到的问题是输出(println)正在跳过每隔一个字节(也许这是错误的术语)。因此i = 4但11的XBee输出应该如下所示:

  

0,19,162,0,64,121,230,206(这是从十六进制转换的XBee地址)。

但是println显示了这个:

  

19,0,121,206,125,1,0,3(开始进入输出的其他部分)。

后来,我通过使用数组尝试了不​​同的路径。它仍然会跳过其他所有条目,我发现它与我的检查有关= i == 126.是否有其他方法可以进行这样的检查?

将此绘图部分替换为下面的部分。它更容易遵循。这是一个结果相同的测试......

void draw() {
    if (myPort.available() > 21) {
        int[] XBeeAddr = new int[22];
        for (int i=0; i<22; i++) {
            XBeeAddr[i] = myPort.read();
            if (myPort.read == 126) {
                i=0;
            }
        println(XBeeAddr[0] + "," + XBeeAddr[1] + "," + XBeeAddr[2]);
    }
}

原始代码

import processing.serial.*;
import de.bezier.data.sql.*; // For SQLite database

SQLite db; // For SQLite database
Serial myPort;

void setup() {
    println(Serial.list());
    myPort = new Serial(this, Serial.list()[0],9600);

    // For SQLite database...
    size( 100, 100 );
    db = new SQLite( this, "test.db" );  // Open database file
    if ( db.connect() ) {
        String[] tableNames = db.getTableNames();
        db.query( "SELECT * FROM %s", tableNames[0] );
        while (db.next()) {
            TableOne t = new TableOne();
            db.setFromRow( t );
            println( t );
        }
    }
}

// For SQLite database
class TableOne {
    public String fieldOne;
    public int fieldTwo;
    public String toString () {
        return String.format("fieldOne: %s fieldTwo: %d", fieldOne, fieldTwo);
    }
}

void draw() {
    if (myPort.available() > 21) {
        int XBeeAddr1 = 0;
        int XBeeAddr2 = 0;
        int XBeeAddr3 = 0;
        int XBeeAddr4 = 0;
        int XBeeAddr5 = 0;
        int XBeeAddr6 = 0;
        int XBeeAddr7 = 0;
        int XBeeAddr8 = 0;

        for (int i=0; i<22; i++) {
            int inByte = myPort.read();
            if (inByte == 126) {
                i=0; // This resets the counter if XBee data was incomplete on the last run.
            }
            if (i == 4) {
                XBeeAddr1 = myPort.read();
            }
            if (i == 5) {
                XBeeAddr2 = myPort.read();
            }
            if (i == 6) {
                XBeeAddr3 = myPort.read();
            }
            if (i == 7) {
                XBeeAddr4 = myPort.read();
            }
            if (i == 8) {
                XBeeAddr5 = myPort.read();
            }
            if (i == 9) {
                XBeeAddr6 = myPort.read();
            }
            if (i == 10) {
                XBeeAddr7 = myPort.read();
            }
            if (i == 11) {
                XBeeAddr8 = myPort.read();
            }

            String XBeeAddrAll = XBeeAddr1 + "," + 
                                 XBeeAddr2 + "," + 
                                 XBeeAddr3 + "," + 
                                 XBeeAddr4 + "," + 
                                 XBeeAddr5 + "," + 
                                 XBeeAddr6 + "," + 
                                 XBeeAddr7 + "," + 
                                 XBeeAddr8;
            println(XBeeAddrAll);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通过将myPort.read == 126更改为if XBeeAddr[0] != 126, break来解决问题。后来我做了if XBeeAddr[0] == 126 println