Пример #1
0
        private void SearchDevice(string PortName, AsyncOperation asyncOp)
        {
            SerialportChannel port = new SerialportChannel(PortName, 19200, ProtocolType.None);

            port.MinimalTimeout = this.Timeout;

            for (int i = 0; i < this.Protocols.Length && !this.SearchingStopped; i++)
            {
                port.RelkonProtocolType = this.Protocols[i];
                for (int j = 0; j < this.BaudRates.Length && !this.SearchingStopped; j++)
                {
                    port.BaudRate = this.BaudRates[j];
                    try
                    {
                        port.Open();
                        byte[] res = port.SendRequest(this.Request, this.Pattern.Length);
                        port.DiscardInBuffer();

                        if (res != null)
                        {
                            string s = Encoding.ASCII.GetString(res);

                            if (s.ToLower().Contains(this.Pattern.ToLower()) || s.ToLower().Contains(this.BootPattern.ToLower()))
                            {
                                // Устройство обнаружено
                                this.devicePort     = port;
                                this.deviceResponse = res;
                            }
                        }
                    }
                    catch { }
                    finally
                    {
                        port.Close();
                    }
                    this.ChangeProgressMethod(asyncOp);
                }
            }

            lock (this.requestingPorts)
            {
                this.requestingPorts.Remove(PortName);
                if (this.requestingPorts.Count == 0)
                {
                    if (this.devicePort != null)
                    {
                        this.CompletionMethod(this.devicePort, this.deviceResponse, null, false, asyncOp);
                    }
                    else
                    {
                        this.CompletionMethod(null, null, (this.canceled ? null : new Exception("Устройство не найдено")), this.canceled, asyncOp);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Запускает поиск устройства
        /// </summary>
        public void StartSearch()
        {
            lock (this.requestingPorts)
            {
                if (this.requestingPorts.Count != 0)
                {
                    throw new Exception("Поиск устройства уже запущен, необходимо дождаться завершения");
                }
            }

            this.deviceResponse = null;
            this.canceled       = false;

            if (devicePort != null)
            {
                byte[] res = { };
                try
                {
                    devicePort.Open();
                    res = devicePort.SendRequest(this.Request, this.Pattern.Length);
                    if (res != null && (Encoding.ASCII.GetString(res).ToLower().Contains(this.Pattern.ToLower()) ||
                                        Encoding.ASCII.GetString(res).ToLower().Contains(this.BootPattern.ToLower())))
                    {
                        this.deviceResponse = res;
                    }
                }
                catch { }
                finally
                {
                    devicePort.Close();
                }
            }

            if (deviceResponse != null)
            {
                DeviceSearchCompletedEventArgs e = new DeviceSearchCompletedEventArgs(this.devicePort, this.deviceResponse, null, canceled);
                if (this.DeviceSearchCompleted != null)
                {
                    this.DeviceSearchCompleted(this, e);
                }
            }
            else
            {
                this.devicePort = null;
                this.progress   = 0;
                this.portsCount = 0;

                string[] portNames = SerialPort.GetPortNames();
                this.portsCount = portNames.Length;
                foreach (string portName in portNames)
                {
                    if (!this.requestingPorts.Keys.Contains(portName))
                    {
                        WorkerEventHandler workerDelegate = new WorkerEventHandler(this.SearchDevice);
                        AsyncOperation     asyncOp        = AsyncOperationManager.CreateOperation(portName);
                        this.requestingPorts.Add(portName, asyncOp);
                        workerDelegate.BeginInvoke(portName, asyncOp, null, null);
                    }
                }
            }
        }