public static void ProccesAsync()
 {
     MainGateClient mainGate = new MainGateClient(new InstanceContext(new DummyListener()));
     /*System.IO.DirectoryInfo dir= new System.IO.DirectoryInfo(@"..\..\..\..\..");
     Console.WriteLine("{0}", dir.FullName);
     Console.ReadLine();*/
     PldParser pldParser = new PldParser(@"..\..\..\..\..\Emulators\Data\pldx\C1_01.10.2011.pldx");
     for (;;)
     {
         foreach (Fusion fusion in pldParser.Fusions)
         {
             foreach (TrendPoint point in fusion.Points)
             {
                 mainGate.PushEvent(new OffGasEvent(point.H2,point.O2,point.CO,point.CO2,point.N2,point.Ar));
                 Console.WriteLine("{0}", string.Format("H2={0} O2={1} CO={2} CO2={3} N2={4} Ar={5} send",
                           point.H2,point.O2,point.CO,point.CO2,point.N2,point.Ar));
                 System.Threading.Thread.Sleep(200);
             }
         }
     }
 }
        private void Trends_Load(object sender, EventArgs e)
        {
            zgMain.IsEnableHZoom = false;
            zgMain.IsEnableVZoom = false;
            zgMain.GraphPane.XAxis.MajorGrid.IsVisible = false;
            zgMain.GraphPane.YAxis.MajorGrid.IsVisible = false;
            zgMain.GraphPane.XAxis.Scale.Min = 0;
            zgMain.GraphPane.YAxis.Scale.Max = 100;
            zgMain.GraphPane.XAxis.Scale.Min = 0;
            zgMain.GraphPane.XAxis.Scale.Max = 1800;
            zgMain.GraphPane.Legend.IsVisible = false;
            zgMain.GraphPane.Title.FontSpec.Size = 12;
            zgMain.GraphPane.XAxis.Title.Text = "";
            zgMain.GraphPane.YAxis.Title.Text = "";
            zgMain.GraphPane.XAxis.Scale.FontSpec.Size = 10;
            zgMain.GraphPane.YAxis.Scale.FontSpec.Size = 10;
            zgMain.GraphPane.XAxis.Scale.MajorStep = 300;
            zgMain.GraphPane.XAxis.Scale.MinorStep = 60;
            zgMain.GraphPane.XAxis.ScaleFormatEvent += new Axis.ScaleFormatHandler(XAxis_ScaleFormatEvent);
            List<PointPairList> pointPairLists = new List<PointPairList>();
            for (int i = 0; i < 6; i++ )
                pointPairLists.Add(new PointPairList());

            MainGateClient mainGate = new MainGateClient(new InstanceContext(new TrendsListener(zgMain, pointPairLists)));
            mainGate.Subscribe();
        }
        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;
        }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     MainGateClient mainGate = new MainGateClient(new InstanceContext(new EventsListener(this)));
     mainGate.Subscribe();
 }
        private void ReceivingThread()
        {
            m_MainGateClient = m_ConnectionName == null
                                   ? new MainGateClient(new InstanceContext(new PrimaryListener(m_EventListener)))
                                   : new MainGateClient(new InstanceContext(new PrimaryListener(m_EventListener)),
                                                        m_ConnectionName);
            m_MainGateClient.InnerDuplexChannel.Faulted += new EventHandler(InnerDuplexChannel_Faulted);
            m_MainGateClient.InnerDuplexChannel.Closed += new EventHandler(InnerDuplexChannel_Faulted);
            if (m_IsSubscribed &&
                (m_MainGateClient.State == CommunicationState.Opened ||
                 m_MainGateClient.State == CommunicationState.Created))
                m_MainGateClient.Subscribe();

            m_Initialized = true;
            do {
                Thread.Sleep(100);
            } while (true);
        }
 private void InnerDuplexChannel_Faulted(object obj1, object obj2)
 {
     //Console.WriteLine("Connection faulted");
     //Thread.Sleep(500);
     m_MainGateClient = m_ConnectionName == null
                            ? new MainGateClient(new InstanceContext(new PrimaryListener(m_EventListener)))
                            : new MainGateClient(new InstanceContext(new PrimaryListener(m_EventListener)),
                                                 m_ConnectionName);
     m_MainGateClient.InnerDuplexChannel.Faulted += new EventHandler(InnerDuplexChannel_Faulted);
     m_MainGateClient.InnerDuplexChannel.Closed += new EventHandler(InnerDuplexChannel_Faulted);
     if (m_IsSubscribed &&
         (m_MainGateClient.State == CommunicationState.Opened ||
          m_MainGateClient.State == CommunicationState.Created))
         m_MainGateClient.Subscribe();
 }