Пример #1
0
        /// <summary>
        /// 设置模板内容
        /// </summary>
        /// <param name="p_strTemplateText">模板内容</param>
        public void m_mthSetTemplateText(string p_strTemplateText)
        {
            p_strTemplateText = p_strTemplateText.Replace("\r\n", "\n");
            if (p_strTemplateText.Length == 0)
            {
                this.Text = "";
                return;
            }

            int intIndex = 0;

            int intPreIndex = intIndex;

            bool blnIsStart = true;

            System.Collections.ArrayList arlTemp     = new System.Collections.ArrayList();
            System.Text.StringBuilder    sbdTempText = new System.Text.StringBuilder(p_strTemplateText.Length);

            while (intIndex >= 0)
            {
                string strCurrentSymbol = blnIsStart?"{{":"}}";

                intIndex = p_strTemplateText.IndexOf(strCurrentSymbol, intIndex);

                if (intIndex >= 0)
                {
                    if (!blnIsStart)
                    {
                        //找到匹配的结束符
                        clsGroupInfo objGroup = new clsGroupInfo();
                        objGroup.m_objGroup      = new clsSelectInfoGroup();
                        objGroup.m_intStartIndex = sbdTempText.Length;
                        sbdTempText.Append(objGroup.m_objGroup.m_strSetSelectInfoFormat(p_strTemplateText.Substring(intPreIndex, intIndex - intPreIndex)));
                        objGroup.m_intEndIndex = sbdTempText.Length - 1;

                        arlTemp.Add(objGroup);

                        intPreIndex = intIndex + 2;
                        blnIsStart  = true;
                    }
                    else
                    {
                        //找到开始符
                        //添加开始符到前一个Index的内容
                        sbdTempText.Append(p_strTemplateText.Substring(intPreIndex, intIndex - intPreIndex));

                        intPreIndex = intIndex + 2;
                        blnIsStart  = false;
                    }
                }
            }

            if (intPreIndex < p_strTemplateText.Length)
            {
                sbdTempText.Append(p_strTemplateText.Substring(intPreIndex));
            }

            m_objGroupInfosArr  = (clsGroupInfo[])arlTemp.ToArray(typeof(clsGroupInfo));
            this.Text           = "";
            this.SelectionColor = m_clrNormalText;
            this.Text           = sbdTempText.ToString();

            m_mthSetNeedSelectedColor();
        }
        public bool DoInit()
        {
            string[] itemstrings = new string[5];
             string sPLC_Adr = "*UnDef*",sAlias = "*UnDef*",sGroup = "",sDesc = "";
             int iPLCDataLength;

             sAliasToControl = "ACT_C1_STAHLMARKE0";
             itemstrings[2] = itemstrings[3] = itemstrings[4] = "-";
             try {
            selectedOpcSrv = "OPC.SimaticNET.1";   // ����� ���� ��������
            AddLogg("������� �� " + sCompName);
            AddLogg("LoadDefinitions()-Start");
            LoadDefinitions();
            AddLogg("LoadDefinitions()-End");

            theSrv = new OpcServer();
            if (!DoConnect(selectedOpcSrv)) {
               AddLogg("DoConnect(" + selectedOpcSrv + ")=false");
               //////////return false;
               }
            // add event handler for server shutdown
            theSrv.ShutdownRequested += new ShutdownRequestEventHandler(this.theSrv_ServerShutDown);
            // precreate the only OPC group in this example
            if (!CreateGroup()) {
               AddLogg("CreateGroup()=false");
               //////////return false;
               }
            AddLogg("DoInit: Start");
            RemoveItem();		// first remove previous item if any
            cbCtrlAliases.Items.Clear();
            groupLi = new List<clsGroupInfo>();
            pointLi = new List<clsPointInfo>();
            j = 0;
            for (i = 0; i < strItems_Alias.Count; i++) {
               try {
                  itemclsPointInfo = new clsPointInfo();
                  itmHandleClient = j;
                  sPLC_Adr = strItems_adrPLC[i];   // "LOCATION0   PLC13:DB2,STRING66,8" // S7:[PLC13]DB2,STRING66,8
                  sAlias = strItems_Alias[i];
                  sGroup = strItems_Groups[i];
                  sDesc = strItems_Desc[i];
                  //k = sPLC_Adr.IndexOf("STRING");
                  k = sPLC_Adr.IndexOf("CHAR");
                  if (k < 0) {
                     itemclsPointInfo.iDataLength = 0;
                     }
                  else {
                     string[] sSplitted = sPLC_Adr.Trim().Split(',');
                     itemclsPointInfo.iDataLength = Convert.ToInt32(sSplitted[sSplitted.GetLength(0) - 1].Trim());
                     }
                  OPCItemDef[] aD = new OPCItemDef[1];
                  aD[0] = new OPCItemDef(sPLC_Adr,true,itmHandleClient,VarEnum.VT_EMPTY);
                  OPCItemResult[] arrRes;
                  theGrp.AddItems(aD,out arrRes);
                  if (arrRes == null) {
                     AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " arrRes == null");
                     //continue;
                     }
                  else {
                     if (arrRes[0].Error != HRESULTS.S_OK) {
                        AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " arrRes[0].Error != HRESULTS.S_OK");
                        //continue;
                        }
                     else {
                        itmHandleServer = arrRes[0].HandleServer;
                        itmAccessRights = arrRes[0].AccessRights;
                        itmTypeCode = VT2TypeCode(arrRes[0].CanonicalDataType);

                        txbActPoint.Text = sPLC_Adr;
                        txtItemDataType.Text = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType);

                        //if ((itmAccessRights & OPCACCESSRIGHTS.OPC_READABLE) != 0) {
                        //   int cancelID;
                        //   theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE,7788,out cancelID);
                        //   }
                        //else {
                        //   txtItemValue.Text = "no read access";
                        //   AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " no read access");
                        //   }
                        if (itmTypeCode != TypeCode.Object) {
                           // Object=failed!
                           ////AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " Object=failed");
                           // check if write is permitted
                           if ((itmAccessRights & OPCACCESSRIGHTS.OPC_WRITEABLE) != 0) {
                              //btnItemWrite.Enabled = true;
                              ////AddLogg("DoInit:AddItem" + i + " " + sPLC_Adr + " write is permitted");
                              }
                           }
                        itemstrings[0] = sAlias;
                        itemstrings[1] = sPLC_Adr;
                        //listOpcView.Items.Add(new ListViewItem(itemstrings,0));
                        itemclsPointInfo.sAlias = sAlias;
                        string[] sEvtNameSplitted = sAlias.Split('_');
                        //for (k = 0; k < sEvtNameSplitted.GetLength(0); k++){
                        //   AddLogg("DEBUG sEvtNameSplitted[" + k + "] = '" + sEvtNameSplitted[k]+"'");
                        //}
                        if (sEvtNameSplitted[0] == "ACT" || sEvtNameSplitted[0] == "SP") {   // Fool-Proof
                           if (sEvtNameSplitted[1] == "C1" || sEvtNameSplitted[1] == "C2" || sEvtNameSplitted[1] == "C3") {
                              itemclsPointInfo.iCnvNr = Convert.ToInt32(sEvtNameSplitted[1].Substring(1,1));
                              if (sEvtNameSplitted[0] == "SP"){
                                 itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(6,itemclsPointInfo.sAlias.Length -6);
                              }
                              else{
                                 itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(7,itemclsPointInfo.sAlias.Length - 7);
                                 }
                           }
                           else {
                              itemclsPointInfo.iCnvNr = 0;
                              if (sEvtNameSplitted[0] == "SP"){
                                 itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(3,itemclsPointInfo.sAlias.Length -3);
                              }
                              else{
                                 itemclsPointInfo.sAliasInfoTeil = itemclsPointInfo.sAlias.Substring(4,itemclsPointInfo.sAlias.Length - 4);
                                 }
                           }
                           if (sEvtNameSplitted[0] == "SP"){
                              itemclsPointInfo.bIsSetPoint = true;
                           }
                        }
                        else {
                           AddLogg("DoInit:AddItem" + i + " " + sAlias + " �� ���������� � 'ACT_'/'SP_'");
                           }
                        itemclsPointInfo.sPLC_Adr = sPLC_Adr;
                        itemclsPointInfo.sDesc = sDesc;
                        itemclsPointInfo.CanonicalDataType = arrRes[0].CanonicalDataType;
                        itemclsPointInfo.sCanonicalDataType = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType);
                        itemclsPointInfo.AccessRights = arrRes[0].AccessRights;
                        itemclsPointInfo.itmTypeCode = itmTypeCode;

                        pointLi.Add(itemclsPointInfo);
                        cbCtrlAliases.Items.Add(sAlias + " " + sPLC_Adr);
                        // ���� � ������ ����
                        //k = groupLi.FindIndex(delegate(clsGroupInfo lfdGroupInfo) {  // ** ��� ���������
                        //                         return lfdGroupInfo.sName == sGroup;
                        //                         }
                        //                     );
                        string @group = sGroup;                                        // ** ����� !!!
                        k = groupLi.FindIndex(lfdGroupInfo => lfdGroupInfo.sName == group);
                        if (k < 0) {                                           // ��� ������ ����� ������, ��������������, ��� TAKE_OVER !!
                           itemclsGroupInfo = new clsGroupInfo();
                           itemclsGroupInfo.sName = sGroup;
                           groupLi.Add(itemclsGroupInfo);
                           pointLi[pointLi.Count - 1].bIsTakeOver = true;  // rueckwerts in PointLi schreiben
                           }
                        k = pointLi.Count - 1;                             // wegen DEBUG
                        pointLi[k].iIdxInGroupLi = groupLi.Count - 1;      // gehoert zu dieser Gruppe;rueckwerts in PointLi schreiben
                        k = groupLi.Count - 1;                             // wegen DEBUG
                        groupLi[k].iItemsCnt++;                            // so viele Punkte in der Gruppe
                        groupLi[k].iIdxInPointLi.Add(pointLi.Count - 1);   // eigentlisch Punkte(als pointLi[Index])
                        DoStoreToOracle(j);                                // AnfangsZustand in Oracle generieren
                        AddLogg("�������� Pkt" + j + " group '" + groupLi[k].sName + "',Alias'" + sAlias + "' " + sPLC_Adr);
                        j++;
                        }
                     }
                  }
               catch (/*COMException*/ Exception comExc)
               {
                  //MessageBox.Show(this,"AddItem " + sPLC_Adr + " OPC error!","DoInit",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                  AddLogg("DoInit:AddItem" + i + " '" + sPLC_Adr + "' OPC exception:" + comExc.Message);
                  //return false;
                  continue;
                  }
               }
            int cancelID;
            theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE,7788,out cancelID);
            }
             catch (Exception e) {		// exceptions MUST be handled
            //MessageBox.Show(this,"init error! " + e.ToString(),"Exception",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            AddLogg("DoInit:AddItem" + i + " '" + sPLC_Adr + "' init error! " + e.Message);
            AddLogg("DoInit: Ende - false");
            return false;
            }
             AddLogg("DoInit: Ende-true,� pointLi " + pointLi.Count + " � groupLi " + groupLi.Count);
             mainGate = new MainGateClient(new InstanceContext(new DummyListener()));

             return true;
        }