我正在从Arduino读取vPython中的串行数据,并在readline中获取数据错误。有时它会连续读取两行,而这次它在我试图在照片中显示时错过了一个逗号。代码如下。
是什么导致这种情况?
[IMG] http://i.imgur.com/cAw7De1.png
Python代码:
arduinoSerialData=serial.Serial('/dev/cu.usbmodem1421',115200)
while (1==1): #loops forever
rate (30) # tells vPython to run this loop (times/sec)
while(arduinoSerialData.inWaiting()==0):
pass #do nothing
sensorCallInfo = arduinoSerialData.readline()
print sensorCallInfo
dataNums = sensorCallInfo.split(',')
x1 = float(dataNums[0])
y1 = float(dataNums[1])
z1 = float(dataNums[2])
sysCal = int(dataNums[3])
gyroCal = int(dataNums[4])
accelCal = int(dataNums[5])
magCal = int(dataNums[6])
print x1, y1, z1, sysCal, gyroCal, accelCal, magCal
print
print
Arduino代码:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#define BNO055_SAMPLERATE_DELAY_MS (50)
imu::Vector<3> linearAccel;
uint8_t systemcal, gyrocal, accelcal, magcal = 0;
Adafruit_BNO055 bno = Adafruit_BNO055(55);
void setup(void)
{
Serial.begin(115200);
while (!Serial);
if (!bno.begin()) //checks for sensor to start
{
Serial.print("No sensor detected. Check wiring or I2C address.");
while (1);
}
bno.setExtCrystalUse(true);
}
void loop(void)
{
bno.getCalibration(&systemcal, &gyrocal, &accelcal, &magcal);
linearAccel = bno.getVector(Adafruit_BNO055::VECTOR_LINEARACCEL);
outputForPython();
delay(BNO055_SAMPLERATE_DELAY_MS);
}
void outputForPython()
{
Serial.print(linearAccel.x()); Serial.print(",");
Serial.print(linearAccel.y()); Serial.print(",");
Serial.print(linearAccel.z()); Serial.print(",");
Serial.print(systemcal, DEC); Serial.print(",");
Serial.print(gyrocal, DEC); Serial.print(",");
Serial.print(accelcal, DEC); Serial.print(",");
Serial.print(magcal, DEC); Serial.println("");
}
答案 0 :(得分:0)
你很可能会收到噪音。您可以尝试调整波特率或尝试使用其他电缆。 Sparkfun在这个问题上有一些很好的细节:https://www.sparkfun.com/tutorials/215 - 另一种方法是根据你需要的可靠性,使用以太网进行通信而不是串行。我认为你会以较低的波特率获得更多的成功。