|
发表于 2014-4-15 16:46:19
|
显示全部楼层
kingone 发表于 2013-11-17 11:44
高手救命,如何把arduino中的float数据通过串口,传到processing中,并还原为float?????
我用的是mpu6050,输出数据时使用
sprintf(str, "%d,%d,%d,%d,%d,%d\n",ax,ay,az,gx,gy,gz);
Serial.print(str);
在processing中,使用回调函数来完成数据的读入,(试过其他方法,都不能有效的将这六个值读入,经常会发生少读或多读的情况)
void setup()
{
println("setup...");
size(VIEW_SIZE_X, VIEW_SIZE_Y, P3D);
myPort = new Serial(this, Serial.list()[0], 38400);
font = loadFont("CourierNewPSMT-48.vlw");
myPort.bufferUntil('\n'); //buffer until meet '\n', then call the event listener
}
再定义回调函数中计算数值
void serialEvent(Serial p) {
inputString = p.readString();
inputStringArr = split(inputString, ',');
if (inputStringArr.length ==6)
{
RwAcc[0] = float(inputStringArr[0]) / 256.0;
RwAcc[1] = float(inputStringArr[1])/ 256.0;
RwAcc[2] = float(inputStringArr[2])/ 256.0;
Gyro[0] = float(inputStringArr[3]) / 14.375;
Gyro[1] = float(inputStringArr[4]) / 14.375;
Gyro[2] = float(inputStringArr[5]) / 14.375;
}
else
{
println("Data Error");
}
}
这样就能读出完整的数据了。
参见http://guoyunhe.me/zh/2013/03/10/arduino-processing/ |
|