Raspberry Pi 3 running Windows 10 Core with custom program shows crossed-out x

梦想的初衷 提交于 2019-12-11 04:46:45

问题


I have a very simple project, in which I aim to be able to toggle an LED by means of a direct method call (through Azure IoT hub) on my Raspberry Pi 3, running Windows 10 IoT Core.

In it's current state, the program I wrote shows only a white box with an x in it, not unlike a mail envelope, and the back end light-controlling code does not respond to direct method calls. However, when connected to a Visual Studio 2017 remote debugger, the code behind the UI seems to run perfectly fine (or so I thought), so I assumed the failure of Direct Method call was due to my internet connection (as I asked about in this question).

Turns out, however, that the code behind the UI was making a request to print a little program-execution update into a text box, and the nonexistent UI never responded, thus preventing the program from continuing on to open the Azure Iot Hub Device Client in the first place.

Rather than just ditching the UI altogether, I'd like to figure out what is preventing my UI from doing, well, anything. What makes this odd is that, when I first tried out my program (minus the IoT Code) the UI worked just fine. Frankly, I haven't the foggiest idea what could suddenly be wrong (the Pi is in headed mode, if you were wondering), so I suppose my best course of action would be to publish my code and see if anyone else can replicate the issues.

Instead of dumping all my code into this post, where someone would have to reconstruct it into a project, and then potentially be missing something, etc... I've simply shoved it all into a Git repo, which can be found here:

https://github.com/Hermanoid/UltimateLED2

I have a couple asides to ask about as well... For one, I neglected to bleep out the Device Key and ID of my Raspberry Pi in the repo. Could that be a (halfway viable, or at least likely) security concern?

And also, I have a truckload of version mismatch warnings in my VS error list. In other projects, I would simply spin up some version redirects in a Web.xml or App.xml file, but (with the exception of a "Runtime Directives" XML file) those don't exist in this project. What can I do to remedy these issues?

Thanks!

Lucas Niewohner


回答1:


The capabilities of the application need to be enhanced. Since the application now needs to send bi directional information from it to azure. It needs both client and server checked in the application manifest




回答2:


In addition for Stuart Smith's reply, as your code provided in GitHub, it will cause deadlock because the Task method does not sign as completed when the all operations have been done.You can refer to this topic,maybe you will understand the differences between Task.Wait and await. I have modified the code like following, it works fine.

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;
         });
    }
}


来源:https://stackoverflow.com/questions/48414311/raspberry-pi-3-running-windows-10-core-with-custom-program-shows-crossed-out-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!