Exemplo n.º 1
0
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            Com0comPortPair port = listPorts.SelectedValue as Com0comPortPair;

            if (port != null)
            {
                port.StartComms();
            }
        }
        /// <summary>
        /// Get all com0com Port Pairs currently installed in the system.
        /// </summary>
        /// <returns></returns>
        public static ObservableCollection <Com0comPortPair> GetPortPairs()
        {
            ObservableCollection <Com0comPortPair> ports = new ObservableCollection <Com0comPortPair>();
            //get the output from setupc --detail-prms list
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = _com0comSetupc,
                    Arguments              = "--detail-prms list",
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true
                }
            };

            proc.Start();
            while (!proc.StandardOutput.EndOfStream)
            {
                try
                {
                    string line = proc.StandardOutput.ReadLine();
                    //get port number
                    Regex regex   = new Regex(@"(?<=CNC[A,B])\d+(?=\s)");
                    int   portnum = int.Parse(regex.Match(line).Value);

                    Com0comPortPair pair = ports.FirstOrDefault(d => d.PairNumber == portnum);

                    if (pair == null)
                    {
                        pair = new Com0comPortPair(portnum);
                        ports.Add(pair);
                    }
                    regex = new Regex(@"(?<=CNC)[A,B](?=\d+\s)");
                    string portAB = regex.Match(line).Value;
                    if (portAB == "A")
                    {
                        pair.PortConfigStringA = line;
                    }
                    else if (portAB == "B")
                    {
                        pair.PortConfigStringB = line;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            return(ports);
        }