Esempio n. 1
0
        /// <summary>
        /// comPortInput_Click: Action to take when 'Connect' button is clicked
        /// - Get the selected device index and use Id to create the SerialDevice object
        /// - Configure default settings for the serial port
        /// - Create the ReadCancellationTokenSource token
        /// - Start listening on the serial port input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void comPortInput_Click(object sender, RoutedEventArgs e)
        {
            var selection = ConnectDevices.SelectedItems;

            if (selection.Count <= 0)
            {
                status.Text = "Select a device and connect";
                return;
            }

            DeviceInformation entry = (DeviceInformation)selection[0];

            try {
                serialPort = await SerialDevice.FromIdAsync(entry.Id);

                // Disable the 'Connect' button
                comPortInput.IsEnabled = false;

                // Configure serial settings
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
                serialPort.BaudRate     = (uint)baudCombox.SelectedItem;
                serialPort.Parity       = SerialParity.None;
                serialPort.StopBits     = SerialStopBitCount.One;
                serialPort.DataBits     = 8;
                serialPort.Handshake    = SerialHandshake.None;

                // Display configured settings
                status.Text  = "Serial port configured successfully: ";
                status.Text += serialPort.BaudRate + "-";
                status.Text += serialPort.DataBits + "-";
                status.Text += serialPort.Parity.ToString() + "-";
                status.Text += serialPort.StopBits;

                // Set the RcvdText field to invoke the TextChanged callback
                // The callback launches an async Read task to wait for data
                rcvdText.Text = "Waiting for data...";

                // Create cancellation token object to close I/O operations when closing the device
                ReadCancellationTokenSource = new CancellationTokenSource();

                // Enable 'WRITE' button to allow sending data
                ConnectDeviceFlyout.Hide();
                baudCombox.IsEnabled = false;
                ListenSerial();
            } catch (Exception ex1) {
                status.Text            = ex1.Message;
                comPortInput.IsEnabled = true;
            }
        }
Esempio n. 2
0
        private void sockConnect_Click(object sender, RoutedEventArgs e)
        {
            var    selection = ConnectDevices.SelectedItems;
            string ip        = ipAddrTextBox.Text;
            string port      = portTextBox.Text;

            //TODO check for correct input

            ApplicationDataCompositeValue composite = (ApplicationDataCompositeValue)localSettings.Values[Constants.SETTINGSKEY];

            if (composite != null)
            {
                composite[Constants.IPADDR] = ipAddrTextBox.Text;
                composite[Constants.PORT]   = portTextBox.Text;
                localSettings.Values[Constants.SETTINGSKEY] = composite;
            }


            try {
                status.Text = "Waiting for network...";
                // Set the RcvdText field to invoke the TextChanged callback
                // The callback launches an async Read task to wait for data
                rcvdText.Text = "Waiting for data...";


                // Create cancellation token object to close I/O operations when closing the device
                ReadCancellationTokenSource = new CancellationTokenSource();

                // Enable 'WRITE' button to allow sending data
                ConnectDeviceFlyout.Hide();

                ListenSock(ip, port);
            } catch (Exception ex1) {
                status.Text = ex1.Message;
            }
        }