Esempio n. 1
0
        public async void OnStart()
        {
            oPCGroups    = new OPCController(ServerName, PLCName, "PLCAutoR");
            isConnecting = true;
            var t1 = Task.Run(() =>
            {
                while (isConnecting)
                {
                    Task.Run(() =>
                    {
                        if (Interlocked.Exchange(ref readTimer, 1) == 0)
                        {
                            Read();
                            Interlocked.Exchange(ref readTimer, 0);
                        }
                    });
                    Thread.Sleep(1000);
                }
            });
            var t = Task.Run(() =>
            {
                while (isConnecting)
                {
                    Task.Run(() =>
                    {
                        Write();
                    });
                    Thread.Sleep(500);
                }
            });
            await t;
            await t1;

            oPCGroups.Dispose();
        }
Esempio n. 2
0
 public void Write()
 {
     if (Interlocked.Exchange(ref writeTimer, 1) == 0)
     {
         if (IntData.IsOPCConnected)
         {
             preparedWritesQueue();
             List <OPCItems> taskList = new List <OPCItems>();
             foreach (OPCItems item in writeTaskDic.Values)
             {
                 taskList.Add(item);
             }
             writeTaskDic = new Dictionary <string, OPCItems>();
             if (taskList.Count > 0)
             {
                 using (OPCController oPCGroup_write = new OPCController(ServerName, PLCName, "PCLAutoW"))
                 {
                     oPCGroup_write.PutData(taskList);
                 }
             }
         }
         Interlocked.Exchange(ref writeTimer, 0);
     }
 }
Esempio n. 3
0
 private void refreshIndicators(int idx)
 {
     var t = Task.Run(() =>
     {
         Control label     = leds["L" + idx];
         Control transLed  = leds["T" + idx];
         Control xcieveLed = leds["X" + idx];
         try
         {
             busyLeds[idx] = true;
             if (IntData.OPCControllers.Count >= idx)
             {
                 OPCController item = IntData.OPCControllers[idx - 1];
                 if (item != null)
                 {
                     if (!label.Enabled)
                     {
                         setEnabled(label, true);
                         setEnabled(transLed, true);
                         setEnabled(xcieveLed, true);
                     }
                     if (item.GroupName != label.Text)
                     {
                         setText(label, item.GroupName);
                     }
                     if (transLed.BackColor == Color.DimGray)
                     {
                         if (item.TransactionFlag)
                         {
                             transLed.BackColor = Color.Lime;
                             Thread.Sleep(200);
                         }
                     }
                     else
                     {
                         if (!item.Transacting)
                         {
                             item.TransactionFlag = false;
                             transLed.BackColor   = Color.DimGray;
                             Thread.Sleep(50);
                             for (int i = 0; i < item.TransationSum; i++)
                             {
                                 if (item.Quality == "Good")
                                 {
                                     xcieveLed.BackColor = Color.Lime;
                                 }
                                 else
                                 {
                                     xcieveLed.BackColor = Color.Crimson;
                                 }
                                 Thread.Sleep(200 / item.TransationSum);
                                 xcieveLed.BackColor = Color.DimGray;
                                 Thread.Sleep(200 / item.TransationSum);
                             }
                         }
                     }
                 }
                 else
                 {
                     if (xcieveLed.Text != "(EMPTY)")
                     {
                         setText(label, "(EMPTY)");
                         xcieveLed.BackColor = Color.DimGray;
                         transLed.BackColor  = Color.DimGray;
                         setEnabled(label, false);
                         setEnabled(transLed, false);
                         setEnabled(xcieveLed, false);
                     }
                 }
             }
             else
             {
                 if (label.Text != "(READY)")
                 {
                     if (transLed.BackColor == Color.Lime)
                     {
                         transLed.BackColor = Color.DimGray;
                         Thread.Sleep(50);
                         xcieveLed.BackColor = Color.Lime;
                         Thread.Sleep(200);
                         xcieveLed.BackColor = Color.DimGray;
                     }
                     if (label.Text != "Channel " + idx)
                     {
                         Thread.Sleep(500);
                     }
                     setText(label, "(READY)");
                     setEnabled(label, false);
                     setEnabled(transLed, false);
                     setEnabled(xcieveLed, false);
                 }
             }
         }
         catch
         {
             if (label.Text != "ERROR")
             {
                 setText(xcieveLed, "ERROR");
                 xcieveLed.BackColor = Color.DimGray;
                 transLed.BackColor  = Color.DimGray;
                 setEnabled(label, false);
                 setEnabled(transLed, false);
                 setEnabled(xcieveLed, false);
             }
         }
         finally
         {
             busyLeds[idx] = false;
         }
     });
 }
Esempio n. 4
0
        private void WriteOPC(string sqlQuery, TagHandler Tags)
        {
            Logging log       = new Logging(sqlQuery);
            string  tableName = "";
            string  operation = "";
            int     CantTags  = 0;

            SQLHandler.ParseTable(sqlQuery, ref tableName, ref operation);
            List <string> TagValues   = new List <string>();
            List <string> TagNames    = new List <string>();
            List <string> ServerNames = new List <string>();
            List <string> PLCNames    = new List <string>();
            List <string> Register    = new List <string>();

            if (tableName == "PLC2DB_Internal")
            {
                CantTags = Columns.Count;
                foreach (ColumnsCommand cc in Columns)
                {
                    string value = cc.Value;
                    TagValues.Add(cc.Format == "" ? value : String.Format(value, cc.Format));
                    TagNames.Add(cc.WriteToTag);
                    ServerNames.Add(cc.ServerName);
                    PLCNames.Add(cc.PLCName);
                    Register.Add(cc.TagAddress);
                }
            }
            else
            {
                try
                {
                    string ConnStr = IntData.DestConn;
                    using (var connection = new SqlConnection(ConnStr))
                    {
                        connection.Open();
                        SqlCommand    cmd = new SqlCommand(sqlQuery, connection);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        CantTags = sdr.FieldCount;

                        if (sdr.FieldCount > 0)
                        {
                            for (int i = 0; i < CantTags; i++)
                            {
                                sdr.Read();
                                ColumnsCommand cc = Columns[i];
                                TagValues.Add(sdr[0].ToString());
                                TagNames.Add(cc.WriteToTag.Trim() == "" ? cc.KeepInTag : cc.WriteToTag);
                                ServerNames.Add(cc.ServerName);
                                PLCNames.Add(cc.PLCName);
                                Register.Add(cc.TagAddress);
                            }
                        }
                        else if (tableName == "PI_Furn_Plan")
                        {
                            for (int i = 0; i < CantTags; i++)
                            {
                                sdr.Read();
                                ColumnsCommand cc = Columns[i];
                                TagValues.Add("0");
                                TagNames.Add(cc.WriteToTag == "" ? cc.KeepInTag : cc.WriteToTag);
                                ServerNames.Add(cc.ServerName);
                                PLCNames.Add(cc.PLCName);
                                Register.Add(cc.TagAddress);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Fatal(ex);
                }
            }
            //-----Perform specific changes and calculation with the record read from database
            if (tableName.ToLower() == "ophtdata")
            {
                //If they change the heat number, also change the year (1st part of the heat number)
                int ix1 = 0;
                int ix2 = 0;
                for (int j = 0; j < TagValues.Count; j++)
                {
                    if (Columns[j].Formula == "CCM_HEAT_1")
                    {
                        ix1 = j;
                    }
                    if (Columns[j].Formula == "CCM_HEAT_2")
                    {
                        ix2 = j;
                    }
                }

                if (Columns[ix2].LastValue != Convert.ToInt32(TagValues[ix2]))
                {
                    ColumnsCommand tt = Columns[ix1];
                    tt.LastValue = -1;
                    Columns[ix1] = tt;
                }
            }
            // ----------------------------------------- Write to the PLC

            int TagIndex = 0;
            int idxPLCName;


            if (ServerNames.Count != 0)
            {
                idxPLCName = -1;
                for (int i = 0; i < ServerNames.Count; i++)
                {
                    string serNam = ServerNames[i].ToString().Trim();
                    if (serNam != "-" && serNam != "")
                    {
                        idxPLCName = i;
                        break;
                    }
                }

                if (idxPLCName != -1)
                {
                    using (OPCController OPCGrp_w = new OPCController(ServerNames[idxPLCName], PLCNames[idxPLCName], "TableCmdW"))
                    {
                        System.Array  str         = new object[CantTags + 1];
                        List <string> PLCTagNames = new List <string>();
                        for (int i = 0; i < CantTags; i++)
                        {
                            if (Columns[i].ServerName != "" && Columns[i].ServerName != "-")
                            {
                                TagIndex++;
                                str.SetValue(TagValues[i], i + 1);
                                PLCTagNames.Add(TagNames[i]);
                                OPCGrp_w.AddItem(TagNames[i], Register[i], " ", PLCNames[i]);
                            }
                        }
                        OPCGrp_w.PutData(str);
                    }
                }
            }
            for (int i = 0; i < CantTags; i++)
            {
                ColumnsCommand cct = Columns[i];
                if (Columns[i].ServerName != "" && Columns[i].ServerName != "-")
                {
                    cct.LastValue = Convert.ToInt32(TagValues[i]);
                    Columns[i]    = cct;
                }
                else
                {
                    Tags.UpdateTagsValue(TagNames[i], TagValues[i]);
                }
            }
        }