【问题标题】:Raspberry Pi 3 running Windows 10 Core with custom program shows crossed-out x运行 Windows 10 Core 和自定义程序的 Raspberry Pi 3 显示划掉的 x
【发布时间】:2018-01-24 03:19:54
【问题描述】:

我有一个非常简单的项目,我的目标是在运行 Windows 10 IoT Core 的 Raspberry Pi 3 上通过直接方法调用(通过 Azure IoT 中心)来切换 LED。

在当前状态下,我编写的程序仅显示一个带有 x 的白框,与邮件信封不同,并且后端光控代码不响应直接方法调用。但是,当连接到 Visual Studio 2017 远程调试器时,UI 背后的代码似乎运行得非常好(或者我认为如此),所以我认为直接方法调用的失败是由于我的互联网连接(正如我在this question)。

然而,结果表明,UI 背后的代码正在请求将一点程序执行更新打印到文本框中,而不存在的 UI 从未响应,从而阻止程序继续打开 Azure IoT首先是集线器设备客户端。

与其完全放弃 UI,我还想弄清楚是什么阻止了我的 UI 做任何事情。奇怪的是,当我第一次尝试我的程序(减去 IoT 代码)时,UI 运行良好。 坦率地说,我完全不知道会突然出现什么问题(如果您想知道的话,Pi is 处于 headed 模式),所以我想我最好的做法是发布我的代码并看看其他人是否可以复制这些问题。

我没有将我所有的代码都转储到这篇文章中,因为有人必须将它重建到一个项目中,然后可能会丢失一些东西等等......我只是把它全部推到了一个 Git 存储库中,这在这里找到:

https://github.com/Hermanoid/UltimateLED2

我还有一些问题要问...首先,我忽略了在 repo 中泄露我的 Raspberry Pi 的设备密钥和 ID。这可能是一个(半可行的,或至少可能的)安全问题吗?

此外,我的 VS 错误列表中有大量版本不匹配警告。在其他项目中,我会简单地在 Web.xml 或 App.xml 文件中启动一些版本重定向,但是(“运行时指令”XML 文件除外)这些在该项目中不存在。我可以做些什么来解决这些问题?

谢谢!

卢卡斯·尼沃纳

【问题讨论】:

    标签: azure uwp raspberry-pi3 azure-iot-hub windows-iot-core-10


    【解决方案1】:

    应用程序的功能需要增强。由于应用程序现在需要将双向信息从它发送到 azure。它需要在应用程序清单中检查客户端和服务器

    【讨论】:

      【解决方案2】:

      另外对于Stuart Smith的回复,由于你在GitHub上提供的代码,由于Task方法在所有操作都完成后没有签名为已完成,会导致死锁。你可以参考this话题,也许你会了解Task.Wait 和await 之间的区别。我已经修改了如下代码,它工作正常。

      public sealed partial class MainPage : Page
      {
          const string DeviceId = "<deviceId>";
          const string DeviceKey = "<device primary key>";
          const string HubEndpoint = "<azure iothub host name>";
          const int LEDPinNumber = 5;
          GpioPin LEDPin;
          bool LEDPinState;
          Brush StatusNormalBrush;
          DeviceClient deviceClient;
      
          public MainPage()
          {
              this.InitializeComponent();
              StatusNormalBrush = StatusIndicator.Fill;
              if (!TryInitGPIO().Result)
              {
                  WriteMessage("GPIO initialization failed"); 
              }
              deviceClient = DeviceClient.Create(HubEndpoint,
                  AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey), TransportType.Mqtt_WebSocket_Only);
              deviceClient.SetMethodHandlerAsync("ToggleLED", new MethodCallback(ToggleLEDMethod), null);
          }
      
          private Task<MethodResponse> ToggleLEDMethod(MethodRequest methodRequest, object userContext)
          {
              WriteMessage("Recieved Direct Request to toggle LED"); 
              LEDPinState = !LEDPinState;
              UpdateLight();
              return Task.FromResult( new MethodResponse(Encoding.UTF8.GetBytes("{\"LightIs\":\"" + (LEDPinState ? "On" : "Off") + "\"}"), 200));
          }
      
          public Task<bool> TryInitGPIO()
          {
              GpioController gpioController = GpioController.GetDefault();
              if (gpioController == null)
              {
                  WriteMessage("This Device is not IoT friendly!  (No GPIO Controller found)", true);
                  return Task.FromResult(false);
              }
              if (gpioController.TryOpenPin(LEDPinNumber, GpioSharingMode.Exclusive, out LEDPin, out GpioOpenStatus openStatus))
              {
                  WriteMessage($"Output Pin ({LEDPinNumber}) Opened Successfully!!");
              }
              else
              {
                  WriteMessage($"Output Pin ({LEDPinNumber}) Failed to Open", true);
                  return Task.FromResult(false);
              }
      
              LEDPin.SetDriveMode(GpioPinDriveMode.Output);
              LEDPin.Write(GpioPinValue.High);
              LEDPinState = true;
              UpdateLight();
              WriteMessage("Output Pin initialized and on");
              return Task.FromResult(true);
          }
      
          private async void WriteMessage(string message, bool isError = false)
          {
              await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    StringBuilder sb = new StringBuilder(OutputBox.Text);
                    if (isError)
                    {
                        sb.AppendLine();
                        sb.AppendLine("*************ERROR**************");
                    }
                    sb.AppendLine(message);
                    if (isError)
                    {
                        sb.AppendLine("*************END ERROR**************");
                        sb.AppendLine();
                    }
                    OutputBox.Text = sb.ToString();
                });
      
          }
      
          private void ManualToggle_Click(object sender, RoutedEventArgs e)
          {
              WriteMessage("Recieved Manual Toggle");
              LEDPinState = !LEDPinState;
              UpdateLight();
          }
      
          private async void UpdateLight()
          {
              LEDPin.Write(LEDPinState ? GpioPinValue.High : GpioPinValue.Low);
              await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
               {
                   StatusIndicator.Fill = LEDPinState ? new SolidColorBrush(Colors.Red) : StatusNormalBrush;
               });
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 2021-02-17
        相关资源
        最近更新 更多