Windows IoT 核心读取值返回 System.Threading.Tasks.Task`1[System.Single] 而不是浮点数
Windows IoT Core reading value returned System.Threading.Tasks.Task`1[System.Single] instead of float
我正在关注 Chris Pietschmann 的 Raspberry Pi 气象站 3.0 教程,使用 BME280 传感器:https://www.hackster.io/23021/weather-station-v-3-0-b8b8bc。
在 MainPage 中,他调用 ReadTemperture(或传感器的任何其他寄存器)来写出 returned 值。
我在 Debug.Writeline() 中得到 System.Threading.Tasks.Task`1[System.Single] 的所有值...温度、湿度、压力和高度。
我在方法中添加了一个写入行,我得到了正确的值,所以我正在从传感器读取...我只是不能 return 它返回到主页并读取它。
看起来我在异步方面遗漏了什么?
这是主页:
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 class 的 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;
}
提前致谢!
你会得到一个 Task
因为这就是你同步调用它时 ReadTemperature
returns 的结果。要获得任务的结果而不是任务本身,您需要使用 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...");
}
}
我正在关注 Chris Pietschmann 的 Raspberry Pi 气象站 3.0 教程,使用 BME280 传感器:https://www.hackster.io/23021/weather-station-v-3-0-b8b8bc。
在 MainPage 中,他调用 ReadTemperture(或传感器的任何其他寄存器)来写出 returned 值。
我在 Debug.Writeline() 中得到 System.Threading.Tasks.Task`1[System.Single] 的所有值...温度、湿度、压力和高度。
我在方法中添加了一个写入行,我得到了正确的值,所以我正在从传感器读取...我只是不能 return 它返回到主页并读取它。
看起来我在异步方面遗漏了什么?
这是主页:
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 class 的 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;
}
提前致谢!
你会得到一个 Task
因为这就是你同步调用它时 ReadTemperature
returns 的结果。要获得任务的结果而不是任务本身,您需要使用 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...");
}
}