Exemplo n.º 1
0
        void Run()
        {
            if (udpAddress != null)
            {
                UdpPort udp = new UdpPort();
                udp.Connect(udpAddress);

                IPEndPoint remote = udp.WaitForOneMessage(TimeSpan.FromSeconds(10));
                if (remote == null)
                {
                    Console.WriteLine("### Error: not getting any messages from " + udpAddress);
                    return;
                }
                port = udp;
            }
            else if (comPortName != null)
            {
                SerialPort serial = new SerialPort();
                serial.Connect(comPortName, DefaultComBaudRate);
                port = serial;
            }
            if (proxyAddress != null)
            {
                proxyPort = new UdpPort();
                proxyPort.Connect(new IPEndPoint(udpAddress.Address, 0), proxyAddress);
                proxyChannel = new MavlinkChannel();
                proxyChannel.MessageReceived += OnProxyMessageReceived;
                proxyChannel.Start(proxyPort);
            }

            MavlinkChannel channel = new MavlinkChannel();
            channel.MessageReceived += OnMessageReceived;
            channel.Start(port);
        }
        private void OnUdpConnect(object sender, RoutedEventArgs e)
        {
            if (port != null)
            {
                port.Close();
                port = null;
                OnPortClosed();
            }
            else
            {
                var model = (NetworkModel)NetworkList.SelectedItem;
                IPAddress addr = null;
                if (model.Network != null)
                {
                    foreach (UnicastIPAddressInformation ip in model.Network.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            addr = ip.Address;
                        }
                    }
                }
                else
                {
                    addr = IPAddress.Parse("127.0.0.1");
                }

                int i = 0;
                if (int.TryParse(PortNumber.Text, out i))
                {
                    var udp = new UdpPort();
                    udp.Connect(new IPEndPoint(addr, i), null);
                    OnPortConnected(udp);
                    UdpConnectButton.Content = "Disconnect";
                }
            }
        }