我正在玩arduino。在项目内部,它有一个从温度传感器获取温度的项目。我说它并且写了下面的简单代码
const int sensorPin = A0;
void loop(){
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal / 1024.0)* 5.0;
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
}
在串行监视器中打印温度。我使用pySerial从arduino中获取温度,就像这样
try:
ser = serial.Serial('/dev/ttyACM0', 9600)
except serial.SerialException as se:
print se
exit()
while True:
temperature = ser.readline()
print "Temp from arduino", temperature
try:
temperature = float(temperature)
print "FLoat temperature", temperature
except ValueError as ve:
print ve
continue
但问题是,虽然来自arduino ide的串行监视器正常显示温度(例如22.3),但正常的浮点数是python的数字除以10
Temp from arduino 2.27
FLoat temperature 2.27
我该如何处理?
答案 0 :(得分:0)
它接缝除了温度之外的其他东西被Arduino打印到串行接口。尝试使用某个标记为温度添加前缀,并在python端解析它:
Serail.print('Temp:')
Serial.println(temperature);
在客户端:
input = ser.readline()
print "Input from arduino", input
try:
if input.startswith('Temp:')
temperature = float(input[len('Temp'):])
print "Float temperature", temperature