Пример #1
0
        private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            lock (SerialPorts)
            {
                var availableSerialPorts         = GetAvailableSerialPorts();
                IEnumerable <string> uniqueItems = availableSerialPorts.Distinct <string>();

                if (eventType == EventType.Insertion)
                {
                    if (!SerialPorts.SequenceEqual(uniqueItems))
                    {
                        var added = uniqueItems.Except(SerialPorts).ToArray();
                        SerialPorts = added;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, added));
                    }
                }
                else if (eventType == EventType.Removal)
                {
                    if (SerialPorts.SequenceEqual(uniqueItems))
                    {
                        var removed = SerialPorts.Except(uniqueItems).ToArray();
                        SerialPorts = removed;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, removed));
                    }
                }
            }
        }
        private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            try
            {
                lock (_serialPorts)
                {
                    var availableSerialPorts = GetAvailablePorts();
                    switch (eventType)
                    {
                    case (EventType.Insertion):
                    {
                        var added = availableSerialPorts.Except(_serialPorts).ToArray();
                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, added));
                        break;
                    }

                    case (EventType.Removal):
                    {
                        var removed = _serialPorts.Except(availableSerialPorts).ToArray();
                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, removed));
                        break;
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
Пример #3
0
        private static void RaisePortsChangedIfNecessary(EventType eventType)
        {
            string[] availableSerialPorts;
            if (eventType == EventType.Insertion)
            {
                lock (_serialPorts)
                {
                    availableSerialPorts = GetAvailableSerialPorts();
                    if (!_serialPorts.SequenceEqual(availableSerialPorts))
                    {
                        var serialPort = availableSerialPorts.Except(_serialPorts).FirstOrDefault();
                        "Port added {0}"._DLOG(serialPort);

                        _serialPorts = availableSerialPorts;
                        PortsChanged.Raise(null, new PortsChangedArgs(eventType, serialPort));
                    }
                }
            }
            else
            {
                PortsChanged.Raise(null, new PortsChangedArgs(eventType, "No matter. We will try."));

                lock (_serialPorts)
                {
                    availableSerialPorts = GetAvailableSerialPorts();
                    if (!_serialPorts.SequenceEqual(availableSerialPorts))
                    {
                        var serialPort = _serialPorts.Except(availableSerialPorts).FirstOrDefault();
                        _serialPorts = availableSerialPorts;
                        "Port closed "._DLOG(serialPort);
                    }
                }
            }
        }
Пример #4
0
 private static void RaisePortsChangedIfNecessary(EventType eventType)
 {
     lock (_serialPorts) {
         var availableSerialPorts = GetAvailableSerialPorts();
         if (!_serialPorts.SequenceEqual(availableSerialPorts))
         {
             _serialPorts = availableSerialPorts;
             PortsChanged.Raise(null, new PortsChangedArgs(eventType, _serialPorts));
         }
     }
 }
Пример #5
0
        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock ( _syncObject ) {
                try {
                    timer.Stop();
                }
                catch (ObjectDisposedException) {
                    // See the try-catch construction at timer.Start() for more information.
                    if (_Disposed)
                    {
                        throw;
                    }
                }
            }

            lock (SerialPortService.serial_ports) {
                var availableSerialPorts = GetAvailableSerialPorts();
                if (!serial_ports.SequenceEqual(availableSerialPorts))
                {
                    serial_ports = availableSerialPorts;

                    // debug info
                    if (Constants.DEBUG_STATUS == true)
                    {
                        System.Diagnostics.Debug.Write("SerialPorts:");
                        foreach (string port in serial_ports)
                        {
                            System.Diagnostics.Debug.Write("\t");
                            System.Diagnostics.Debug.Write(port);
                        }
                        System.Diagnostics.Debug.Write("\r\n\r\n");
                    }
                }

                // Always fire this event, since we now need access to other device without
                // a serial port as well (such as STM32 bootloader).
                PortsChanged.Raise(null, new PortsChangedArgs(EventType.All, serial_ports));
            }
        }