private void PortScanner_OnFindPort(AdvancedPortScanner sender, PortScannerEventArgs e)
 {
     Debug.WriteLine("[main] find port {0}", e.Link.Port.PortName);
     link = e.Link;
     link.OnReceivePackage += Link_OnReceivePackage;
     link.OpenPort();
     portScanner.Stop();
 }
Esempio n. 2
0
        private void Scantimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (portFound)
            {
                return;
            }
            if (!isStarted)
            {
                return;
            }
            string[] portnames = SerialPort.GetPortNames();
            //查找所有串口
            foreach (string pn in portnames)
            {
                bool flag = false;
                for (int i = 0; i < ports.Count; i++)
                {
                    if (ports[i].name == pn)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {//添加新出现的串口
                    PortData port = new PortData(pn, baudRate, maxDataSize);

                    port.lastCheckTime = DateTime.Now;

                    ports.Add(port);
                    Debug.WriteLine("[port scanner]find new port:" + pn);
                }
            }

            //更新各个串口状态
            for (int i = 0; i < ports.Count; i++)
            {
                switch (ports[i].state)
                {
                case PortScannerState.NewPort:
                    try
                    {
                        //ports[i].port.BaudRate = baudRate;
                        ports[i].port.Open();
                        ports[i].lastCheckTime = DateTime.Now;
                        ports[i].state         = PortScannerState.Scanning;
                        Debug.WriteLine("[port scanner]start scanning:" + ports[i].name);
                    }
                    catch
                    {
                        ports[i].state = PortScannerState.Unavailable;
                        Debug.WriteLine("[port scanner]cannot start scanning:" + ports[i].name);
                    }
                    break;

                case PortScannerState.Scanning:
                    if (DateTime.Now.Subtract(ports[i].lastCheckTime).TotalSeconds > timeout)
                    {
                        ports[i].port.Close();
                        //ports[i].port.DiscardInBuffer();
                        ports[i].state         = PortScannerState.Unavailable;
                        ports[i].lastCheckTime = DateTime.Now;
                        Debug.WriteLine("[port scanner]unavilable port(timeout):" + ports[i].name);
                    }

                    break;

                case PortScannerState.Available:
                    ports[i].port.Close();
                    portFound = true;
                    SerialLink link = new SerialLink(ports[i].name, ports[i].protocol, baudRate);
                    Debug.WriteLine("[port scanner]find port:" + ports[i].name + ports[i].protocol.ToString());
                    OnFindPort?.Invoke(this, new PortScannerEventArgs(link));
                    break;

                case PortScannerState.Unavailable:
                    if (ports[i].port.IsOpen)
                    {
                        ports[i].port.Close();
                    }
                    if (DateTime.Now.Subtract(ports[i].lastCheckTime).TotalSeconds > 1.0)
                    {
                        ports[i].state = PortScannerState.NewPort;
                    }
                    break;
                }
            }
        }
Esempio n. 3
0
 public PortScannerEventArgs(SerialLink l)
 {
     link     = l;
     portName = l.Port.PortName;
 }