从UWA到WPF的流数据

时间:2016-02-10 20:38:23

标签: c# wpf

我有一个通用Windows应用程序,我可以在其中实时收集Microsoft Band传感器数据。现在我想将这些传感器数据传输到WPF应用程序。我原本以为AppServiceConnection类可能是最好的方法,但是当我只想要服务提供商抓取传感器数据的实时通信时,我似乎无法理解如何使用这种通信过程。连续流式传输,客户端接收并显示数据。

这是一些代码: 客户端:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net.Sockets;
using System.Threading;
using System.Text;

using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;


namespace Socket_Communication_UWA
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {


        private StreamSocket clientSocket;
        private HostName serverHost;
        //private string serverHostnameString;
        //private string serverPort;
        private bool connected = false;
        private bool closing = false;

        public MainPage()
        {
            this.InitializeComponent();
            clientSocket = new StreamSocket();
        }



        private async void connect(object sender, RoutedEventArgs e)
        {
            if (connected)
            {
                StatusText.Text = "Already connected";
                return;
            }

            try
            {
                OutputView.Text = "";
                StatusText.Text = "Trying to connect ...";

                serverHost = new HostName(ServerHostname.Text);
                // Try to connect to the 
                await clientSocket.ConnectAsync(serverHost, ServerPort.Text);
                connected = true;
                StatusText.Text = "Connection established" + Environment.NewLine;

            }
            catch (Exception exception)
            {
                // If this is an unknown status, 
                // it means that the error is fatal and retry will likely fail.
                if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }

                StatusText.Text = "Connect failed with error: " + exception.Message;
                // Could retry the connection, but for this simple example
                // just close the socket.

                closing = true;
                // the Close method is mapped to the C# Dispose
                clientSocket.Dispose();
                clientSocket = null;

            }
        }

        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            if (!connected)
            {
                StatusText.Text = "Must be connected to send!";
                return;
            }

            UInt32 len = 0; // Gets the UTF-8 string length.

            try
            {
                OutputView.Text = "";
                StatusText.Text = "Trying to send data ...";

                // add a newline to the text to send
                string sendData = SendText.Text + Environment.NewLine;
                DataWriter writer = new DataWriter(clientSocket.OutputStream);
                len = writer.MeasureString(sendData); // Gets the UTF-8 string length.

                // Call StoreAsync method to store the data to a backing stream
                await writer.StoreAsync();

                StatusText.Text = "Data was sent" + Environment.NewLine;

                // await writer.FlushAsync();
                // writer.WriteString("go" + Environment.NewLine);
                // detach the stream and close it
                await writer.FlushAsync();
                writer.DetachStream();
                writer.Dispose();



                closing = true;
                clientSocket.Dispose();
                clientSocket = null;
                connected = false;

            }
            catch (Exception exception)
            {
                // If this is an unknown status, 
                // it means that the error is fatal and retry will likely fail.
                if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    throw;
                }

                StatusText.Text = "Send data or receive failed with error: " + exception.Message;
                // Could retry the connection, but for this simple example
                // just close the socket.

                closing = true;
                clientSocket.Dispose();
                clientSocket = null;
                connected = false;

            }

        }
    }
}

监听器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Net;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
using System.Threading;

namespace Socket_listener
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {



        string line;

        public MainWindow()
        {
            InitializeComponent();
        }


        public async void button_start_Click(object sender, RoutedEventArgs e)
        {
            Int32 port = 4510;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(localAddr, port);
            listener.Start();

            using (TcpClient client = await listener.AcceptTcpClientAsync())
            using (StreamReader reader = new StreamReader(client.GetStream(), Encoding.UTF8))
            {
                while ((line = reader.ReadLine()) != null)
                {
                    Thread.Sleep(10);
                    line = reader.ReadLine();
                    Console.WriteLine("This is the received message " + line);
                    //textBox.Text = line;
                    // client.Close();
                }
                //  Console.WriteLine("This is the received message " + line);

            }

            listener.Stop();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你可以使用SignalR lib。我已经成功地将它与SCADA网络一起用于WPF和Web应用程序。

您可以在SignalR InformationSignalR ASP.NET Site

找到有关此库的更多信息