Esempio n. 1
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. 2
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]);
                }
            }
        }