[英]Python misreads serial data
我正在從Arduino讀取vPython中的串行數據,並在讀取行期間出現數據錯誤。 有時它連續讀取兩行,而這一次我想在照片中顯示時卻錯過了逗號。 代碼如下。
是什么原因造成的?
[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("");
}
您很可能會聽到噪音。 您可以嘗試調整波特率,也可以嘗試使用其他電纜。 Sparkfun對此問題有一些詳細的信息: https ://www.sparkfun.com/tutorials/215-另一種方法是根據所需的可靠性,使用以太網進行通信,而不是串行通信。 我認為您將以較低的波特率獲得更大的成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.