我有一个正在读取数据并将其发送到我的计算机的arduino,然后由python脚本读取,以保存为csv文件。
但是在该行的末尾会弹出一个“\ r \ n”。我基本上希望所有这些值都存储为浮点数,而不是字符串。
这是我的py脚本
import serial
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
index = 0
ser = serial.Serial('/dev/ttyUSB0', 9600)
vol = 1.0
tempHL = []
tempWL = []
tempCL = []
timeL = []
humidL = []
dataT = timeL, tempWL, tempCL, tempHL, humidL
while True:
index +=1
#temp = float(ser.readline())
temp = ser.readline()
time, tempW, tempC, tempH, humidity = temp.split(",")
print "Time: ", time, " TempW: ", tempW, " TempC: ", tempC, " TempH: ", tempH, " Humidity: ", humidity
print
timeL.append(time)
tempWL.append(tempW)
tempCL.append(tempC)
tempHL.append(tempH)
humidL.append(humidity)
with open("/home/matthew/sketchbook/VaporPod/VaporPod/data.csv", 'wb') as f:
writer = csv.writer(f)
for t, tW, tC, tH, h in zip(dataT[0], dataT[1], dataT[2], dataT[3], dataT[4]):
writer.writerow([t, tW, tC, tH, h])
#print "Time: ", t, " TempWater: ", tW, " TempCube: ", tC, " tempHumidity: ", tH, " Humidity: ", h
#print
f.close()
这是我的Arduino代码。
void loop() {
monitor.setCursor(0, 1);
sensors.requestTemperatures();
//---from humidity sensor
float tempWater = sensors.getTempF(waterTemp);
float tempCube = sensors.getTempF(cubeTemp);
float humd = myHumidity.readHumidity();
float tempH = (myHumidity.readTemperature()) * 1.8 + 32.0;
//time
Serial.print(millis() / 1000);
Serial.print(",");
//Water TempC
Serial.print(tempWater);
//printTemperature(waterTemp);
Serial.print(",");
//---cube temp
Serial.print(tempCube);
//printTemperature(cubeTemp);
Serial.print(",");
//---tempC from humidity sensor
Serial.print(tempH, 1);
Serial.print(",");
//humidity
Serial.print(humd, 1);
Serial.print("\n");
delay(1000);
}
以下是已保存的csv文件的屏幕截图
答案 0 :(得分:1)
time, tempW, tempC, tempH, humidity = map(float,temp.split(","))
可能?