Пример #1
0
        public static void Uncheck(Guid uidGuid)
        {
            var index = GetIndex <CheckListItem>(uidGuid);

            if (AvailableSerialPorts.Any(availableSerialPort => uidGuid == Guid.Parse(availableSerialPort.UidGuid)))
            {
                var checkItem = new CheckListItem
                {
                    ItemName  = AvailableSerialPorts[index].ItemName,
                    UidGuid   = AvailableSerialPorts[index].UidGuid,
                    Content   = AvailableSerialPorts[index].Content,
                    IsChecked = false
                };
                AvailableSerialPorts.RemoveAt(index);
                AvailableSerialPorts.Insert(index, checkItem);
            }
            else if (AvailableModbusSerialPorts.Any(
                         availableModbusSerialPort => uidGuid == Guid.Parse(availableModbusSerialPort.UidGuid)))
            {
                var checkItem = new CheckListItem
                {
                    ItemName  = AvailableModbusSerialPorts[index].ItemName,
                    UidGuid   = AvailableModbusSerialPorts[index].UidGuid,
                    Content   = AvailableModbusSerialPorts[index].Content,
                    IsChecked = false
                };
                AvailableModbusSerialPorts.RemoveAt(index);
                AvailableModbusSerialPorts.Insert(index, checkItem);
            }
            else
            {
                throw new NullReferenceException();
            }
        }
Пример #2
0
        public static void ChangeCheck(Guid uidGuid)
        {
            var cboCheckListItem = OppositeCheck(uidGuid);
            var index            = GetIndex <CheckListItem>(uidGuid);

            if (cboCheckListItem == null)
            {
                return;
            }
            var cbo = (CheckListItem)cboCheckListItem;

            cbo.IsChecked = false;
            CheckListItem item;

            switch (CheckType(uidGuid))
            {
            case 1:
                //disables Modbus COM Port
                item = AvailableSerialPorts.First(
                    availableSerialPorts => Guid.Parse(availableSerialPorts.UidGuid) == uidGuid);
                item.IsChecked = false;
                AvailableModbusSerialPorts.RemoveAt(index);
                AvailableModbusSerialPorts.Insert(index, cbo);
                break;

            case 2:

                //disables Serial COM Port
                item = AvailableModbusSerialPorts.First(
                    availableModbusSerialPorts => Guid.Parse(availableModbusSerialPorts.UidGuid) == uidGuid);
                item.IsChecked = false;

                AvailableSerialPorts.RemoveAt(index);
                AvailableSerialPorts.Insert(index, cbo);
                break;

            case null:
                return;

            default:
                return;
            }
        }
        /// <summary>
        /// Proivdes a threadsafe way to close the serial ports
        /// </summary>
        /// <param name="index">
        /// Index for Dynamic Server Allocation
        /// </param>
        public static void CloseSerialPortUnexpectedly(int index, Dispatcher uiDispatcher)
        {
            if (!SerialPorts[index].IsOpen)
            {
                return;
            }
            while (SerialPorts[index].IsOpen)
            {
                SerialPorts[index].Close();
            }

            uiDispatcher.Invoke((MethodInvoker) delegate
            {
                ;
                var i = 0;
                i    += AvailableSerialPorts.TakeWhile(e => Guid.Parse(e.UidGuid) == SerialCommunications[index].UidGuid)
                        .Count();

                if (i > (AvailableSerialPorts.Count - 1))
                {
                    i  = 0;
                    i += AvailableModbusSerialPorts
                         .TakeWhile(e => Guid.Parse(e.UidGuid) == SerialCommunications[index].UidGuid)
                         .Count();
                    if (i > (AvailableModbusSerialPorts.Count - 1))
                    {
                        throw new NullReferenceException();
                    }

                    var checklistItem = new CheckListItem
                    {
                        IsChecked = false,
                        Content   = AvailableModbusSerialPorts[i].Content,
                        ItemName  = AvailableModbusSerialPorts[i].ItemName,
                        UidGuid   = AvailableModbusSerialPorts[i].UidGuid
                    };

                    AvailableModbusSerialPorts.RemoveAt(i);
                    AvailableModbusSerialPorts.Insert(i, checklistItem);
                }
                else
                {
                    var checkListItem = new CheckListItem
                    {
                        Content   = AvailableSerialPorts[i].Content,
                        IsChecked = false,
                        ItemName  = AvailableSerialPorts[i].ItemName,
                        UidGuid   = AvailableSerialPorts[i].UidGuid
                    };

                    AvailableSerialPorts.RemoveAt(i);
                    AvailableSerialPorts.Insert(i, checkListItem);
                }

                MessageBox.Show(
                    @"Error with Serial Port. Closing connection. Please check settings and connection and try again.",
                    @"Serial Port Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            });
            SerialPorts          = RemoveAtIndex <SerialPort>(index, SerialPorts);
            SerialCommunications = RemoveAtIndex <SerialCommunication>(index, SerialCommunications);
            StatsGroupings       = RemoveAtIndex <int>(index, StatsGroupings);
            ServerInformation    = RemoveAtIndex <RabbitServerInformation>(index, ServerInformation);
            Lineseries           = RemoveAtIndex <LineSeries>(index, Lineseries);

            while (FactoryChannel[index].IsOpen)
            {
                FactoryChannel[index].Close();
            }
            while (FactoryConnection[index].IsOpen)
            {
                FactoryConnection[index].Close();
            }

            FactoryChannel    = RemoveAtIndex <IModel>(index, FactoryChannel);
            FactoryConnection = RemoveAtIndex <IConnection>(index, FactoryConnection);
            Factory           = RemoveAtIndex <IConnectionFactory>(index, Factory);
        }