Beispiel #1
0
 protected void StepDownInterfaceToStrings(List<string> list, CommunicationInterfaceComposite inputComposite, int startPos, int stopPos)
 {
     foreach (var component in inputComposite.Children.Where(component => component.Pos >= startPos && component.Pos < stopPos))
     {
         if (component.GetType() == typeof (CommunicationInterfaceComposite))
         {
             var composite = (CommunicationInterfaceComposite) component;
             StepDownInterfaceToStrings(list, composite, startPos, stopPos);
         }
         else
         {
             switch (component.TypeOfVariable)
             {
                 case CommunicationInterfaceComponent.VariableType.Bit:
                     var variableCastedBit = (CiBit)component;
                     list.Add(variableCastedBit.Pos + "." + variableCastedBit.BitPosition + "$" + variableCastedBit.Name + "$" + variableCastedBit.TypeOfVariable + "$" +
                              variableCastedBit.StringValue());
                     break;
                 default:
                     list.Add(component.Pos + "$" + component.Name + "$" + component.TypeOfVariable + "$" +
                              component.StringValue());
                     break;
             }
         }
     }
 }
        public override void Initialize()
        {
            if (_communicationThread.IsAlive)
            {
                _communicationThread.Abort();
                Thread.Sleep(500);

                _communicationThread = new Thread(CommunicationHandler);
                _communicationThread.SetApartmentState(ApartmentState.STA);
                _communicationThread.IsBackground = true;
            }

            _readInterfaceComposite = new CommunicationInterfaceHierarchicalBuilder().InitializeInterface(Header.Id,
                CommunicationInterfaceComponent.InterfaceType.ReadInterface, PathFile, this);
            _writeInterfaceComposite = new CommunicationInterfaceHierarchicalBuilder().InitializeInterface(Header.Id,
                CommunicationInterfaceComponent.InterfaceType.WriteInterface, PathFile, this);
            new DisplayDataHierarchicalBuilder().Build(_readInterfaceCollection, _writeInterfaceCollection, this);

            if (OnInterfaceUpdatedDelegate != null) OnInterfaceUpdatedDelegate();

            _communicationThread.Start();

            Logger.Log("ID: " + Header.Id + " Communication interface Initialized");
        }
Beispiel #3
0
 public List<string> InterfaceToStrings(CommunicationInterfaceComposite inputComposite, int startPos, int stopPos)
 {
     var list = new List<string>();
     StepDownInterfaceToStrings(list, inputComposite, startPos, stopPos);
     return list;
 }
        public CommunicationInterfaceComposite InitializeInterface(uint id, CommunicationInterfaceComponent.InterfaceType type, CommunicationInterfacePath pathFile, object sender)
        {
            var readAreaFound = false;
            var writeAreaFound = false;

            var readAddress = new Address { ByteAddress = 0, BitAddress = 0, };
            var writeAddress = new Address { ByteAddress = 0, BitAddress = 0, };

            var readByteOverloaded = false;
            var writeByteOverloaded = false;

            var interfaceComposite = new CommunicationInterfaceComposite(type.ToString()) { Owner = sender, TypeOfInterface = type };
            var reader = new StreamReader(pathFile.Path[id]);

            var previousReadType = "";
            var previousWriteType = "";

            string line;
            string[] words;

            switch (type)
            {
                case CommunicationInterfaceComponent.InterfaceType.ReadInterface:
                    while (true)
                    {
                        line = reader.ReadLine();
                        if (line == null) break;
                        words = line.Split(';');
                        if (readAreaFound && words[0] == "#END") break;
                        if (readAreaFound)
                        {
                            var readByteOverloadedAux = readByteOverloaded;
                            readAddress = CheckRules(previousReadType, words[1], readByteOverloadedAux, readAddress,
                                out readByteOverloaded);

                            AddToInterface(interfaceComposite, CommunicationInterfaceFactory.CreateVariable(words[0],
                                readAddress.ByteAddress, readAddress.BitAddress, StringToVariableType(words[1]),
                                GetLength(words[1])));

                            readAddress = CreateNewAddress(readAddress, words[1]);
                            previousReadType = words[1];
                        }
                        if (words[0] == "#READ") readAreaFound = true;
                    }
                    if (!readAreaFound)
                    {
                        throw new Exception("Read Area not found");
                    }
                    break;
                case CommunicationInterfaceComponent.InterfaceType.WriteInterface:
                    while (true)
                    {
                        line = reader.ReadLine();
                        if (line == null) break;
                        words = line.Split(';');
                        if (writeAreaFound && words[0] == "#END") break;
                        if (writeAreaFound)
                        {
                            var writeByteOverloadedAux = writeByteOverloaded;
                            writeAddress = CheckRules(previousWriteType, words[1], writeByteOverloadedAux, writeAddress,
                                out writeByteOverloaded);

                            AddToInterface(interfaceComposite, CommunicationInterfaceFactory.CreateVariable(words[0],
                                writeAddress.ByteAddress, writeAddress.BitAddress, StringToVariableType(words[1]),
                                GetLength(words[1])));

                            writeAddress = CreateNewAddress(writeAddress, words[1]);
                            previousWriteType = words[1];
                        }
                        if (words[0] == "#WRITE") writeAreaFound = true;
                    }
                    if (!writeAreaFound)
                    {
                        throw new Exception("Write Area not found");
                    }
                    break;
                default:
                    throw new Exception("Error: Wrong interface type.");
            }
            return interfaceComposite;
        }
 protected override void AddToInterface(CommunicationInterfaceComposite interfaceComposite, CommunicationInterfaceVariable variable)
 {
     interfaceComposite.Add(variable);
 }
        private static CommunicationInterfaceComposite CreateCompositeStructure(out string outputName, string name, int position, CommunicationInterfaceComposite baseComposite, char splitChar)
        {
            var nameSplit = name.Split(splitChar);
            var actualComposite = baseComposite;

            for (var i = 0; i < nameSplit.Length - 1; i++)
            {
                var newComposite = actualComposite.ReturnComposite(nameSplit[i]);

                if (newComposite == null)
                {
                    newComposite = new CommunicationInterfaceComposite(nameSplit[i]) { Pos = position };
                    actualComposite.Add(newComposite);
                }

                actualComposite = newComposite;
            }

            outputName = nameSplit[nameSplit.Length - 1];
            return actualComposite;
        }
        protected override void AddToInterface(CommunicationInterfaceComposite interfaceComposite, CommunicationInterfaceVariable variable)
        {
            string newCompositeName;

            var actualComposite = CreateCompositeStructure(out newCompositeName, variable.Name, variable.Pos, interfaceComposite, '.');
            actualComposite = CreateCompositeStructure(out newCompositeName, newCompositeName, variable.Pos, actualComposite, '[');
            actualComposite.Add(variable);
        }
 protected abstract void AddToInterface(CommunicationInterfaceComposite interfaceComposite, CommunicationInterfaceVariable variable);