我正在尝试将 XBee 输出的片段分组到一个连接它们的变量中。我正在使用处理来编码和编译。我遇到的问题是输出(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);
}
}
}