我创建了一个UWP应用程序来接收另一个应用程序发送的消息。正如我在“ IoT设备”选项卡下检查的那样,消息成功到达了IoT中心。在本地计算机上运行并注释掉GPIO引脚代码时,也成功接收到该消息。但是,当在实际的Raspberry Pi上运行时,代码会引发“在意外时间调用方法”异常,如调试器的e变量所示。
Here is the exception that is thrown
我尝试过更改目标版本,更新和降级Azure客户端库,从头开始创建整个项目。
namespace Test
{
public sealed partial class MainPage : Page
{
const int LED_PIN = 5;
GpioPin pin;
GpioPinValue pinValue;
string connection = string.Format("HostName=FYPHub.azure-devices.net;DeviceId=Device;SharedAccessKey=7Cw6whjx4EU7s5nqR7PmljY/vOYLY9QMqG2fG+mM0Ig=");
DeviceClient deviceClient;
public MainPage()
{
this.InitializeComponent();
InitGPIO();
deviceClient = DeviceClient.CreateFromConnectionString(connection, TransportType.Amqp);
ReceiveMessage();
}
private async void ReceiveMessage()
{
while (true)
{
var message = await deviceClient.ReceiveAsync();
if (message == null)
continue;
try
{
var bytes = message.GetBytes();
var command = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
TBox.Text = "Command: " + command;
if (command == "On")
{
pinValue = GpioPinValue.High;
pin.Write(pinValue);
}
else
{
pinValue = GpioPinValue.Low;
pin.Write(pinValue);
}
await deviceClient.CompleteAsync(message);
}
catch (Exception) { }
}
}
private void InitGPIO()
{
GpioController gpio = GpioController.GetDefault();
pin = gpio.OpenPin(LED_PIN);
pinValue = GpioPinValue.Low;
pin.Write(pinValue);
pin.SetDriveMode(GpioPinDriveMode.Output);
}
}
}