我正在使用BME280传感器跟踪Chris Pietschmann的Raspberry Pi气象站3.0教程:https://www.hackster.io/23021/weather-station-v-3-0-b8b8bc。
在MainPage中,他调用ReadTemperture(或传感器的任何其他寄存器)来写出返回的值。
我在Debug.Writeline()中获取System.Threading.Tasks.Task`1 [System.Single]以获取所有值...温度,湿度,压力和海拔高度。
我在方法中添加了一条写字符,我得到了正确的值,所以我从传感器中读取...我只是无法将其返回主页并阅读它。
看起来我在异步方面遗漏了什么?
这是MainPage:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace IoT_BME280_Temp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
DispatcherTimer _timer;
const float seaLevelPressure = 1026.00f; //was 1022.00
BME280Sensor _bme280 = new BME280Sensor();
public MainPage()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
await _bme280.Initialize();
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(5);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private void _timer_Tick(object sender, object e)
{
try
{
var temp = _bme280.ReadTemperature();
Debug.WriteLine("Temp: {0} deg C", temp); // Results in: Temp: System.Threading.Tasks.Task`1[System.Single] deg C
var humidity = _bme280.ReadHumidity();
var pressure = _bme280.ReadPressure();
var altitude = _bme280.ReadAltitude(seaLevelPressure);
}
catch
{
Debug.WriteLine("Cannot read values from sensor...");
}
}
}
}
这是来自BME280Sensor类的ReadTemprature():
public async Task<float> ReadTemperature()
{
//Make sure the I2C device is initialized
if (!init) await Begin();
//Read the MSB, LSB and bits 7:4 (XLSB) of the temperature from the BME280 registers
byte tmsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_MSB);
byte tlsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_LSB);
byte txlsb = ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_XLSB); // bits 7:4
//Combine the values into a 32-bit integer
Int32 t = (tmsb << 12) + (tlsb << 4) + (txlsb >> 4);
//Convert the raw value to the temperature in degC
double temp = BME280_compensate_T_double(t);
Debug.WriteLine("Temp: {0} deg C", temp); // This results in the correct temperature value...
//Return the temperature as a float value
return (float)temp;
}
提前致谢!
答案 0 :(得分:1)
你得到一个Task
,因为当你同步调用它时,ReadTemperature
会返回。要获取任务的结果而不是任务本身,您需要使用await
调用方法,并将_timer_Tick
更改为async
:
private async void _timer_Tick(object sender, object e)
{
try
{
var temp = await _bme280.ReadTemperature();
Debug.WriteLine("Temp: {0} deg C", temp);
var humidity = _bme280.ReadHumidity();
var pressure = _bme280.ReadPressure();
var altitude = _bme280.ReadAltitude(seaLevelPressure);
}
catch
{
Debug.WriteLine("Cannot read values from sensor...");
}
}