C# Windows IoT核心读取值返回System.Threading.Tasks.Task`1[System.Single]而不是float

C# Windows IoT核心读取值返回System.Threading.Tasks.Task`1[System.Single]而不是float,c#,async-await,raspberry-pi2,windowsiot,C#,Async Await,Raspberry Pi2,Windowsiot,我正在关注Chris Pietschmann关于Raspberry Pi使用BME280传感器的气象站3.0教程: 在主页中,他调用ReadTemperature(或传感器的任何其他寄存器)来写出返回值 我在Debug.Writeline()中为所有值获取System.Threading.Tasks.Task`1[System.Single]。。。温度、湿度、压力和海拔高度 我在方法中添加了一条写线,得到了正确的值,所以我从传感器中读取。。。我就是不能把它放回主页去读 看起来我在异步端遗漏了什么

我正在关注Chris Pietschmann关于Raspberry Pi使用BME280传感器的气象站3.0教程:

在主页中,他调用ReadTemperature(或传感器的任何其他寄存器)来写出返回值

我在Debug.Writeline()中为所有值获取System.Threading.Tasks.Task`1[System.Single]。。。温度、湿度、压力和海拔高度

我在方法中添加了一条写线,得到了正确的值,所以我从传感器中读取。。。我就是不能把它放回主页去读

看起来我在异步端遗漏了什么

以下是主页:

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...");
        }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Threading.Tasks;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间IoT_BME280_温度
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
调度定时器;
常数浮动海平面压力=1026.00f;//为1022.00
bme280传感器_bme280=新的bme280传感器();
公共主页()
{
this.InitializeComponent();
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
等待_bme280.Initialize();
_计时器=新调度程序();
_timer.Interval=从秒开始的时间跨度(5);
_timer.Tick+=\u timer\u Tick;
_timer.Start();
}
私有无效\u计时器\u勾选(对象发送方,对象e)
{
尝试
{
var temp=_bme280.ReadTemperature();
Debug.WriteLine(“Temp:{0}摄氏度”,Temp);//结果为:Temp:System.Threading.Tasks.Task`1[System.Single]摄氏度
变量湿度=_bme280.read湿度();
var压力=_bme280.ReadPressure();
var高度=_bme280.读取高度(海平面压力);
} 
抓住
{
WriteLine(“无法从传感器读取值…”);
}
}
}
}
以下是来自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;
    }
public异步任务ReadTemperature()
{
//确保I2C设备已初始化
如果(!init)等待开始();
//从BME280寄存器读取温度的MSB、LSB和位7:4(XLSB)
字节tmsb=ReadByte((字节)eRegisters.BME280_REGISTER_TEMPDATA_MSB);
字节tlsb=读取字节((字节)eRegisters.BME280_寄存器_TEMPDATA_LSB);
byte txlsb=ReadByte((byte)eRegisters.BME280_REGISTER_TEMPDATA_XLSB);//位7:4
//将这些值合并为32位整数
int32t=(tmsb4);
//将原始值转换为以摄氏度为单位的温度
双温=BME280双温补偿双温(T);
Debug.WriteLine(“Temp:{0}deg C”,Temp);//这将产生正确的温度值。。。
//将温度作为浮点值返回
返回(浮动)温度;
}

提前谢谢

您将得到一个
任务
,因为当您同步调用它时,
ReadTemperature
将返回该任务。要获得任务的结果而不是任务本身,您需要使用
wait
调用该方法,并将
\u timer\u 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...");
    }
}

您需要等待对异步函数的调用hanks@Abion47我意识到有点太晚了。。。我知道,这是一个很好的理由,不能盲目地对所有事情使用
var
,而只能对类型明显不言而喻的变量使用。如果行read
Task temp=_bme280.ReadTemperature()这个问题会很突出。这也是遵循广泛采用的命名异步方法的约定的一个很好的理由
xxxxAsync()
;e、 g.
ReadTemperatureAsync()
在这种情况下。@sellotape说得很好。如果行是
int temp=_bme280.ReadTemperature(),那么问题也就不言而喻了,因为程序甚至不会编译。