Exemple #1
0
        /// <summary>
        /// Gets an instance the CM11A device and opens the specified port for communication.
        /// </summary>
        /// <param name="portName">The name of the port to open, such as COM1.</param>
        public static CM11AServer GetInstance(string portName)
        {
            if (string.IsNullOrEmpty(portName))
            {
                throw new ArgumentNullException(nameof(portName));
            }

            CM11AServer instance = null;

            lock (Instances)
            {
                if (Instances.ContainsKey(portName))
                {
                    instance = Instances[portName];
                }
                else
                {
                    try
                    {
                        instance = new CM11AServer(portName);
                    }
                    catch (Exception ex)
                    {
                        instance = null;
                        throw new IOException(string.Format("There was an error creating the CM11A device on {0}.", portName), ex);
                    }
                    finally
                    {
                        if (instance != null)
                        {
                            Interlocked.Increment(ref instance._refCount);
                            Instances.AddOrUpdate(portName, instance, (key, existingVal) => instance);
                        }
                    }
                }
            }

            return(instance);
        }
        /// <summary>
        /// Gets an instance the CM11A device and opens the specified port for communication.
        /// </summary>
        /// <param name="portName">The name of the port to open, such as COM1.</param>
        public static CM11AServer GetInstance(string portName)
        {
            if (string.IsNullOrEmpty(portName))
                throw new ArgumentNullException(nameof(portName));

            CM11AServer instance = null;
            lock (Instances)
            {
                if (Instances.ContainsKey(portName))
                {
                    instance = Instances[portName];
                }
                else
                {
                    try
                    {
                        instance = new CM11AServer(portName);
                    }
                    catch (Exception ex)
                    {
                        instance = null;
                        throw new IOException(string.Format("There was an error creating the CM11A device on {0}.", portName), ex);
                    }
                    finally
                    {
                        if (instance != null)
                        {
                            Interlocked.Increment(ref instance._refCount);
                            Instances.AddOrUpdate(portName, instance, (key, existingVal) => instance);
                        }
                    }
                }
            }

            return instance;
        }