4

I want to run UDP Recevie on RPi 3 and with Android app "UDP Sender" when send text, I want to control GPIO Pins.

I need help, how to make UDP Recevie for Windows 10 IoT? I search a lot but can't make it work.

Thanks in advance for help!

Mile M.
  • 131
  • 1
  • 2
  • 5

1 Answers1

5

I sloved the problem!

Here is code:

   public sealed partial class MainPage : Page
{
    DatagramSocket listenerSocket = null;
    const string port = "8080";
    public MainPage()
    {
        this.InitializeComponent();
        Listen();
        Send();
    }


private async void Listen()
{
    listenerSocket = new DatagramSocket();
    //listenerSocket.MessageReceived += (x, y) =>
    //{
    //    var a = "2";
    //};
    listenerSocket.MessageReceived += MessageReceived;
    await listenerSocket.BindServiceNameAsync(port);
}

private async void Send()
{
    IOutputStream outputStream;
    string localIPString = GetLocalIp();
    IPAddress localIP = IPAddress.Parse(localIPString);
    string subnetMaskString = "255.0.0.0";
    IPAddress subnetIP = IPAddress.Parse(subnetMaskString);
    HostName remoteHostname = new HostName(localIP.ToString());
    outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, port);

    using (DataWriter writer = new DataWriter(outputStream))
    {
        writer.WriteString("aaaa");
        await writer.StoreAsync();
    }


}

//private object GetBroadcastAddress(IPAddress localIP, IPAddress subnetIP)
//{
//    throw new NotImplementedException();
//}

async void MessageReceived (DatagramSocket socket, DatagramSocketMessageReceivedEventArgs args)
{
    DataReader reader = args.GetDataReader();
    uint len = reader.UnconsumedBufferLength;
    string msg = reader.ReadString(len);

    string remoteHost = args.RemoteAddress.DisplayName;
    reader.Dispose();

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        text.Text = msg;
    });

}

private string GetLocalIp()
{
   ...
}

}

And also don't forget to:

I found the solution. Apparently, you have to specifically enable that the application will be receiving incoming network traffic. The UWP project in Visual Studio contains a file named Package.appxmanifest. Double click it, select the Capabilities tab and check Internet (Client & Server). More about that at the MSDN - Networking basics.

Mile M.
  • 131
  • 1
  • 2
  • 5