PySerial read()返回不正确的值

时间:2014-05-14 10:02:17

标签: python arduino raspberry-pi pyserial

我有一个带有标准模拟温度传感器的Arduino,它由Raspberry Pi的USB端口供电,还有一个获取所有传入值的Python脚本。

但是,我发现传入的值大约是10deg。

当我将其连接到计算机时,串行监视器会返回正确的值。这让我认为Raspberry Pi上的错误缩小了。

Python代码:

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
    temp = float(ser.readline())
    tempstring = "{:.1f}".format(temp)
    print(tempstring)
    f = open("/var/www/temp", "w")
    f.write(tempstring)
    f.close()

Arduino代码(为了更好的衡量标准)

const int thermometer = A0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  int sensorReading = analogRead(thermometer);
  float volts = (sensorReading/1024.0) * 5.0;
  float temp = (volts - 0.5) * 100.0;
  int largeTemp = temp * 100.0;


  Serial.println(largeTemp);
  delay(10000);
}

我不想打扰发送一个浮点数,因此int largeTemp = temp * 100.0;行。

0 个答案:

没有答案