示例#1
0
        public override object Read(plcdb_lib.Models.Model.TagsRow t)
        {
            if (!Client.Connected())
            {
                int connResult = Client.ConnectTo(ControllerInfo.Address, (int)ControllerInfo.s7_rack, (int)ControllerInfo.s7_slot);
                if (connResult != 0)
                {
                    Log.Error(GetErrorText(connResult));
                    Client.Disconnect();
                    Client = null;
                    Client = new S7Client();
                    throw new EntryPointNotFoundException("Unable to connect to PLC " + ControllerInfo.Name);
                }
                bgReadThread.DoWork      -= ReadFromPLC;
                bgReadThread.DoWork      += ReadFromPLC;
                RefreshDataTimer.Interval = 1000;
                RefreshDataTimer.Elapsed -= RefreshDataTimer_Elapsed;
                RefreshDataTimer.Elapsed += RefreshDataTimer_Elapsed;
                RefreshDataTimer.Start();
            }

            SiemensTag Tag = ActiveTags.FirstOrDefault(p => p.TagRow.PK == t.PK);

            if (Tag == null)
            {
                Tag = new SiemensTag()
                {
                    TagRow = t
                };
                ActiveTags.Add(Tag);
            }
            return(Tag.TagRow.CurrentValue);
        }
示例#2
0
        private void SetPropertiesFromTagRow(Model.TagsRow value)
        {
            SiemensTag Tag = CreateTag(value.Address);

            Start  = Tag.Start;
            Length = Tag.Length;
            Bit    = Tag.Bit;
        }
示例#3
0
 private void WriteToPLC(SiemensTag t, object val)
 {
     lock (Client)
     {
         SiemensTag Tag = ActiveTags.FirstOrDefault(p => p.TagRow.PK == t.TagRow.PK);
         if (Tag == null)
         {
             ActiveTags.Add(new SiemensTag()
             {
                 TagRow = t.TagRow
             });
         }
         int result = 0;
         if (Tag.Length == SiemensTag.AddressLength.Bit) //if bit is used, start address is byte_number*8 + bit_number
         {
             byte mask = (byte)(1 << Tag.Bit);
             if (Convert.ToBoolean(val))
             {
                 Buffer[Tag.Start] |= mask;
             }
             else
             {
                 Buffer[Tag.Start] &= mask;
             }
             result = Client.WriteArea((int)Tag.AddressSpace, Tag.DB, Tag.Start * 8 + Tag.Bit, 1, (int)Tag.Length, Buffer);
         }
         else if (Tag.Length == SiemensTag.AddressLength.DWord || Tag.Length == SiemensTag.AddressLength.Real)
         {
             byte[] ConvertedToBytes = BitConverter.GetBytes((int)val);
             for (int i = 0; i < 4; i++)
             {
                 Buffer[Tag.Start + i] = ConvertedToBytes[i];
             }
             result = Client.WriteArea((int)Tag.AddressSpace, Tag.DB, Tag.Start, 2, (int)Tag.Length, Buffer);
         }
         else
         {
             byte[] ConvertedToBytes = BitConverter.GetBytes(short.Parse(val.ToString()));
             for (int i = 0; i < 2; i++)
             {
                 Buffer[Tag.Start + i] = ConvertedToBytes[i];
             }
             result = Client.WriteArea((int)Tag.AddressSpace, Tag.DB, Tag.Start, 1, (int)Tag.Length, Buffer);
         }
         if (result != 0)
         {
             Log.Error("Error writing tag " + Tag.TagRow + ", " + GetErrorText(result));
             Client.Disconnect();
             Client = null;
             Client = new S7Client();
         }
     }
 }
示例#4
0
        public override bool Write(plcdb_lib.Models.Model.TagsRow t, object val)
        {
            SiemensTag Tag = ActiveTags.FirstOrDefault(p => p.TagRow.PK == t.PK);

            if (Tag == null)
            {
                Tag = new SiemensTag()
                {
                    TagRow = t
                };
                ActiveTags.Add(Tag);
            }
            WriteToPLC(Tag, val);
            return(true);
        }
示例#5
0
        public static SiemensTag CreateTag(String address)
        {
            SiemensTag NewTag = new SiemensTag();

            NewTag.Bit = -1;
            address    = address.ToUpper();
            if (DbRegex.IsMatch(address))
            {
                char AddressType = Regex.Match(address, "(DBB|DBX|DBW|DBD)").Value[2];
                if (AddressType == 'X')
                {
                    NewTag.Length = AddressLength.Bit;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"DBX[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(3));
                    NewTag.Bit    = int.Parse(Regex.Match(address, @"\.[0-7]").Value.Substring(1));
                }
                else if (AddressType == 'B')
                {
                    NewTag.Length = AddressLength.Byte;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"DBB[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(3));
                }
                else if (AddressType == 'W')
                {
                    NewTag.Length = AddressLength.Word;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"DBW[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(3));
                }
                else if (AddressType == 'D')
                {
                    NewTag.Length = AddressLength.DWord;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"DBD[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(3));
                }
            }
            else if (MRegex.IsMatch(address))
            {
                Match AddressTypeMatch = Regex.Match(address, "(MB|MW|MD)");
                if (!AddressTypeMatch.Success)
                {
                    NewTag.Length = AddressLength.Bit;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"M[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(1));
                    NewTag.Bit    = int.Parse(Regex.Match(address, @"\.[0-7]").Value.Substring(1));
                }
                else
                {
                    char AddressType = AddressTypeMatch.Value[1];
                    if (AddressType == 'B')
                    {
                        NewTag.Length = AddressLength.Byte;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"MB[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'W')
                    {
                        NewTag.Length = AddressLength.Word;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"MW[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'D')
                    {
                        NewTag.Length = AddressLength.DWord;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"MD[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                }
            }
            else if (IRegex.IsMatch(address))
            {
                Match AddressTypeMatch = Regex.Match(address, "(IB|IW|ID)");
                if (!AddressTypeMatch.Success)
                {
                    NewTag.Length = AddressLength.Bit;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"I[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(1));
                    NewTag.Bit    = int.Parse(Regex.Match(address, @"\.[0-7]").Value.Substring(1));
                }
                else
                {
                    char AddressType = AddressTypeMatch.Value[1];
                    if (AddressType == 'B')
                    {
                        NewTag.Length = AddressLength.Byte;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"IB[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'W')
                    {
                        NewTag.Length = AddressLength.Word;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"IW[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'D')
                    {
                        NewTag.Length = AddressLength.DWord;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"ID[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                }
            }
            else if (QRegex.IsMatch(address))
            {
                Match AddressTypeMatch = Regex.Match(address, "(QB|QW|QD)");
                if (!AddressTypeMatch.Success)
                {
                    NewTag.Length = AddressLength.Bit;
                    NewTag.Start  = int.Parse(Regex.Match(address, @"Q[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(1));
                    NewTag.Bit    = int.Parse(Regex.Match(address, @"\.[0-7]").Value.Substring(1));
                }
                else
                {
                    char AddressType = AddressTypeMatch.Value[1];
                    if (AddressType == 'B')
                    {
                        NewTag.Length = AddressLength.Byte;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"QB[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'W')
                    {
                        NewTag.Length = AddressLength.Word;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"QW[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                    else if (AddressType == 'D')
                    {
                        NewTag.Length = AddressLength.DWord;
                        NewTag.Start  = int.Parse(Regex.Match(address, @"QD[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(2));
                    }
                }
            }
            else if (CRegex.IsMatch(address))
            {
                NewTag.Length = AddressLength.Counter;
                NewTag.Start  = int.Parse(Regex.Match(address, @"C[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(1));
            }
            else if (TRegex.IsMatch(address))
            {
                NewTag.Length = AddressLength.Timer;
                NewTag.Start  = int.Parse(Regex.Match(address, @"T[0-9]+[0-9]*[0-9]*[0-9]*[0-9]*").Value.Substring(1));
            }
            else
            {
                return(null);
            }
            return(NewTag);
        }
示例#6
0
        public override bool ValidateTag(string address)
        {
            bool CreatedTagSuccess = SiemensTag.CreateTag(address) != null;

            return(CreatedTagSuccess);
        }