コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LightTower"/> class.
        /// </summary>
        /// <param name="red">The red DO bit.</param>
        /// <param name="yellow">The yellow DO bit.</param>
        /// <param name="green">The green DO bit.</param>
        /// <param name="blue">The blue DO bit.</param>
        public LightTower(DOBit red, DOBit yellow, DOBit green, DOBit blue)
        {
            _red    = red;
            _yellow = yellow;
            _green  = green;
            _blue   = blue;
            _bits   = new DOBit[] { _red, _yellow, _green, _blue };

            _blinkTimer          = new Timer();
            _blinkTimer.Interval = 500;
            _blinkTimer.Elapsed += BlinkTimer_Elapsed;
            _blinkTimer.Enabled  = true;
        }
コード例 #2
0
        /// <summary>
        /// Creates the IO parts from comma separated strings.
        /// Uses the format "Name,Device,Channel,(ReadOnly | optional)"
        /// </summary>
        /// <param name="partLines">The string line describing a single IOPart.</param>
        /// <param name="type">The IO part type. ie. DIBit, DOBit, etc.</param>
        /// <param name="ioParts">The IO parts.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">IO part must be in the form {name},{device name},{channel}</exception>
        private IList <string> CreateIOParts(IEnumerable <string> partLines, IOPartType type, out IList <IIOPart> ioParts)
        {
            ioParts = new List <IIOPart>();
            List <string> forcedParts = new List <string>();

            foreach (string partLine in partLines)
            {
                IIOPart part = null;

                try
                {
                    string[] partInfo   = partLine.Split(',');
                    string   partName   = partInfo[0];
                    string   deviceName = partInfo[1];
                    int      channel    = -1;
                    bool     readOnly   = false;

                    if (partInfo.Length > 2)
                    {
                        int.TryParse(partInfo[2], out channel);
                    }

                    if (channel < 0)
                    {
                        throw new Exception("IO part must be in the form {name},{device name},{channel}");
                    }

                    if (String.Equals(partInfo.Last(), "ReadOnly", StringComparison.CurrentCultureIgnoreCase) ||
                        String.Equals(partInfo.Last(), "Reserved", StringComparison.CurrentCultureIgnoreCase))
                    {
                        readOnly = true;
                    }

                    if (string.IsNullOrEmpty(deviceName))
                    {
                        deviceName = GetSimDeviceType().Name;

                        Log.Error(this, "No device name specified for {0}. Switching to a Simulated {1} device instead.", partInfo[0], Name);
                    }

                    IIODevice device = GetDevice(deviceName);

                    if (device == null)
                    {
                        string simName = GetSimDeviceType().Name;

                        if (!String.Equals(deviceName, simName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            Log.Error(this, "No device found with the name '{0}'. Switching to a Simulated {1} device instead.", deviceName, Name);
                        }

                        device = GetSimDevice(deviceName);
                    }

                    try
                    {
                        if (!device.Connected)
                        {
                            device.Open();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(this, "{0} could not connect to {1} device '{2}' on port {3}. {4}", typeof(IIOPart).Name, device.GetType().Name, device.Name, device.Port, ex);
                        Log.Error(this, "Switching {0} '{1}' to simulated device instead.", typeof(IIOPart).Name, partName);
                        device = GetSimDevice(deviceName);
                    }

                    if (!_devMgr.Devices.Contains(deviceName))
                    {
                        _devMgr.Devices.Add(device);
                    }

                    switch (type)
                    {
                    case IOPartType.DIBit:
                        part = new DIBit(device);
                        break;

                    case IOPartType.DOBit:
                        part = new DOBit(device);
                        break;

                    case IOPartType.AIBit:
                        part = new AIBit(device);
                        break;

                    case IOPartType.AOBit:
                        part = new AOBit(device);
                        break;

                    case IOPartType.DIWord:
                        DIWord inWord = new DIWord(device);
                        InitializeWord(inWord, partInfo);
                        part = inWord;
                        break;

                    case IOPartType.DOWord:
                        DOWord outWord = new DOWord(device);
                        InitializeWord(outWord, partInfo);
                        part = outWord;
                        break;
                    }

                    part.Name     = partName;
                    part.Channel  = channel;
                    part.ReadOnly = readOnly;

                    ioParts.Add(part);

                    if (!String.Equals(device.Name, deviceName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        forcedParts.Add(part.Name);
                    }
                }
                catch (Exception ex)
                {
                    string error = "";

                    if (type == IOPartType.DIWord || type == IOPartType.DOWord)
                    {
                        error += "Word ";
                    }
                    else
                    {
                        error += "Bit ";
                    }

                    if (part != null && !String.IsNullOrEmpty(part.Name))
                    {
                        error += part.Name + " ";
                    }

                    error += String.Format("could not be parsed from string {0}.\n{1}", partLine, ex);

                    Log.Error(this, error);
                }
            }

            return(forcedParts);
        }