我有下面的代碼,它從覆盆子pi上的傳感器讀取溫度。瞬態錯誤發生,deviceclient.sendmessage
代碼是異步的,如果我只刪除代碼的最後一行,發送消息的那一行,我永遠不會得到異常。 我的意思是這條線:
await deviceClient.SendEventAsync(message);
private async void InitializeSensors()
{
string calibrationData;
//_periodicTimer.Dispose();
// Inicializar el sensor bmp180
try
{
_bmp180 = new Bmp180Sensor();
await _bmp180.InitializeAsync();
calibrationData = _bmp180.CalibrationData.ToString(); //Retorna una cadena que representa al objeto actual.
if (_periodicTimer == null)
{
_periodicTimer = new Timer(this.TimerCallback, null, 0, readingInterval);
}
}
catch (Exception ex)
{
calibrationData = "Error de dispositivo! " + ex.Message;
}
var task = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
//calibrationDataTextBlock.Text = "";
});
}
public async void TimerCallback(object state)
{
string temperatureText;
// Leer datos del sensor
try
{
var sensorData = await _bmp180.GetSensorDataAsync(Bmp180AccuracyMode.UltraHighResolution);
temperatureText = sensorData.Temperature.ToString("");
//pressureText = sensorData.Pressure.ToString("F2");
temperatureText += "°C";
//pressureText += "hPa - " + BitConverter.ToString(sensorData.UncompestatedPressure);
var temperatureDataPoint = new
{
deviceKey = deviceKey,
deviceName = deviceName,
temperatura = sensorData.Temperature,
fecha = DateTime.Now
};
var messageString = JsonConvert.SerializeObject(temperatureDataPoint);
var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));
message.Properties["Ambiente"] = ambiente;
////temperatura.Text = temperatureText;
await deviceClient.SendEventAsync(message);
}
catch (Exception ex)
{
temperatureText = "Sensor Error: " + ex.Message;
//pressureText = "Sensor Error: " + ex.Message;
}
//// actualizaciones de la interfaz de usuario... deben ser invocados en el subproceso de interfaz de usuario
var task = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
{
temperatura.Text = temperatureText;
});
}
如果我刪除行,我可以看到,傳感器工作,因爲文本框與實際temmperature更新。
只有當我去掉了SendMessage函數線我得到的異常
我怎樣才能解決這個問題?
你得到了什麼異常? – Jackie
通用Windows應用程序未處理的類型爲'System.ExecutionEngineException'的異常 – gina